• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

composite-id with foreign key

 
angelo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,

i have an entity mapping a table with a composite primary key: one of it's field is a foreign key who references another table's primary key.
I'm using jpa annotations, hibernate3, weblogic 9.2.
I would like use a generator to populate the foreign key,
i know how to do this for an entity with a simple primary key, but not with a composite key, i'm trying to do this with EmbeddedId and IdClass without any success (oracle says: cannot insert NULL into... )

here is entity (simplified) with IdClass:

@IdClass(EntityPK.class)
@Entity()
@Table(name="A_TABLE")
public class Entity implements Serializable {
private java.sql.Timestamp DDate;
private double qty;
private Objquantity objquantity;
private Long id;
private Timestamp DDate;

@Id
public Long getId() {
return this.id;
}
@Id
public java.sql.Timestamp getDDate() {
return this.DDate;
}
//many-to-one relationship
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="P_QTYID",referencedColumnName="P_QTYID",nullable=false, insertable=false, updatable=false)
public Objquantity getObjquantity () {
return this.objquantity ;
}

The PK Class:
public class EntityPK implements Serializable {
private Long id;
private Timestamp DDate;

@GenericGenerator(
name="idPKGenerator",
strategy="foreign",
parameters={
@Parameter(name="property", value="objquantity")
}
)
@Id
@GeneratedValue(generator="idPKGenerator")
@Column(name="ID", nullable=false, precision=22, insertable=false,updatable=false)
public Long getId() {
return this.id;
}
@Id
@Column(name="D_DATE", nullable=false, length=7)
public java.sql.Timestamp getDDate() {
return this.DDate;
}

Any suggest.

thanks in advance.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"angelo",
Please check your private messages regarding an important administrative matter.
-Ben
 
angelo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i've found a solution (now in production working enviroment) and i try to explain with example:

if you have a table "A" with primary key "id",

and a table "B" with a composite primary key ("id","date") and B.id is foreign key to A.id,

then you can define B entity like:

@IdClass(BPK.class)
@Entity()
@Table(name=B)
public class B{
private A a;
private Date date;

@Id
public Date getDate() {
return date;
}
@Id
public A getA(){
return A;
}
}

then you define the primary key class:

@Embeddable
public class BPK{
private Date date;
private A a;

@Id
@Column(name="DATE", nullable=false)
public Date getDate() {
return this.date;
}

@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name="id", referencedColumnName="id", nullable=false, insertable=false, updatable=false)
public A getA() {
return this.a
}
}

Now, If A has an autogenerated id then A aInstance= new A();
//set some properties on aInstance but not the id
B bInstance= new B();
//set some properties on bInstance and then set releationship:
bInstance.setA(aInstance); // this makes the trick
em.persist(aInstance);
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Al Capone" please re-read Ben's message.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic