-->
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.  [ 8 posts ] 
Author Message
 Post subject: Threads and "More than one row with the given identifie
PostPosted: Mon Jan 12, 2004 6:56 pm 
Newbie

Joined: Fri Dec 12, 2003 2:12 am
Posts: 16
I have some code that creates a bunch of threads to query the database (they load the same object-type, and sometimes even the same object).

I am getting this error:

"More than one row with the given identifier was found"

At first, I thought the issue was that I was using one session amongst all the different threads, so I passed the session factory around, and each thread got it's own session.

Then I thought that maybe one session was trying to load the same object more than once, so I added some code (a hashtable to track the primary keys, and I tried a "get" first to see if I got the object back):

Long boatListingID = new Long(lBoatListingID);
if (ht.get(boatListingID) != null)
{
System.out.println("FOUND A DUPLICATE: " + boatListingID.toString());
break;
}
ht.put(boatListingID, boatListingID);


BoatListing bl = (BoatListing) session.get(BoatListing.class, new Long(lBoatListingID));
if (bl == null)
bl = (BoatListing) session.load(BoatListing.class, new Long(lBoatListingID));

Same issue.

Then I thought maybe I should handle the connections rather than let Hibernate do it via the hibernate.properties file. No luck.

Anyone know what I am doing wrong? I've read the docs and searched the forum for the "more than one row" string, without luck.

Thanks.
David


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2004 6:58 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
it sounds like you have more than one row in the database with the same id values!

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2004 6:59 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Are you sure your id is really unique in the DB?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2004 7:06 pm 
Newbie

Joined: Fri Dec 12, 2003 2:12 am
Posts: 16
Yes, I checked:

net.sf.hibernate.HibernateException: More than one row with the given identifier was found: 10047830, for class: stresstest.hibernate.BoatListing

mysql> select count(*) from boat_listing where boat_listing_id = 10047830;
+----------+
| count(*) |
+----------+
| 1 |
+----------+
1 row in set (0.00 sec)

The "boat_listing_id" column is the primary key for the table. Should have mentioned that - was the first thing I checked.

Thanks for the response, tho.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2004 7:12 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I am 99% sure the query hibernate executes returns more than one row. Please enable SQL logging and execute the select by hand and see what it returns.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2004 7:38 pm 
Newbie

Joined: Fri Dec 12, 2003 2:12 am
Posts: 16
gloeglm wrote:
I am 99% sure the query hibernate executes returns more than one row. Please enable SQL logging and execute the select by hand and see what it returns.


It is returning more than one row - the issue was that one of the tables that was outer-joined has duplicate values in it for some reason. Since it has two rows, when it joins the primary table, two sets of results are returned.

Thanks for the tip.

David.


Top
 Profile  
 
 Post subject: Re: Threads and "More than one row with the given identifie
PostPosted: Fri Jul 24, 2009 1:56 pm 
Beginner
Beginner

Joined: Fri Apr 20, 2007 10:48 am
Posts: 49
Location: France
Hello,

I have a similar problem to the one here but I'm unable to highlight where hibernate finds two rows with the same id (I verified, it's impossible to insert two lines with the same id via sql).

