This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes how to insert Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "how to insert" Watch "how to insert" New topic
Author

how to insert

james edwin
Ranch Hand

Joined: Nov 22, 2001
Posts: 393
Hi,

I am new to hibernate and I want to use hibernate to insert the data in database(ORACLE).I have done some search on hibernate and i am able to store data and retrive data from one table.Now in my application we have three table

Table A , B , C

Relationship A - B(1-Many) - one record in A and many records in B for the same record

A - C( 1-1)

How do i use hibernate to insert records in all three tables ? at one go..

Regards,

James
[ September 05, 2004: Message edited by: james edwin ]

Regards,
James
Venkat dasari
Ranch Hand

Joined: Nov 12, 2003
Posts: 67
hi,
I am also new to hibernate..i am still trying to insert records into the database. will you please help me how to do it?
Moreover, how to retrieve values from two tables since it returns an object array...
for example i have a sample database emp and dept...
i want all the details of employees along with their location whose deptno=10...

please do help me regarding this..
Thanking you,
Venkat Dasari


Regards,<br />Venkat Dasari<br />SCJP 1.4, SCBCD.
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
hi James

you can set a "cascade" mode for each relation:
cascades

if you set the correct cascade mode, then hibernate will take care of storing/deleting your related objects.

pascal
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
hi Venkat

i think you better post a new question in a new topic.

it is hard to answer such a general question, perhaps you should first read one of the tutorials on hibernat, learn how to insert/query a single object and then do the next step and add relations to your objects.

As a hint for your problem: you can query for objects using HQL. HQL is similar to SQL.
if you have a specifi question then dont forget to post the code you have so far.

these links might be useful:
hibernate documentaion
external hibernate docs ant tutorials

pascal
james edwin
Ranch Hand

Joined: Nov 22, 2001
Posts: 393
Thanks will look at that.But while i am running the code for Relation A-C table which is having one-to-one relation.When i am trying to insert the record in both the tables,it giving me error...

HIBERNATE EXCEPTION No persister for: c

In Table A.hbm.xml i am an entry like

<one-to-one name="C" class="C"/>

Can someone put some light,why this error is coming !!!

Regards,

James
james edwin
Ranch Hand

Joined: Nov 22, 2001
Posts: 393
Hi All,

I am able to move ahead in coding and do mapping in XML part,But when i run the Code,i get the following error :-

HIBERNATE EXCEPTION Repeated column in mapping for class C should be mapped with insert="false" update="false": QUE_ID

Table A
-------------

Que_ID not null Varchar2(10) (PK)
CDate not null Date

Table C
------------
Que_ID not null Varchar2(10) (FK)
CID not null Varchar2(20)

Is it possible Que_ID name in both table is same ,that's why this error is coming .....

Help needed !!!

Regards,

James
[ September 06, 2004: Message edited by: james edwin ]
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
can you post the full mapping of C ?

how do you realize the one-to-one mapping (there are two ways, as stated in the hibernate docs) ?


pascal
james edwin
Ranch Hand

Joined: Nov 22, 2001
Posts: 393
Hi,

In A.hbm.xml i have an following entry

**********************
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="A" table="a">
<id name="aId" type="string" unsaved-value="null">
<column name="A_ID" sql-type="char(7)" not-null="true"/>
<generator class="assigned"/>
</id>
<property name="ADescX">
<column name="A_DESC_X" sql-type="char(40)" not-null="true"/>
</property>
<one-to-one name="aId" class="C"/>
</class>
</hibernate-mapping>
***********************************************

AND c.hbm.xml is as follows

***********************************************
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN" "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="C" table="c">

<id name="cId" type="string" unsaved-value="null">
<column name="c_ID" sql-type="char(7)" not-null="true"/>
<generator class="assigned"/>
</id>
<property name="mailLastSentD">
<column name="MAIL_LAST_SENT_D" sql-type="Date" not-null="true"/>
</property>
<one-to-one name="cId" class="A"/>
</class>
</hibernate-mapping>
************************************************


Regards,

James
james edwin
Ranch Hand

Joined: Nov 22, 2001
Posts: 393
After lot of trials !!! ONE-2-ONE PROBLEM IS SOLVED.....

BUT I am stuck up in one to many now....

trying hard on that too !!!
 
Don't get me started about those stupid light bulbs.
 
subject: how to insert
 
Similar Threads
Need Suggestion about Spring IoC, Hibernate Search, Hibernate Shard integration.. :D
How to rollback multiple prepared statements
Issue with Hibernate transaction in CMT
Selecting data from multiple tables
reading and writing large data(200000) from oracle