File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes How to insert using hibernate? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "How to insert using hibernate?" Watch "How to insert using hibernate?" New topic
Author

How to insert using hibernate?

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Hi all, from the past 2 days I have been learning and searching how to insert records into database using hibernate.

Suppose that there is a table

=====================
AUTHOR
=====================
aid long pri key
name varchar
=====================

Suppose it already consists a record

============================
12245 Herbert Schildt
============================

Suppose there is another table named BOOKS which refers to AUTHOR

======================
BOOKS
======================
bid long pri key
name varchar
aid long mul key(refers aid in AUTHOR)
======================

I want to insert a record into table BOOKS like this

===================================
12234 Java Complete Reference 12245
===================================

Could anyone tell me how to insert a record which has foreign key constraint?

Thank you all in advance.


Love all, trust a few, do wrong to none.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1506
Have you tried this at all, and if so, are you getting an error. This seems pretty straight forward. As long as the tables and their relationships are defined correctly.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Hi Boson, actually I tried it, got some errors, finally got the output but not efficient way of using hibernate. Actually I posted it in this forum, due to the lengthy post no one was replying, so I posted this thread.

Anyhow just now one of my friend gave me an idea, I am working on it now. I will post it when I am done with it.

Thank you dude.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

This is how I inserted the row in child table.

hibernate.cfg.xmlAuthor.hbm.xmlAuthor.javaBook.javaExample1.javaExample2.javaI followed this approach. Is this right?
Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
Hi chaitanya ,

your hibernate configuration files looks fine to me but with just one recommendation .

Use "com.mysql.jdbc.Driver" MYSQL driver instead of org.gjt.mm.mysql .

org.gjt.mm.mysql driver is an old third party jar latest version 2.0.14 was released on 05-19-02.

references:
MySQL DBCP Example
http://mmmysql.sourceforge.net/.

anyway internally org.gjt.mm.mysql extends "com.mysql.jdbc.Driver"
reference :
com.mysql.jdbc.Driver & org.gjt.mm.mysql.Driver.

but its still recommended to use "com.mysql.jdbc.Driver" driver class.


Hope this help.
Hemant
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Oops!!! I missed that. Thanks Hemant.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

@Hemant: What about the Example2.java program? Is it the right approach, getting an entire record from database and inserting a new record based on it?
Hemant Thard
Ranch Hand

Joined: Dec 23, 2008
Posts: 122
Hi chaitanya,

based on the way you have retreived the object, it seems to me that there is only one Author with name "Herbert Schildt"


Query query=session.createQuery("select id from Author where name='Herbert Schildt'");
for(@SuppressWarnings("rawtypes")
Iterator it=query.iterate();it.hasNext();)
id=Long.parseLong(it.next().toString());


if so is the case, i would recommend this approach for author retrival,



Hope this helps,
Hemant
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Thanks again Hemant. Just for an instance I coded like that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to insert using hibernate?
 
Similar Threads
Mapping a link DB table in hibernate
relation without ID attribute
Is my approach right while inserting a foreign key?
how to interpret this relation(optional one to one) in Hibernate
Check Table's Relationships