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