ManyToMany Mapping(帮助程序表)+额外字段+复合主键

时间:2012-01-24 15:38:02

标签: java hibernate spring spring-mvc

我正在尝试实现这个表结构:

http://www.img-teufel.de/uploads/Unbenannt1d4f8a349jpg.jpg

(由于尺寸而未嵌入)

它创建了我想要的类,除了tbl_license_user_alerts中没有复合主键。

那么,如何在我的案例中创建一个?

tbl_license:

@Entity
@Table( name = "tbl_license" )
public class License implements Serializable
{
  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue
  @Column( name = "id", nullable = false, columnDefinition = "serial" )
  private int id;

  @ElementCollection
  @JoinTable( name = "tbl_license_user_alert" )
  private List<LicenseUserAlert> alerts;

  public int getId()
  {
    return id;
  }

  public void setId( int id )
  {
    this.id = id;
  }

  public List<LicenseUserAlert> getAlerts()
  {
    return alerts;
  }

  public void setAlerts( List<LicenseUserAlert> alerts )
  {
    this.alerts = alerts;
  }
}

tbl_user:

@Entity
@Table( name = "tbl_user" )
public class User implements Serializable
{
  private static final long serialVersionUID = 1L;

  @Id
  @GeneratedValue
  @Column( name = "id", nullable = false, columnDefinition = "serial" )
  private int id;

  @ElementCollection
  @JoinTable( name = "tbl_license_user_alert" )
  private List<LicenseUserAlert> alerts;

  public int getId()
  {
    return id;
  }

  public void setId( int id )
  {
    this.id = id;
  }

  public List<LicenseUserAlert> getAlerts()
  {
    return alerts;
  }

  public void setAlerts( List<LicenseUserAlert> alerts )
  {
    this.alerts = alerts;
  }
}

tbl_license_user_alert:

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

  @ManyToOne
  @JoinColumn( name = "license_id"/*, columnDefinition = "int"*/ )
  private License license;

  @ManyToOne
  @JoinColumn( name = "user_id"/*, columnDefinition = "int"*/ )
  private User user;

  @Column( name = "timer", nullable = false, columnDefinition = "int default 86400" )
  private int timer = 86400

  public License getLicense()
  {
    return license;
  }

  public void setLicense( License license )
  {
    this.license = license;
  }

  public User getUser()
  {
    return user;
  }

  public void setUser( User user )
  {
    this.user = user;
  }

  public int getTimer()
  {
    return timer;
  }

  public void setTimer( int timer )
  {
    this.timer = timer;
  }
}

修改

@ danny.lesnik 然后我得到一个例外: org.hibernate.AnnotationException:com.example.my.entities.license.LicenseUserAlertId在用作@EmbeddedId时不得具有@Id属性:com.example.my.entities.user.User.alerts.collection&amp;&amp; element。 ID

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

  @EmbeddedId
  private LicenseUserAlertId id;

  @Column( name = "timer", nullable = false, columnDefinition = "int default 86400" )
  private int timer = 86400;

  public LicenseUserAlertId getId()
  {
    return id;
  }

  public void setId( LicenseUserAlertId id )
  {
    this.id = id;
  }

  public License getLicense()
  {
    return id.getLicense();
  }

  public void setLicense( License license )
  {
    id.setLicense( license );
  }

  public User getUser()
  {
    return id.getUser();
  }

  public void setUser( User user )
  {
    id.setUser( user );
  }

  public int getTimer()
  {
    return timer;
  }

  public void setTimer( int timer )
  {
    this.timer = timer;
  }
}

IdClass:

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

  @ManyToOne
  @JoinColumn( name = "license_id"/*, columnDefinition = "int"*/ )
  private License license;

  @ManyToOne
  @JoinColumn( name = "user_id"/*, columnDefinition = "int"*/ )
  private User user;

  public License getLicense()
  {
    return license;
  }

  public void setLicense( License license )
  {
    this.license = license;
  }

  public User getUser()
  {
    return user;
  }

  public void setUser( User user )
  {
    this.user = user;
  }
}

1 个答案:

答案 0 :(得分:3)

请看这篇文章

Mapping ManyToMany with composite Primary key and Annotation:

它有完整的示例如何映射具有复合主键的表。

希望它有所帮助。