JPA @Embedded 注释是强制性的吗? [英] Is the JPA @Embedded annotation mandatory?

查看:31
本文介绍了JPA @Embedded 注释是强制性的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试省略 @Embedded 注释,但字段仍然嵌入到表中.我找不到任何可以说 @Embedded 注释是可选的.

I have tried omitting the @Embedded annotation and still the fields have been embedded in the table. I cannot find anything which would say that the @Embedded annotation is optional.

还是不是可选的?

以下代码

@Embeddable
public class Address {
    String city;
    String street;
}

@Entity
public class Person {
    String name;
    @Embedded // it seems that it works even if this annotation is missing!?
    Address address;
}

生成始终相同的表

person
    name
    city
    street

即使我指定@Embedded.

我的配置:

  • JBoss EAP 6.4.0
  • hibernate-jpa-2.0-api-1.0.1.Final-redhat-3.jar

JPA 规范说:

http://docs.oracle.com/javaee/7/api/javax/persistence/Embedded.html

@javax.persistence.Embedded

指定实体的持久字段或属性,其值为可嵌入类的实例.embeddable 类必须标注为 Embeddable.

Specifies a persistent field or property of an entity whose value is an instance of an embeddable class. The embeddable class must be annotated as Embeddable.

http://docs.oracle.com/javaee/7/api/javax/persistence/Embeddable.html

@javax.persistence.Embeddable

指定一个类,其实例存储为拥有实体的固有部分并共享实体的标识.嵌入对象的每个持久属性或字段都映射到实体的数据库表.

Specifies a class whose instances are stored as an intrinsic part of an owning entity and share the identity of the entity. Each of the persistent properties or fields of the embedded object is mapped to the database table for the entity.

推荐答案

在使用 Hibernate 的情况下,是注释字段本身(如 @Embedded)还是注释引用的字段都没有关系类(如 @Embeddable).至少需要两者之一才能让 Hibernate 确定类型.

In case of using Hibernate it does not matter if you annotate the field itself (as @Embedded) or if you annotate the referenced class (as @Embeddable). At least one of both is needed to let Hibernate determine the type.

并且在 Hibernate 文档中有一个(隐式)声明,请看这里:http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/mapping.html#mapping-declaration-component

And there is a (implicit) statement about this inside the Hibernate documentation, take a look here: http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/mapping.html#mapping-declaration-component

它说:

Person 实体有两个组件属性,homeAddress 和出生在.homeAddress 属性没有被注释,但是Hibernate会通过查找来猜测它是一个持久化组件地址类中的@Embeddable 注解.

The Person entity has two component properties, homeAddress and bornIn. homeAddress property has not been annotated, but Hibernate will guess that it is a persistent component by looking for the @Embeddable annotation in the Address class.

这篇关于JPA @Embedded 注释是强制性的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