2

I'm trying to realise this table structure:

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

(not embedded due to size)

It creates my classes how i want, except there's no composite primary key in tbl_license_user_alerts.

So, how to create one in my case?

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;
  }
}

EDIT:

@danny.lesnik Then I'm getting an exception: org.hibernate.AnnotationException: com.example.my.entities.license.LicenseUserAlertId must not have @Id properties when used as an @EmbeddedId: 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;
  }
}
dtrunk
  • 4,685
  • 17
  • 65
  • 109

1 Answers1

3

Please see this post

Mapping ManyToMany with composite Primary key and Annotation:

It has full example how to map tables with composite primary keys.

Hope it helps.

Community
  • 1
  • 1
danny.lesnik
  • 18,479
  • 29
  • 135
  • 200
  • i've edited the post above because i can't answer my own question and i needed to post my updated code – dtrunk Jan 24 '12 at 16:15
  • weel, I don't know what the problem is, try to run my code and verify that it's running and then just try to modify it.By the way, I don't see any @transient annotation where it supposed to be. anyway just run my code. – danny.lesnik Jan 24 '12 at 17:09
  • thanks. under pressure of time i can't work :-) did exactly the same you did and it worked. – dtrunk Jan 25 '12 at 07:19