-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Multiple composite primary keys and foreign keys
PostPosted: Tue Jun 07, 2011 2:25 am 
Newbie

Joined: Tue Jun 07, 2011 1:53 am
Posts: 2
Hi,

I have a scenario, my table A has got composite primary keys (Id (autonumber using oracle sequence), db_timestamp (timestamp)) and table B has also got composite primary key (Id (autonumber using oracle sequence), db_timestamp (timestamp)) and table B has also got db_timestamp as a foreign key to table A, as well A_Id as foreign key to table A's Id.

SQL script looks like:
CREATE TABLE A
(
"A_ID" NUMBER,
"MESSAGE_GUID" VARCHAR2(64 BYTE),
...
"DB_TIMESTAMP" TIMESTAMP (6),
...
CONSTRAINT "PK_SOM_AUDIT" PRIMARY KEY ("A_ID", "DB_TIMESTAMP")
)

CREATE TABLE "AUDITRPT_NEW"."SOM_AUDIT_EXTENSION"
(
"B_ID" NUMBER NOT NULL ENABLE,
"A_ID" NUMBER NOT NULL ENABLE,
...
"C_ID" NUMBER NOT NULL ENABLE,
"VALUE" VARCHAR2(500 BYTE),
"DB_TIMESTAMP" TIMESTAMP (6),
CONSTRAINT "B_PK" PRIMARY KEY ("B_ID", "DB_TIMESTAMP") ENABLE,
CONSTRAINT "B_FK" FOREIGN KEY ("A_ID", "DB_TIMESTAMP") REFERENCES "A" ("A_ID", "DB_TIMESTAMP") ENABLE,
CONSTRAINT "B_FK2" FOREIGN KEY ("C_ID") REFERENCES "C_KEY" ("C_ID") ENABLE
)

I created @Entity A, @Entity B and have APK as @Embeddable class implementing Serializable and have BPK as @Embeddable class implementing Serializable as well. Also as part of class B I added following code.

@Entity
@Table(name = "B")
public class B implements Serializable{
private static final long serialVersionUID = 1L;
private A a;
private C c;
private String value;
private BPK bPK;


@Id
public BPK getBPK() {
return bPK;
}
@ManyToOne
@JoinColumns ({
@JoinColumn(name = "A_ID",referencedColumnName="A_ID" ),
@JoinColumn(name = "DB_TIMESTAMP",referencedColumnName="DB_TIMESTAMP" )})
public A getA() {
return a;
}
...
...}
Class A looks like:

@Entity
@Table(name = "A")
public class SomAudit implements Serializable{
private static final long serialVersionUID = 1L;
private APK aPK;
........
@Id
public APK getAPK() {
return aPK;
}
...}

PK classes are:
@Embeddable
public class APK implements Serializable {

private static final long serialVersionUID = 1L;
private Integer aId;
private Date dbTimeStamp;
public APK (Integer aId, Date dbTimeStamp) {
super();
this.aId = aId;
this.dbTimeStamp = dbTimeStamp;
}

public APK () {

}
@GeneratedValue(strategy = GenerationType.AUTO, generator = "SOMASEQ")
@SequenceGenerator(name = "SOMASEQ", sequenceName = "SOM_A_SEQ", catalog = "SOM_A_SEQ")
@Column(name = "A_ID", insertable=false, updatable=false)
public Integer getAId() {
return aId;
}

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "DB_TIMESTAMP")
public Date getDbTimeStamp() {
return dbTimeStamp;
}
...... SETTERS
}
BPK class
@Embeddable
public class BPK implements Serializable {

private static final long serialVersionUID = 1L;
private Integer bId;
private Date dbTimeStamp;
public BPK (Integer bId, Date dbTimeStamp) {
super();
this.bId = bId;
this.dbTimeStamp = dbTimeStamp;
}

public BPK () {

}
@GeneratedValue(strategy = GenerationType.AUTO, generator = "SOMBSEQ")
@SequenceGenerator(name = "SOMBSEQ", sequenceName = "SOM_B_SEQ", catalog = "SOM_B_SEQ")
@Column(name = "B_ID", insertable=false, updatable=false)
public Integer getBId() {
return bId;
}

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "DB_TIMESTAMP")
public Date getDbTimeStamp() {
return dbTimeStamp;
}
...... SETTERS
}

Now when I try to run the program, on my initialisation
AnnotationConfiguration config = new AnnotationConfiguration();
config.configure("hibernate.cfg.xml");
SessionFactory factory = config.buildSessionFactory();

I get following error:

Exception in thread "main" java.lang.ExceptionInInitializerError
at au.gov.asic.etl.auditing.App.<init>(App.java:71)
at au.gov.asic.etl.auditing.App.main(App.java:82)
Caused by: java.lang.NullPointerException
at au.gov.asic.etl.auditing.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:44)
at au.gov.asic.etl.auditing.util.HibernateUtil.<clinit>(HibernateUtil.java:17)
... 2 more

Please advise or refer me to similar scenario where we have composite primary key being referred in another table and half of the key is part of composite primary key in 2nd table.


Top
 Profile  
 
 Post subject: Re: Multiple composite primary keys and foreign keys
PostPosted: Wed Jun 08, 2011 12:50 am 
Newbie

Joined: Tue Jun 07, 2011 1:53 am
Posts: 2
The issue turn to be duplication of DB_TIMESTAMP.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.