-->
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.  [ 21 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: A question about net.sf.hibernate.NonUniqueObjectException:
PostPosted: Sat Sep 17, 2005 2:21 pm 
Newbie

Joined: Sat Sep 17, 2005 2:15 pm
Posts: 9
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

As I complie a java file I got the following error message:

net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 0, of class: com.oreilly.hh.Track

How can I overcome this problem?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 17, 2005 2:22 pm 
Newbie

Joined: Sat Sep 17, 2005 2:15 pm
Posts: 9
Sorry, here is my question again:

As I complie a java file I got the following error message:

net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 0, of class: com.oreilly.hh.Track

How can I overcome this problem?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 17, 2005 2:29 pm 
Senior
Senior

Joined: Thu Aug 04, 2005 4:54 am
Posts: 153
Location: Birmingham, UK
1) This is not a compilation error.

2) Judging by the package you're trying an exanple from an O'Reilly book. Please post the exception generated, the code between session open and close etc.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 17, 2005 2:41 pm 
Newbie

Joined: Sat Sep 17, 2005 2:15 pm
Posts: 9
You are right. I am trying the example in book "Hibernate - A developer's notebook".

The exception is posted in my question.

Here is the code where the session is opened and closed:

public class CreateTest {

public static void main(String args[]) throws Exception {
// Create a configuration based on the properties file we've put
// in the standard place.
Configuration config = new Configuration();

// Tell it about the classes we want mapped, taking advantage of
// the way we've named their mapping documents.
config.addClass(Track.class);

// Get the session factory we can use for persistence
SessionFactory sessionFactory = config.buildSessionFactory();

// Ask for a session using the JDBC information we've configured
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
// Create some data and persist it
tx = session.beginTransaction();

Track track = new Track("Russian Trance",
"vol2/album610/track02.mp3",
Time.valueOf("00:03:30"), new Date(),
(short)0);
session.save(track);

track = new Track("Video Killed the Radio Star",
"vol2/album611/track12.mp3",
Time.valueOf("00:03:49"), new Date(),
(short)0);
session.save(track);


track = new Track("Gravity's Angel",
"vol2/album175/track03.mp3",
Time.valueOf("00:06:06"), new Date(),
(short)0);
session.save(track);

// We're done; make our changes permanent
tx.commit();

} catch (Exception e) {
if (tx != null) {
// Something went wrong; discard all partial changes
tx.rollback();
}
throw e;
} finally {
// No matter what, close the session
session.close();
}

// Clean up after ourselves
sessionFactory.close();
}
}

And here is another code:

public class Track implements Serializable {

/** identifier field */
private Integer id;

/** persistent field */
private String title;

/** persistent field */
private String filePath;

/** nullable persistent field */
private Date playTime;

/** nullable persistent field */
private Date added;

/** persistent field */
private short volume;

/** full constructor */
public Track(String title, String filePath, Date playTime, Date added, short volume) {
this.title = title;
this.filePath = filePath;
this.playTime = playTime;
this.added = added;
this.volume = volume;
}

/** default constructor */
public Track() {
}

/** minimal constructor */
public Track(String title, String filePath, short volume) {
this.title = title;
this.filePath = filePath;
this.volume = volume;
}

public Integer getId() {
return this.id;
}

protected void setId(Integer id) {
this.id = id;
}

public String getTitle() {
return this.title;
}

public void setTitle(String title) {
this.title = title;
}

public String getFilePath() {
return this.filePath;
}

public void setFilePath(String filePath) {
this.filePath = filePath;
}

/**
* Playing time
*/
public Date getPlayTime() {
return this.playTime;
}

public void setPlayTime(Date playTime) {
this.playTime = playTime;
}

/**
* When the track was created
*/
public Date getAdded() {
return this.added;
}

public void setAdded(Date added) {
this.added = added;
}

/**
* How loud to play the track
*/
public short getVolume() {
return this.volume;
}

public void setVolume(short volume) {
this.volume = volume;
}

public String toString() {
return new ToStringBuilder(this)
.append("id", getId())
.toString();
}

}


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 17, 2005 2:44 pm 
Senior
Senior

