父和子表中的OpenJPA OneToMany和组合键

时间:2015-06-22 18:12:17

标签: java jpa jpa-2.0 openjpa

我有包含合成主键的表。

Server(key=ServerId)
ServerId|Name
1       |server1
2       |server2
ParentObj(key=ServerId+Code)
ServerId|Code |Title
1       |code1|value1
1       |code2|value2
2       |code1|Value2b
ChildObj(key=ServerId+Code+Name)
ServerId|Code |Name |Value
1       |code1|prop1|val1
1       |code1|prop2|val2
1       |code2|prop1|val1b
2       |code1|prop3|val3

这是我的Java bean。

@Entity @Table(name="ParentObj") @Access(AccessType.FIELD)
@IdClass(value=ParentObj.PK.class)
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@XmlRootElement
public class ParentObj {
   @Id private long serverId;
   @Id private String code;
   private String title;

   public long getServerId() { return serverId; }
   public String getCode() { return code; }
   public String getTitle() { return title; }

   public static class PK implements Serializable {
     private static final long serialVersionUID = 1L;       
     private long serverId;
     private String code;

     public long getServerId() { return serverId; }
     public void setServerId(long id) { serverId=id; }
     public String getCode() { return code; }
     public void setCode(String code) { this.code=code; }
  }
}

@Entity @Table(name="ChildObj") @Access(AccessType.FIELD)
@IdClass(value=ChildObj.PK.class)
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@XmlRootElement
public class ChildObj {
   @Id private long serverId;
   @Id private String code;
   @Id private String name;
   private String value;
   // public getter+setters for each field

   public static class PK implements Serializable {
     private static final long serialVersionUID = 1L;       
     private long serverId;
     private String code;
     private String name;

     public long getServerId() { return serverId; }
     public void setServerId(long id) { serverId=id; }
     public String getCode() { return code; }
     public void setCode(String code) { this.code=code; }
     public String getName() { return name; }
     public void setName(String name) { this.name=name; }
  }
}

我一直在尝试“一切”来创建OneToMany映射(ParentObj-> ChildObj),但似乎没有任何效果。我不需要ManyToOne(ParentObj< -ChildObj)链接,但如果必须定义那就没关系。

这是一个遗留数据库,因此我无法插入auto_increment标识列或在父级和子级之间创建额外的连接表。

这个注释在概念上是我想要的,但是OpenJPA2.x库不接受多个连接列。

// from parent to zero or more childs
@OneToMany(fetch=FetchType.LAZY)
@JoinColumns({
    @JoinColumn(name="server_id", referencedColumnName="server_id"),
    @JoinColumn(name="code", referencedColumnName="code")
})
private List<ChildObj> properties;

修改,回答 OneToMany,ManyToOne和EmbeddedId注释有效。我只是尝试阅读现有的行,但它现在很好。稍后我尝试更新+插入+删除任务。

public class ParentObj {
   @EmbeddedId ParentObj.PK pk;
   @OneToMany(fetch=FetchType.LAZY, cascade=CascadeType.ALL, mappedBy="parent", orphanRemoval=true)
   private List<ChildObj> childs;

   public PK getPK() { return pk; }
   public void setPK(PK pk) { this.pk=pk; }
   public List<ChildObj> getChilds() { return childs; }
   ...

   @Embeddable @Access(AccessType.FIELD)
   public static class PK implements Serializable {    
       private static final long serialVersionUID = 1L;     
       @Column(nullable=false) private long serverId;
       @Column(nullable=false) private String code;
       ..getters+setters+hashCode+equals functions
   }
}

public class ChildObj {
   @EmbeddedId ChildObj.PK pk;
   @ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.PERSIST, optional=false)
      @JoinColumns({
      @JoinColumn(name="serverid", referencedColumnName="serverid", nullable=false), 
      @JoinColumn(name="code", referencedColumnName="code", nullable=false)
   })   
   private ParentObj parent;

   public PK getPK() { return pk; }
   public void setPK(PK pk) { this.pk=pk; }
   public long getServerId() { return pk.getServerId(); }
   public String getCode() { return pk.getCode(); }
   public String getName() { return pk.getName(); }
   ...

   @Embeddable @Access(AccessType.FIELD)
   public static class PK implements Serializable {
      private static final long serialVersionUID = 1L;      
      @Column(nullable=false) private long serverId;
      @Column(nullable=false) private String code;
      @Column(nullable=false) private String name;
      ..getters+setters+hashCode+equals functions
   }
}

1 个答案:

答案 0 :(得分:1)

最简单的方法是创建从ChildObjParentObj的关联,类似于以下内容:

@ManyToOne(fetch = FetchType.LAZY, optional = true)
@JoinColumns({
    @JoinColumn(name = "serverId", referencedColumnName = "serverId"),
    @JoinColumn(name = "code", referencedColumnName = "code")})
private ParentObj parentObj;

然后在@OneToMany中定义ParentObj关联,如下所示:

@OneToMany(mappedBy = "parentObj", fetch=FetchType.LAZY)
private List<ChildObj> children;

我还建议您将复合键定义为@Embeddable类,在实体中用作@EmbeddedId引用。这些可嵌入的PK类应该是单独的类(而不是内部类),因为您将单独使用它们来查询相关的实体,serialisation of inner classes can cause problems