我已尝试省略 @Embedded
注释,但字段仍已嵌入表中。我找不到任何可以说明 @Embedded
注释是可选的内容。
是还是不是可选的?
下面的代码
@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
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 确定类型。
在 Hibernate 文档中有一个关于此的(隐含的)声明,请看这里: http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/mapping.html#mapping-declaration-component
它说:
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.
关于java - JPA @Embedded 注释是强制性的吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31157700/