Code:
   protected void retreiveIdAndUpdate(EntityGeoLocation geoLocation, EntityCalendar alreadyInDb) {
      Query findCalendarQry = em.createQuery("from " + EntityCalendar.class.getSimpleName() + " where calendarId=:calendarId");
      String calendarGoogleId = alreadyInDb.getCalendarId();
      findCalendarQry.setParameter("calendarId", calendarGoogleId);
      // there's only one (if more or less, already handled before)
      EntityCalendar calendar = (EntityCalendar) findCalendarQry.getResultList().get(0);
      long entityId = calendar.getOwner().getId();
      geoLocation.setId(entityId);

      em.clear();
      // problem occurs here
      Set<Phone> phones = geoLocation.getPhones();
      for (Phone phone : phones) {
         if (phone.getId() == 0) {
            em.persist(phone);
         } else {
[b]            [color=#BF0000]em.merge(phone);[/color][/b]
         }
      }
      // test
      // Query testQuery = em.createQuery("from " +
      // EntityGeoLocation.class.getSimpleName() + " where id=" +
      // geoLocation.getId());
      // List resultList = testQuery.getResultList();
      // System.out.println(resultList);

      Set<EntityCalendar> calendars = geoLocation.getCalendars();
      geoLocation.setCalendars(new HashSet<EntityCalendar>());

      em.merge(geoLocation);
      em.clear();
      geoLocation.setCalendars(calendars);
   }


I tried the commented etst and I confirm there's only one result.

The error stack (where the bold line is the one highlited in the code upper) If I don't do a merge on Phone (only persist), no error occurs :
Caused by: javax.persistence.PersistenceException: org.hibernate.HibernateException: More than one row with the given identifier was found: 33, for class: eu.nextstreet.web.ejb.model.EntityGeoLocation
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:244)
at org.jboss.jpa.tx.TransactionScopedEntityManager.merge(TransactionScopedEntityManager.java:193)
at eu.nextstreet.web.ejb.session.GeoEntityManagerBean.retreiveIdAndUpdate(GeoEntityManagerBean.java:65)
at eu.nextstreet.web.ejb.session.GeoEntityManagerBean.insertEntity(GeoEntityManagerBean.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.jboss.aop.joinpoint.MethodInvocation.invokeTarget(MethodInvocation.java:122)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:111)
at org.jboss.ejb3.EJBContainerInvocationWrapper.invokeNext(EJBContainerInvocationWrapper.java:69)
at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.invoke(InterceptorSequencer.java:73)
at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.aroundInvoke(InterceptorSequencer.java:59)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.jboss.aop.advice.PerJoinpointAdvice.invoke(PerJoinpointAdvice.java:174)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.fillMethod(InvocationContextInterceptor.java:72)
at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_fillMethod_25820485.invoke(InvocationContextInterceptor_z_fillMethod_25820485.java)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.setup(InvocationContextInterceptor.java:88)
at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_setup_25820485.invoke(InvocationContextInterceptor_z_setup_25820485.java)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
at org.jboss.ejb3.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:62)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:56)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:68)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
... 42 more
Caused by: org.hibernate.HibernateException: More than one row with the given identifier was found: 33, for class: eu.nextstreet.web.ejb.model.EntityGeoLocation
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:92)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3072)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:223)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:873)
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:590)
at org.hibernate.type.EntityType.resolve(EntityType.java:412)
at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:139)
at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:877)
at org.hibernate.loader.Loader.doQuery(Loader.java:752)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259)
at org.hibernate.loader.Loader.loadEntity(Loader.java:1881)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3072)
at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434)
at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415)
at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165)
at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:223)
at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126)
at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:905)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:842)
at org.hibernate.event.def.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:281)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:167)
at org.hibernate.event.def.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:81)
at org.hibernate.impl.SessionImpl.fireMerge(SessionImpl.java:704)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:688)
at org.hibernate.impl.SessionImpl.merge(SessionImpl.java:692)
at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:235)
... 77 more

The involved objects are
Code:
public class Phone {
...
   @ManyToOne(fetch = FetchType.EAGER)
   public EntityGeoLocation getEntity() {
      return entity;
   }
}


Code:
@Entity
public class EntityGeoLocation {
   @OneToMany(mappedBy = "entity", cascade = CascadeType.PERSIST)
   public Set<Phone> getPhones() {
      return phones;
   }
}


As you can see, nothing special..

Any comment is very welcome,
Best Regards,
Zied Hamdi

_________________
Regards,
Zied Hamdi


Top
 Profile  
 
 Post subject: Re: Threads and "More than one row with the given identifie
PostPosted: Fri Jul 24, 2009 2:11 pm 
Beginner
Beginner

Joined: Fri Apr 20, 2007 10:48 am
Posts: 49
Location: France
Sorry!!

When I wanted to post the selects made before I saw one that explained me the problem (as was mentioned above the outer join returns two rows on the Adress though the relation had to be @OneToOne (this query is the third above the last one that's why I didn't see it :-/.

Anyway, would't it be nice if hibernate genereates a unique constraint on a foreign key where the relationship is declared as @OneToOne (and nicer if it checks the shema and warns about it), this will avoid many database unconsistency with the declared model (that always occur in the developement phase)

Code:
select entitygeol0_.id as id0_1_, entitygeol0_.category_id as category16_0_1_, entitygeol0_.currency as currency0_1_, entitygeol0_.endDate as endDate0_1_, entitygeol0_.googlePassword as googlePa4_0_1_, entitygeol0_.googleUser as googleUser0_1_, entitygeol0_.homePage as homePage0_1_, entitygeol0_.htmlDescription as htmlDesc7_0_1_, entitygeol0_.lat as lat0_1_, entitygeol0_.lng as lng0_1_, entitygeol0_.name as name0_1_, entitygeol0_.payed as payed0_1_, entitygeol0_.payementDate as payemen12_0_1_, entitygeol0_.subsribingDate as subsrib13_0_1_, entitygeol0_.unlockKey as unlockKey0_1_, entitygeol0_.unlocked as unlocked0_1_, adress1_.id as id6_0_, adress1_.adress as adress6_0_, adress1_.complement as complement6_0_, adress1_.country as country6_0_, adress1_.geoLocation_id as geoLocat8_6_0_, adress1_.number as number6_0_, adress1_.town as town6_0_, adress1_.zipCode as zipCode6_0_ from EntityGeoLocation entitygeol0_ left outer join Adress adress1_ on entitygeol0_.id=adress1_.geoLocation_id where entitygeol0_.id=33


Thanks for your reading, and tell me if you think the idea is a candidate for an issue?

Best Regards,
Zied Hamdi
http://zatreex.sf.net
http://csvengine.sf.net
http://intocode.fr
and some more... ;-)

_________________
Regards,
Zied Hamdi


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.