Joined: Thu Aug 04, 2005 4:54 am
Posts: 153
Location: Birmingham, UK
Could you also post the hbm.xml mapping files and the full stack trace.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 17, 2005 2:45 pm 
Newbie

Joined: Sat Sep 17, 2005 2:15 pm
Posts: 9
Maybe this the exception what you want?

[java] 20:02:50,187 INFO SessionFactoryImpl:118 - building session factory
[java] 20:02:50,948 INFO SessionFactoryObjectFactory:82 - Not binding factory to JNDI, no JNDI name configured
[java] net.sf.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: 0, of class: com.oreilly.hh.Track
[java] at net.sf.hibernate.impl.SessionImpl.checkUniqueness(SessionImpl.java:1677)
[java] at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:937)
[java] at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:857)
[java] at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:775)
[java] at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:738)
[java] at com.oreilly.hh.CreateTest.main(CreateTest.java:43)
[java] Exception in thread "main"
[java] [ERROR] Java Result: 1
BUILD SUCCESSFUL
Total time: 10 seconds


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 17, 2005 3:35 pm 
Newbie

Joined: Sat Sep 17, 2005 2:15 pm
Posts: 9
Here is the mapping file:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>

<class name="com.oreilly.hh.Track" table="TRACK">
<meta attribute="class-description">
Represents a single playable track in the music database.
@author Jim Elliott (with help from Hibernate)
</meta>

<id name="id" type="int" column="TRACK_ID">
<meta attribute="scope-set">protected</meta>
<generator class="native"/>
</id>

<property name="title" type="string" not-null="true"/>

<property name="filePath" type="string" not-null="true"/>

<property name="playTime" type="time">
<meta attribute="field-description">Playing time</meta>
</property>

<property name="added" type="date">
<meta attribute="field-description">When the track was created</meta>
</property>

<property name="volume" type="short" not-null="true">
<meta attribute="field-description">How loud to play the track</meta>
</property>

</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 17, 2005 3:38 pm 
Newbie

Joined: Sat Sep 17, 2005 2:15 pm
Posts: 9
And here is the stack trace:

F:\Documents and Settings\ChenWei\workspace\TestHibernate>ant ctest
Buildfile: build.xml

prepare:

compile:

ctest:
[java] 21:36:55,024 INFO Environment:469 - Hibernate 2.1.6
[java] 21:36:55,064 INFO Environment:503 - loaded properties from resource
hibernate.properties: {hibernate.connection.username=sa, hibernate.connection.p
assword=, hibernate.cglib.use_reflection_optimizer=true, hibernate.dialect=net.s
f.hibernate.dialect.HSQLDialect, hibernate.connection.url=jdbc:hsqldb:data/music
, hibernate.connection.driver_class=org.hsqldb.jdbcDriver}
[java] 21:36:55,074 INFO Environment:529 - using CGLIB reflection optimize
r
[java] 21:36:55,084 INFO Configuration:350 - Mapping resource: com/oreilly
/hh/Track.hbm.xml
[java] 21:36:56,265 INFO Binder:229 - Mapping class: com.oreilly.hh.Track
-> TRACK
[java] 21:36:56,476 INFO Configuration:627 - processing one-to-many associ
ation mappings
[java] 21:36:56,476 INFO Configuration:636 - processing one-to-one associa
tion property references
[java] 21:36:56,476 INFO Configuration:661 - processing foreign key constr
aints
[java] 21:36:56,536 INFO Dialect:82 - Using dialect: net.sf.hibernate.dial
ect.HSQLDialect
[java] 21:36:56,546 INFO SettingsFactory:63 - Use outer join fetching: tru
e
[java] 21:36:56,556 INFO DriverManagerConnectionProvider:42 - Using Hibern
ate built-in connection pool (not for production use!)
[java] 21:36:56,566 INFO DriverManagerConnectionProvider:43 - Hibernate co
nnection pool size: 20
[java] 21:36:56,576 INFO DriverManagerConnectionProvider:77 - using driver
: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:data/music
[java] 21:36:56,576 INFO DriverManagerConnectionProvider:78 - connection p
roperties: {user=sa, password=}
[java] 21:36:56,626 INFO TransactionManagerLookupFactory:33 - No Transacti
onManagerLookup configured (in JTA environment, use of process level read-write
cache is not recommended)
[java] 21:36:57,707 INFO SettingsFactory:103 - Use scrollable result sets:
true
[java] 21:36:57,717 INFO SettingsFactory:106 - Use JDBC3 getGeneratedKeys(
): false
[java] 21:36:57,717 INFO SettingsFactory:109 - Optimize cache for minimal
puts: false
[java] 21:36:57,717 INFO SettingsFactory:118 - Query language substitution
s: {}
[java] 21:36:57,717 INFO SettingsFactory:129 - cache provider: net.sf.hibe
rnate.cache.EhCacheProvider
[java] 21:36:57,727 INFO Configuration:1116 - instantiating and configurin
g caches
[java] 21:36:58,048 INFO SessionFactoryImpl:118 - building session factory

