[英]How to persist JPA Map @OneToMany
我有一个包含地图的实体类。 该地图引用了Child类型的排名实体。
@Entity
public class Parent implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@OneToMany
@JoinTable
private Map<String, Child> ranking;
}
@Entity
public class Child implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
}
我将对象放入地图中,如下所示:
ranking.put(1, childA);
ranking.put(2, childB);
ranking.put(3, childA);
Child类不引用Parent。 映射键是我要保留的排名属性。 我不希望rank属性成为Child类的一部分。 我想实现一个连接表(parent_child),其中的列如下所示:
|-------------| |----------| |---------------|
| parent | | child | | parent_child |
|-------------| |----------| |---------------|
| id | | id | | parent_id |
| ... | | ... | | child_id |
| | | | | ranking | <-- missing
|-------------| |----------| |---------------|
实际上,我没有获得等级列。 我想念什么? 这有可能吗?
如上文所述,您将需要创建一个额外的实体,例如Relationship。 然后,您可以将其映射为Map,例如:
@Entity
public class Parent implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
@OneToMany(mappedBy ="parent")
@JoinColumn(name = "parent_id")
@MapKeyColumn(name = "ranking")
private Map<String, Relationship> ranking;
}
您不应该使用地图,因为这个问题,您应该为您的parent_child
创建一个实体,并创建@NamedQuery
以返回带有孩子的排名
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.