java - JPA @Embedded 注释是强制性的吗?

标签 java jakarta-ee jpa annotations

我已尝试省略 @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/

相关文章:

Java程序运行但不在bash脚本中

java - 如何在zk中创建从右到左的选项卡框?

java - Hibernate一级缓存丢失

java - Spring JPA/Hibernate EmptyInterceptor 不注入(inject) Entitymanager/Spring beans

java - 如何在 Jaspersoft Studio 中启动并运行 java 代码?

java - 创建新文件 - 获取 IOException

Java - 尝试使用 10 次抛硬币的汇总结果进行抛硬币脚本

java - 如何比较两个包含 Java 中同一类对象的 Arraylists?

java - 使用键/值对创建对象

使用 EclipseLink 的远程客户端的 JPA 延迟加载策略