java - ManyToMany映射(辅助表)+额外字段+复合主键

标签 java hibernate spring spring-mvc

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

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

(由于大小未嵌入)

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

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

tbl_许可证:

@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_用户:

@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 然后我得到一个异常(exception): org.hibernate.AnnotationException:com.example.my.entities.license.LicenseUserAlertId 用作 @EmbeddedId 时不得具有 @Id 属性:com.example.my.entities.user.User.alerts.collection&&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;
  }
}

最佳答案

请看这篇文章

Mapping ManyToMany with composite Primary key and Annotation:

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

希望有帮助。

关于java - ManyToMany映射(辅助表)+额外字段+复合主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8989568/

相关文章:

java - JLabel 作为背景图像

java - 将全局变量传递给对象

java - 在 View 中创建简单的是/否对话框 (Android)

java - JSF 2.0 和 Hibernate 错误

java - 使用 Hibernate 排除值

spring - 事务服务中的 ConstraintViolationException 没有回滚

java - 前端和后端分别部署(域)的Cookies的使用

java - 不可编辑的 JEditorPane 使用 Enter 键

java - JpaRepository findAll() 正在获取嵌套对象

java - 绑定(bind)到线程 [main] 的键 [org.hibernate.internal.SessionFactoryImpl] 没有值