Hi all,
i'm a beginner in hibernate, i try to write some code about hibernate one to many mapping but i get error like this :
Code:
Not binding factory to JNDI, no JNDI name configured
org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.hibernate.model.Story#0]
at org.hibernate.engine.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:590)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:284)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:223)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:89)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:70)
at org.hibernate.impl.SessionImpl.fireSaveOrUpdate(SessionImpl.java:507)
at org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:499)
at org.hibernate.engine.CascadingAction$5.cascade(CascadingAction.java:218)
at org.hibernate.engine.Cascade.cascadeToOne(Cascade.java:268)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:216)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascadeCollectionElements(Cascade.java:296)
at org.hibernate.engine.Cascade.cascadeCollection(Cascade.java:242)
at org.hibernate.engine.Cascade.cascadeAssociation(Cascade.java:219)
at org.hibernate.engine.Cascade.cascadeProperty(Cascade.java:169)
at org.hibernate.engine.Cascade.cascade(Cascade.java:130)
at org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:456)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:334)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:181)
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)
i already search in google but i don't find the answer, would any body help me?
this is my code :
Group.java
Code:
package com.hibernate.model;
import java.util.List;
public class Group
{
private int id;
private String name;
private List stories;
public Group()
{
}
public Group(String name)
{
this.name = name;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public List getStories()
{
return stories;
}
public void setStories(List stories) {
this.stories = stories;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Group other = (Group) obj;
if (this.id != other.id) {
return false;
}
if ((this.name == null) ? (other.name != null) : !this.name.equals(other.name)) {
return false;
}
if (this.stories != other.stories && (this.stories == null || !this.stories.equals(other.stories))) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 7;
hash = 59 * hash + this.id;
hash = 59 * hash + (this.name != null ? this.name.hashCode() : 0);
hash = 59 * hash + (this.stories != null ? this.stories.hashCode() : 0);
return hash;
}
}
Story.java
Code:
package com.hibernate.model;
public class Story
{
private int id;
private String info;
public Story()
{
}
public Story(String info)
{
this.info = info;
}
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getInfo()
{
return info;
}
public void setInfo(String info)
{
this.info = info;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Story other = (Story) obj;
if (this.id != other.id) {
return false;
}
if ((this.info == null) ? (other.info != null) : !this.info.equals(other.info)) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 7;
hash = 97 * hash + this.id;
hash = 97 * hash + (this.info != null ? this.info.hashCode() : 0);
return hash;
}
}
Group.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.hibernate.model.Group" table="grouptable">
<id name="id" type="integer" unsaved-value="null" column="id">
<generator class="increment" />
</id>
<list name="stories" cascade="save-update">
<key column="parent_id" />
<index column="idx" />
<one-to-many class="com.hibernate.model.Story" />
</list>
<property name="name" type="string" column="name"/>
</class>
</hibernate-mapping>
Story.hbm.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.hibernate.model.Story" table="story">
<id name="id" type="integer" unsaved-value="null" column="id">
<generator class="increment" />
</id>
<property name="info" type="string" column="info"/>
</class>
</hibernate-mapping>
HibernateUtil.java
Code:
package com.hibernate.session;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
public class HibernateUtil
{
private static SessionFactory sessionFactory;
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
static
{
try
{
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
}
catch (Throwable ex)
{
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static Session getSession() throws HibernateException
{
Session session = (Session) threadLocal.get();
if (session == null || !session.isOpen())
{
if (sessionFactory == null)
{
rebuildSessionFactory();
}
session = (sessionFactory != null) ? sessionFactory.openSession(): null;
threadLocal.set(session);
}
return session;
}
public static void rebuildSessionFactory()
{
try
{
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
}
catch (Exception e)
{
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
public static void closeSession() throws HibernateException
{
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null)
{
session.close();
}
}
public static org.hibernate.SessionFactory getSessionFactory()
{
return sessionFactory;
}
}
Hibernate.cfg.xml
Code:
<?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.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/learning_java</property>
<property name="hibernate.connection.username">root</property>
<mapping resource="com/hibernate/model/Group.hbm.xml"/>
<mapping resource="com/hibernate/model/Story.hbm.xml"/>
</session-factory>
</hibernate-configuration>