-->
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.  [ 3 posts ] 
Author Message
 Post subject: compositekey as a foreignkey... ConstraintViolationException
PostPosted: Tue Sep 05, 2006 2:06 am 
Newbie

Joined: Tue Sep 05, 2006 12:23 am
Posts: 2
Location: india,Delhi
I am new user of Hibernate.
I want to map composite key of one table to as a foreign key in other table.
I received an error when running bo(bussiness method)
org.hibernate.exception.ConstraintViolationException

Please give any response as soon as possible.

Table Script , hbm.xml, bo code are as follow :

-------------------------------------------------------------------------------------
Table Script
-------------------------------------------------------------------------------------
create table TABLE_A (
ID bigint not null,
C_ID bigint not null,
DA varchar(255) not null,

primary key (ID, C_ID, DA)
);

create table TABLE_B (
R_ID bigint not null,
C_P_ID bigint not null,
ADA varchar(255) not null,

ID bigint not null,
C_ID bigint not null,
DA varchar(255) not null,

INDEX (ID,C_ID,DA),
FOREIGN KEY (ID,C_ID,DA) REFERENCES
TABLE_A(ID,C_ID,DA),

primary key (R_ID)
);

-------------------------------------------------------------------------------------
A.hbm.xml
-------------------------------------------------------------------------------------

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping>
<class
name="A"
table="TABLE_A" >

<meta....../meta>
<composite-id>
<key-property name="id" type="java.lang.Long" column="ID" />
<key-property name="cId"
type="java.lang.Long" column="C_ID" />
<key-property name="da"
type="java.lang.String" column="DA" />
</composite-id>

<set name="bs" lazy="true"
inverse="true"
cascade="none" >
<key >
<column name="ID" />
<column name="C_ID" />
<column name="DA" />
</key>

<one-to-many class="B"/>
</set>

</class>
</hibernate-mapping>

-------------------------------------------------------------------------------------
B.hbm.xml
-------------------------------------------------------------------------------------

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"

"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >

<hibernate-mapping>
<class
name="B"
table="TABLE_B"
>
.../meta>

<id
name="rId"
type="java.lang.Long"
column="R_ID"
>

<generator class="native" />
</id>

<property
name="cPId"
type="java.lang.Long"
column="C_P_ID"
not-null="true"
length="1"
/>

<property
name="aDA"
type="java.lang.String"
column="ADA"
not-null="true"
length="1"
/>

<property
name="id"
type="java.lang.Long"
column="ID"
not-null="true"
length="1"
insert="false"
update="false"
/>

<property
name="cId"
type="java.lang.Long"
column="C_ID"
not-null="true"
length="1"
insert="false"
update="false"
/>

<property
name="da"
type="java.lang.String"
column="DA"
not-null="true"
length="1"
insert="false"
update="false"
/>

<many-to-one name="a"
class="A"
not-null="true" insert="false" update="false"
>
<column name="ID" />
<column name="C_ID" />
<column name="DA" />
</many-to-one>


</class>
</hibernate-mapping>

-------------------------------------------------------------------------------------
Business Method
-------------------------------------------------------------------------------------


Session session = getSession();
try {
//set values in object of A Class
session.save(a);//save the Object of A Class
commit();
}
catch(Exception e ) {
log.error(e);
rollback();
}
session = null;
session = getSession();
try {
//set values in object of B Class
session.save(pccrl);//save the Object of B Class(error come on this line) commit();
}
catch(Exception e) {
log.error(e);
rollback();
}


Top
 Profile  
 
 Post subject: RE:compositekey as a foreignkey... ConstraintViolationExcept
PostPosted: Tue Sep 05, 2006 2:59 am 
Beginner
Beginner

Joined: Mon Nov 22, 2004 11:21 am
Posts: 42
Please post your stack trace. Another issue: you should close the session, not just setting it to null. But this doesn't solve your problem.

_________________
Real programmers confuse Halloween and Christmas because OCT 31 = DEC 25


Top
 Profile  
 
 Post subject: thanks for reply
PostPosted: Tue Sep 05, 2006 6:00 am 
Newbie

Joined: Tue Sep 05, 2006 12:23 am
Posts: 2
Location: india,Delhi
sorry i forget to write that after first value save to the session i used session.close()

thanks for quick reply
my problem was solved now.
I making two error in above code
first is in creation of second table i am not using auto_incriment for primary key

Second In bo when i am saving object for second table(B) then i am setting wrong value in on of the foreign key column.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.