2017-03-06 35 views
0

我有一个父实体,像这样:商店相关JPA实体JSON列父实体

@Entity 
public class Parent implements Serializable { 
    @Id 
    private String id; 

    @Convert(converter = ChildConverter.class) 
    private Collection<Child> children; 
    ... 
} 

和子实体的定义,像这样:

@Entity 
public class Child implements Serializable { 
    @Id 
    private String id; 
    ... 
} 

不过,我想我的Child实体将保存在其自己的表中,但作为Parent中的(JSON)列。我想这样做的原因是因为Child实体包含的数据很少,我感觉好像它不保证它自己的表格。 JPA有可能吗?

更新

最后,我决定根本就没有与@Entity注释Child类。但这带来的麻烦是我必须在没有JPA的帮助下执行实体约束。

+0

如果集合是一个实体,那么这需要是一个关系,所以'@ OneToMany' /'@ ManyToMany'。如果元素类型不是实体(例如字符串,日期等),则只能使用AttributeConverter –

回答

0

您仍然需要一张表来存储父母的孩子,因为您在父类中使用了一个集合。

您可以在Parent类中的Child集合上使用@Embeddable注释对Child类和@ElementCollection和@CollectionTable。

@Embeddable 
public class Child implements Serializable { 
    @Id 
    private String id; 
    ... 
} 
@Entity 
public class Parent implements Serializable { 
    @Id 
    private String id; 

    @Convert(converter = ChildConverter.class) 
    @ElementCollection 
    @CollectionTable(
     name="CHILD", 
     [email protected](name="PARENT_ID") 
    private Collection<Child> children; 
    ... 
} 
+0

啊。但是这仍然会给我留下一张“儿童”物品的表格,对吧? –

+0

是的,你是对的。 –