[java] 21:36:58,809 INFO SessionFactoryObjectFactory:82 - Not binding fact
ory to JNDI, no JNDI name configured
[java] net.sf.hibernate.NonUniqueObjectException: a different object with t
he same identifier value was already associated with the session: 0, of class: c
om.oreilly.hh.Track
[java] at net.sf.hibernate.impl.SessionImpl.checkUniqueness(SessionImpl
.java:1677)
[java] at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:937
)
[java] at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:857
)
[java] at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier
(SessionImpl.java:775)
[java] at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:738)
[java] at com.oreilly.hh.CreateTest.main(CreateTest.java:43)
[java] Exception in thread "main"
[java] Java Result: 1

BUILD SUCCESSFUL
Total time: 8 seconds


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 17, 2005 4:10 pm 
Regular
Regular

Joined: Mon Apr 25, 2005 9:22 am
Posts: 62
Location: Bucharest/Romania
Which is the line 43 in the code? Does your DB support generating ids? Also, it seems to me that you are missing a session.flush().

./alex
--
.the_mindstorm.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 17, 2005 5:04 pm 
Senior
Senior

Joined: Thu Aug 04, 2005 4:54 am
Posts: 153
Location: Birmingham, UK
Try this

Code:
public class CreateTest {

public static void main(String args[]) throws Exception {
// Create a configuration based on the properties file we've put
// in the standard place.
Configuration config = new Configuration();

// Tell it about the classes we want mapped, taking advantage of
// the way we've named their mapping documents.
config.addClass(Track.class);

// Get the session factory we can use for persistence
SessionFactory sessionFactory = config.buildSessionFactory();

// Ask for a session using the JDBC information we've configured
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
// Create some data and persist it
tx = session.beginTransaction();

Track track1 = new Track("Russian Trance",
"vol2/album610/track02.mp3",
Time.valueOf("00:03:30"), new Date(),
(short)0);
session.save(track1);

Track track2 = new Track("Video Killed the Radio Star",
"vol2/album611/track12.mp3",
Time.valueOf("00:03:49"), new Date(),
(short)0);
session.save(track2);


Track track3 = new Track("Gravity's Angel",
"vol2/album175/track03.mp3",
Time.valueOf("00:06:06"), new Date(),
(short)0);
session.save(track3);

// We're done; make our changes permanent
tx.commit();

} catch (Exception e) {
if (tx != null) {
// Something went wrong; discard all partial changes
tx.rollback();
}
throw e;
} finally {
// No matter what, close the session
session.close();
}

// Clean up after ourselves
sessionFactory.close();
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 17, 2005 5:08 pm 
Regular
Regular

Joined: Mon Apr 25, 2005 9:22 am
Posts: 62
Location: Bucharest/Romania
jamie what is the difference between your code and the original? (seems like I miss it)

./alex
--
.the_mindstorm.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 17, 2005 5:10 pm 
Regular
Regular

Joined: Mon Apr 25, 2005 9:22 am
Posts: 62
Location: Bucharest/Romania
ahhhh, I saw it. Though, I am not sure why it should solve the issue. Can you explain?

./alex
--
.the_mindstorm.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 17, 2005 5:26 pm 
Newbie

Joined: Sat Sep 17, 2005 2:15 pm
Posts: 9
Hi Jamie,

I got the same error again:

F:\Documents and Settings\ChenWei\workspace\TestHibernate>ant ctest
Buildfile: build.xml

prepare:

compile:

ctest:
[java] 23:24:26,961 INFO Environment:469 - Hibernate 2.1.6
[java] 23:24:27,051 INFO Environment:503 - loaded properties from resour
hibernate.properties: {hibernate.connection.username=sa, hibernate.connection
assword=, hibernate.cglib.use_reflection_optimizer=true, hibernate.dialect=net
f.hibernate.dialect.HSQLDialect, hibernate.connection.url=jdbc:hsqldb:data/mus
, hibernate.connection.driver_class=org.hsqldb.jdbcDriver}
[java] 23:24:27,071 INFO Environment:529 - using CGLIB reflection optimi
r
[java] 23:24:27,101 INFO Configuration:350 - Mapping resource: com/oreil
/hh/Track.hbm.xml
[java] 23:24:30,025 INFO Binder:229 - Mapping class: com.oreilly.hh.Trac
-> TRACK
[java] 23:24:30,626 INFO Configuration:627 - processing one-to-many asso
ation mappings
[java] 23:24:30,636 INFO Configuration:636 - processing one-to-one assoc
tion property references
[java] 23:24:30,636 INFO Configuration:661 - processing foreign key cons
aints
[java] 23:24:30,776 INFO Dialect:82 - Using dialect: net.sf.hibernate.di
ect.HSQLDialect
[java] 23:24:30,786 INFO SettingsFactory:63 - Use outer join fetching: t
e
[java] 23:24:30,806 INFO DriverManagerConnectionProvider:42 - Using Hibe
ate built-in connection pool (not for production use!)
[java] 23:24:30,806 INFO DriverManagerConnectionProvider:43 - Hibernate
nnection pool size: 20
[java] 23:24:30,837 INFO DriverManagerConnectionProvider:77 - using driv
: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:data/music
[java] 23:24:30,837 INFO DriverManagerConnectionProvider:78 - connection
roperties: {user=sa, password=}
[java] 23:24:30,907 INFO TransactionManagerLookupFactory:33 - No Transac
onManagerLookup configured (in JTA environment, use of process level read-writ
cache is not recommended)
[java] 23:24:33,530 INFO SettingsFactory:103 - Use scrollable result set
true
[java] 23:24:33,530 INFO SettingsFactory:106 - Use JDBC3 getGeneratedKey
): false
[java] 23:24:33,530 INFO SettingsFactory:109 - Optimize cache for minima
puts: false
[java] 23:24:33,530 INFO SettingsFactory:118 - Query language substituti
s: {}
[java] 23:24:33,540 INFO SettingsFactory:129 - cache provider: net.sf.hi
rnate.cache.EhCacheProvider
[java] 23:24:33,550 INFO Configuration:1116 - instantiating and configur
g caches
[java] 23:24:34,301 INFO SessionFactoryImpl:118 - building session facto

