Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

composite-id with foreign key

 
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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • 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);
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Al Capone" please re-read Ben's message.
 
He's my best friend. Not yours. Mine. You can have this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic