b]Hibernate version:[/b] 2.1
Mapping documents:
I have a table which is
create table A(
id NUMBER(19,0),
name VARCHAR2(255),
primary key (id)
)
and another table
create table B(
id NUMBER(19,0),
version NUMBER(10,0),
filedName VARCHAR2(255),
primary key(id, version)
)
table A's primary key is id, table A's primary key is (id, version)
mapping file:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.hibernate.A" table="A" dynamic-update="false" dynamic-insert="false" >
<id name="id" column="id" type="Integer" unsaved-value="null" >
<generator class="assigned">
</generator>
</id>
<property name="name" type="java.lang.String" update="true" insert="true" access="property" column="name" />
<set name="b" lazy="false" inverse="false" cascade="none" sort="unsorted" >
<key column="id" >
</key>
<one-to-many class="com.hibernate.B" />
</set>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.hibernate.B" table="B" dynamic-update="false" dynamic-insert="false" >
<composite-id name="id" class="com.hibernate.BKey" >
<key-property name="version" type="java.lang.Integer" column="version" />
<key-many-to-one name="id" class="com.hibernate.A" column="id" />
</composite-id>
<property name="filedName" type="java.lang.String" update="true" insert="true" access="property" column="filedName" />
<many-to-one name="a" class="com.hibernate.A" cascade="none" outer-join="auto" update="false" insert="false" access="property" column="id" not-null="true" />
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
I want to insert data into this parent table and child table
I have a class A, class B, composite key class BKey
A test = new A();
test.setName("test");
//id does not need to set here , right? as I have class to generate id for it
session.save(test);
???what should I do here with B if I want to insert data into table B
B test= new B();
SHould I need to initiate class "BKey" ?
could mapping file do it automatically? or I should manually set here?
I could not find the answer from previous post.
If anyone could give me a sample code of above should be appreciated.
the mapping file above posted is from a nice guy in this forum. But I do not know how to initiate the class and insert into table. could anyone help?
Full stack trace of any exception that occurs:
Name and version of the database you are using:
The generated SQL (show_sql=true):
Debug level Hibernate log excerpt:
|