[java] 23:24:36,004 INFO SessionFactoryObjectFactory:82 - Not binding fa
ory to JNDI, no JNDI name configured
[java] net.sf.hibernate.NonUniqueObjectException: a different object with
he same identifier value was already associated with the session: 0, of class:
om.oreilly.hh.Track
[java] at net.sf.hibernate.impl.SessionImpl.checkUniqueness(SessionIm
.java:1677)
[java] at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:9
)
[java] at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:8
)
[java] at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifi
(SessionImpl.java:775)
[java] at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:738
[java] at com.oreilly.hh.CreateTest.main(CreateTest.java:43)
[java] Exception in thread "main"
[java] Java Result: 1

BUILD SUCCESSFUL
Total time: 18 seconds


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 17, 2005 5:29 pm 
Newbie

Joined: Sat Sep 17, 2005 2:15 pm
Posts: 9
alexandrupopescu wrote:
Which is the line 43 in the code? Does your DB support generating ids? Also, it seems to me that you are missing a session.flush().

./alex
--
.the_mindstorm.


Here is line 43 of CreateTest.java:

43: session.save(track2);


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 17, 2005 5:32 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Here is a recommendation: Close the book. Put it away and don't open it anymore. Follow the steps outlined here: http://hibernate.org/152.html


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 21 posts ]  Go to page 1, 2  Next

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.