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

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

问题描述

我曾尝试漏报 @Embedded 标注,仍然领域已经嵌入在表中。我找不到任何事说, @Embedded 注释是可选的。

它是是不是可选?

以下code

  @Embeddable
公共类地址{
    串城市;
    串街;
}@实体
公共类Person {
    字符串名称;
    @Embedded //看来,它的作品即使这个注释丢失!?
    地址地址;
}

总是产生相同的表

 
    名称
    市
    街

就算我做的的指定 @Embedded


我的配置:


  • 的JBoss EAP 6.4.0

  • 休眠-JPA-2.0-API 1.0.1.Final-的redhat-3.jar


JPA规范说:

<一个href=\"http://docs.oracle.com/javaee/7/api/javax/persistence/Embedded.html\">http://docs.oracle.com/javaee/7/api/javax/persistence/Embedded.html


  

@ javax.persistence.Embedded


  
  

指定其值是一个可嵌入类的实例实体的持久字段或属性。可嵌入类必须标注为嵌入式。


<一个href=\"http://docs.oracle.com/javaee/7/api/javax/persistence/Embeddable.html\">http://docs.oracle.com/javaee/7/api/javax/persistence/Embeddable.html


  

@ javax.persistence.Embeddable


  
  

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



解决方案

嵌入式嵌入式不是强制性的,但它给你的实体的关系不错的面向对象的视角。另一种方式做这样的事情 - 就是用一对一映射。但在这种情况下的实体将被写入到单独的表(而在嵌入式它可以在自己的数据库被写入到单独的表的情况下)。

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.

Is it or is it not optional?

The following code

@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;
}

generates always the same table

person
    name
    city
    street

even if I do not specify @Embedded.


My configuration:

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

The JPA specification says:

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.

解决方案

Embedded-Embeddable is not mandatory, but it gives you nice OOP perspective of your entities' relationship. Another way to do such a thing - is to use OneToOne mapping. But in such a case entity WILL be written to separate table (while in case of embedded it CAN be written to the separate table in your DB).

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

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