-->
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.  [ 6 posts ] 
Author Message
 Post subject: hi , please .. can any one help me finding this error ?
PostPosted: Tue Feb 14, 2006 1:23 pm 
Newbie

Joined: Tue Feb 14, 2006 1:07 pm
Posts: 1
hi , i am trying to insert a record into mysql database through a simple javaprogramming using hiber nate ? I am able to insert 1 record , but i am trying to enter a second record i am getting the following error ???????


log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Inserting Record
Done
Hibernate: insert into CONTACT (FIRSTNAME, LASTNAME, EMAIL, ID) values (?, ?, ?, ?)
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:74)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:161)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:669)
at roseindia.tutorial.hibernate.FirstExample.main(FirstExample.java:36)
Caused by: java.sql.BatchUpdateException: Duplicate key or integrity constraint violation message from server: "Duplicate entry '3' for key 1"
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:1492)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:154)
... 6 more
Exception in thread "main"



here is my main java class code :::


package roseindia.tutorial.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


/**
* @author Deepak Kumar
*
* http://www.roseindia.net
* Hibernate example to inset data into Contact table
*/
public class FirstExample {
public static void main(String[] args) {
Session session = null;

try{
// This step will read hibernate.cfg.xml and prepare hibernate for use
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session =sessionFactory.openSession();
//Create new instance of Contact and set values in it by reading them from form object
System.out.println("Inserting Record");
Contact contact = new Contact();
contact.setId(3);
contact.setFirstName("Deepak");
contact.setLastName("Kumar");
contact.setEmail("deepak_38@yahoo.com");
session.save(contact);
System.out.println("Done");
}catch(Exception e){
System.out.println(e.getMessage());
}finally{
// Actual contact insertion will happen at this step
session.flush();
session.close();

}

}
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 2:11 pm 
Newbie

Joined: Tue Oct 11, 2005 10:57 am
Posts: 13
The problem is when you run de code for de second time and a key violation occurs. Each time that you run your code, you have change the line:

contact.setId(3);

to

contact.setId(4); // Second time you run the code

to

contact.setId(5); // Third time you run the code

and so on.

Reinaldo.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 2:46 pm 
Beginner
Beginner

Joined: Wed Feb 08, 2006 5:45 pm
Posts: 23
Location: Phoenix, AZ
Actually, don't set the id in the Object when you are inserting. You should set an id generator in your mapping file and then that will automatically generate an id for you (I recommend Sequence if you are just going in numerical order).

Are you trying to insert the same record twice? If you have a primary key constraint then that is why you are getting the error (as you cannot have duplicate primary keys).

Hope this helps,
-B


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 2:48 pm 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
also check out your mapping file, looking for the <id ...unsaved-value="null">.

perhaps you want hibernate to generate the key and as such you wouldn't have to specify the # for id.

example:
<id name="id" unsaved-value="null">
<generator class="hilo">
<param name="table">my_seq_table</param>
<param name="column">current_value</param>
</generator>
</id>

create a table named my_seq_table(current_value number);
and seed it with a 1, and then everytime you do a save hibernate will generate an ID for you. (and you should not explicitly set with the setId())...

If this helped please give out the credits... thx. I need 'em.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 14, 2006 2:50 pm 
Beginner
Beginner

Joined: Wed Feb 08, 2006 5:45 pm
Posts: 23
Location: Phoenix, AZ
oops, I hit the refresh button... :p


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 16, 2006 12:56 am 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
setty_4u -- did any of this help?

If so, please provide the credit for the answer...to the folks that helped.
(as you review the postings for this question there is a Y/N prompt after each to prompt you to post credit to the person who helped.)


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

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.