-->
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.  [ 1 post ] 
Author Message
 Post subject: how to insert new data into these 2 tables
PostPosted: Wed Jan 19, 2005 9:36 am 
Newbie

Joined: Mon Jan 17, 2005 9:05 am
Posts: 17
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:


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

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.