Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to insert using hibernate?

 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 122
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops!!! I missed that. Thanks Hemant.
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 122
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again Hemant. Just for an instance I coded like that.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic