Play2 / JPA / Ebean OneToMany Mapping,使用复合PrimaryKey

时间:2014-04-30 08:44:57

标签: java hibernate jpa playframework-2.0 ebean

我尝试在复合键上创建ManyToOne relationShip,但是当我插入我的Pres2 bean时,我遇到了这个错误:

[error] Caused by: org.h2.jdbc.JdbcSQLException: Duplicate column name "CODE1"; SQL statement:
[error] insert into Pres2 (code1, code2, code3, champ1, code1) values (?,?,?,?,?) [42121-172]

我的专业版在Play 2.2.2上。

你能帮我解决一下我的问题吗?

我对Pres1的第一个Id类代码:

@Embeddable
public class ClePres1 implements Serializable {
  private static final long serialVersionUID = 1L;

  @Column(name = "code1")
  private String code1;

  @Override
  public int hashCode() {
     ...
  }
  @Override
  public boolean equals(Object obj) {
     ...
  }

我的bean Pres1代码:

@Entity
@Table(Name = "Pres1")
public class Pres1 extends Model {
   private static final long serialVersionUID = 1L;

   @EmbeddedId
   ClePres1 cle;

   @OneToMany(mappedBy = "pres1")
   private List<Pres2> listPres2;

   ...

我的第二个用于bean Pres2代码的Id类:

@Embeddable
public class ClePres2 implements Serializable {
   private static final long serialVersionUID = 1L;
   @Column(name = "code1")
   public String code1;

   @Column(name = "code2")
   public String code2;

   @Column(name = "code3")
   public String code3;

   @Override
   public int hashCode() {
   ...
   }

   @Override
   public boolean equals(Object obj) {
   ...
   }

我的Pres2 bean:

@Entity
@Table(name = "Pres2")
public class Pres2 extends Model {
   private static final long serialVersionUID = 1L;

   @EmbeddedId
   ClePres2 cle;

   @Column(name = "champ1")
   private String champ1;

   @ManyToOne
   @JoinColumns({ 
      @JoinColumn(name = "code1",referencedColumnName="code1")
   })
   private Pres1 pres1; 

1 个答案:

答案 0 :(得分:0)

您在code1嵌入类以及ClePres2类中配置了Pres2列,用于配置@ManyToOne关系。如果您从code1删除ClePres2,则会解决此问题,但如果您希望在code1的嵌入ID中使用Pres2,请将@ManyToOne关系配置如下以避免插入code1列两次。

   @ManyToOne
   @JoinColumns({ 
      @JoinColumn(name = "code1",referencedColumnName="code1",insertable = false, updatable = false)
   })
   private Pres1 pres1;