aspose file tools*
The moose likes Object Relational Mapping and the fly likes org.hibernate.NonUniqueObjectException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "org.hibernate.NonUniqueObjectException" Watch "org.hibernate.NonUniqueObjectException" New topic
Author

org.hibernate.NonUniqueObjectException

Ranendra Kumar
Greenhorn

Joined: Sep 27, 2012
Posts: 5
please try to solve my errors in below code.if required some more codes which i wrote please tell me to paste.Thanks

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
0
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.jlcindia.hibernate.CurrentStudent#0]
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:168)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:187)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:172)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:535)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:523)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:519)
at com.jlcindia.hibernate.ALab111Client.main(ALab111Client.java:17)


Actually i have written a small application of TABLE_PER_CLASS using annotations.My persistence class as follows:
package com.jlcindia.hibernate;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;


@Entity
@Table(name="jlcstudents")
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class Student {

@Id
@Column(name="sid")
private int sid;

@Column(name="sname")
private String sname;

@Column(name="city")
private String city;

@Column(name="status")
private String status;

@Column(name="totalfee")
private double totalfee;
//constructors
//setters and getters
}
below is my code of creating related class objects:

public class ALab111Client {
public static void main(String as[]){
Transaction tx=null;
try{
SessionFactory factory =AHibernateUtil.getSessionFactory();
Session session=factory.openSession();
tx=session.beginTransaction();
// adding the student
Student stu=new Student("sri","Blore","enabled",15000.00);
Integer it=(Integer)session.save(stu);
System.out.println(it.intValue());

//2.adding the current status
CurrentStudent cstu=new CurrentStudent("vas","Bangalore","Enabled",15000.00,2000.00,"6:30Pm","Mathekere");
it =(Integer)session.save(cstu);
System.out.println(it.intValue());

//3.adding the oldstudent
OldStudent ostu= new OldStudent("vas","blore","Enabled",15000.00,"sdsoft","aa@sd.com",9.0);
it =(Integer)session.save(ostu);
System.out.println(it.intValue());


//4. adding the Regular Student
RegularStudent rstu=new RegularStudent("vas","blore","Enabled",15000.00,2000.00,"6.30Pm","Mathikere","Msc","85.5",3);
it =(Integer)session.save(rstu);
System.out.println(it.intValue());



//5. adding the weekend Student
WeekendStudent wstu=new WeekendStudent("vas","blore","Enabled",15000.00,2000.00,"6.30Pm","Mathikere","sdsoft","aa@sd.com",9.0);
it =(Integer)session.save(wstu);
System.out.println(it.intValue());

tx.commit();
session.close();

}catch(Exception e){
e.printStackTrace();
if(tx!=null)
tx.rollback();
}

}
}
---------------------------------------------------
my cfg file contains following code for required mapping
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>

<session-factory>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">altruist</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/jlcdb1</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="myeclipse.connection.profile">mysqlDriver</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>

<property name="hbm2ddl.auto"> create</property>
<mapping class="com.jlcindia.hibernate.Student"/>
<mapping class="com.jlcindia.hibernate.OldStudent"/>
<mapping class="com.jlcindia.hibernate.CurrentStudent"/>
<mapping class="com.jlcindia.hibernate.WeekendStudent"/>
<mapping class="com.jlcindia.hibernate.RegularStudent"/>


</session-factory>

</hibernate-configuration>
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: org.hibernate.NonUniqueObjectException