-->
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.  [ 9 posts ] 
Author Message
 Post subject: More than one row with the given identifier was found
PostPosted: Thu Dec 10, 2015 6:09 am 
Newbie

Joined: Thu Dec 10, 2015 5:19 am
Posts: 3
Hello, from time to time i encounter such exception:
"More than one row with the given identifier was found: DeploymentPK@40f5ce59, for class: OwsmServicesDeployment"
I'm using hibernate 3.3.1.GA as JPA realization and ehcache as 2-nd and 3-rd(query) level caches.
Most interesting that exception occurs not alwaus but rarely and without obvios connection to external conditions, i mean i can query same query 10 times and can get or not this exception (Sooth to say, i cant reproduce it on test environment).
I'm using rather simple configuration of my entities without any joins or relations (but data is taken from db view - not from table):
Entity Deployment:
Code:
@Entity
@Table(name="DEPLOYMENT")
public class Deployment implements DeploymentAbstract, Serializable {
   private static final long serialVersionUID = 1L;

   @EmbeddedId
   private DeploymentPK id;

   @Column(name="CODE", insertable=false, updatable=false)
   private String Code;

   @Column(name="SID", insertable=false, updatable=false)
   private String Sid;

   @Column(name="NATIVE_REG_ID", insertable=false, updatable=false)
   private String nativeRegId;

   @Column(name="REGION_ID", insertable=false, updatable=false)
   private String RegionId;
getters/setters

}
And id:
Code:
@Embeddable
public class DeploymentPK implements DeploymentPKAbstract, Serializable {

   private static final long serialVersionUID = 5675951380093663967L;

   @Column(name="CODE", unique = true, insertable=false, updatable=false)
   private String Code;
   
   @Column(name="SID", unique = true, insertable=false, updatable=false)
   private String Sid;

   @Override
   public int hashCode() {
      final int prime = 31;
      int result = 1;
      result = prime * result + ((Code == null) ? 0 : Code.hashCode());
      result = prime * result + ((Sid == null) ? 0 : Sid.hashCode());
      return result;
   }

   @Override
   public boolean equals(Object obj) {
      if (this == obj)
         return true;
      if (obj == null)
         return false;
      if (getClass() != obj.getClass())
         return false;
      DeploymentPK other = (DeploymentPK) obj;
      if (Code == null) {
         if (other.Code != null)
            return false;
      } else if (!Code.equals(other.Code))
         return false;
      if (Sid == null) {
         if (other.Sid != null)
            return false;
      } else if (!Sid.equals(other.Sid))
         return false;
      return true;
   }

And query that causes error:
Code:
<named-query name="DeploymentBySIDandRegion">
      <query>select a from Deployment a where a.RegionId = :RegionId and a.Sid = :sid</query>
        <hint name="org.hibernate.cacheable" value="true"/>
</named-query>

I don't expect that anybody will help me with exactly this situation, but i want to ask some questions:
1) As i've seen in hibernate code, this exception occurs at method
Code:
org.hibernate.loader.entity.AbstractEntityLoader.load(
         SessionImplementor session,
         Object id,
         Object optionalObject,
         Serializable optionalId,
         LockOptions lockOptions)

I wasn't able to trace what's happening furter in hibernate code, so i want to ask if this method tries to hit the cache?
2)Is there some probability that 2 or more same records could be placed in cache at some conditions? (i'm using memory and disc cache) Can memory and disc cache conflict in some way?
3) Can it be that 2 different queries that query same entity may conflict?
I've been told that exception started to occur after disc cache was set. But i've searced, googled, and nowhere was able to find any references regarding such conflicts.
Can anybody share some suggestions about my situation or answer any of my questions please? (sorry for my english)


Top
 Profile  
 
 Post subject: Re: More than one row with the given identifier was found
PostPosted: Thu Dec 10, 2015 6:52 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
One thing that I find curios about your mapping is that the embeddable cannot manage the primary key fields because the insertbale/updatable are set to false on both the Deployment and the DeploymentPK.

I would remove those attributes from the DeploymentPK like this:

Code:

@Column(name="CODE", unique = true)
private String Code;
   
@Column(name="SID", unique = true)
private String Sid;


and then also remove these fields from "Deployment" entity:

Code:
@Column(name="CODE", insertable=false, updatable=false)
private String Code;

@Column(name="SID", insertable=false, updatable=false)
private String Sid;


You can access those from the entity identifier anyway.

The problem that you reported happens when you have a Department in your Session, and you try to add a new one using saveOrUpdate or update.
If that's not the case, you need to write a reproducible test case for it and submit it as a bug report, as it might be some issue.

I would suggest trying to update to a new version of Hibernate and see if it reproduces because you are running an old version: 3.3.1.GA.
Maybe there was an issue at some point and it got fixed.


Top
 Profile  
 
 Post subject: Re: More than one row with the given identifier was found
PostPosted: Thu Dec 10, 2015 7:43 am 
Newbie

Joined: Thu Dec 10, 2015 5:19 am
Posts: 3
I think it's not about update or insert - these entities are not managed to be updatable or insertable and are taken from the database view. All interactions that involve described entities represented by 2 queries:
Code:
<named-query name="DeploymentBySIDandRegion">
      <query>select a from Deployment a where a.RegionId = :RegionId and a.Sid = :sid</query>
        <hint name="org.hibernate.cacheable" value="true"/>
</named-query>

and
Code:
<named-query name="DeploymentBySIDandCode">
      <query>select a from Deployment a where a.Code = :Code and a.Sid = :sid</query>
                <hint name="org.hibernate.cacheable" value="true"/>
</named-query>

that are results of webservice method invocation, through the EJB dao like:
@SuppressWarnings("unchecked")
@Override
public List<DeploymentAbstract> getDeployments(String code, String sid) {
return entityManager.createNamedQuery("DeploymentBySIDandCode")
.setParameter("sid", sid)
.setParameter("Code", code)
.getResultList();
}


Top
 Profile  
 
 Post subject: Re: More than one row with the given identifier was found
PostPosted: Thu Dec 10, 2015 7:51 am 
Regular
Regular

Joined: Mon Oct 19, 2015 7:49 am
Posts: 61
Location: ChengDu China
First, please follow the suggestion of mihalcea_vlad, because your mapping is wrong
Second, change your query to be
Code:
select a from Deployment a where a.id.code = :code and a.id.sid = :sid


Top
 Profile  
 
 Post subject: Re: More than one row with the given identifier was found
PostPosted: Thu Dec 10, 2015 7:54 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
So you only use them to query the database. In that case it could be something related to the cache, especially if you use two stores for the same 2nd level cache. It could be that there is a synchronization issue and from time to time, the same entity is tried to be pushed from the 2nd level cache into the 1st level cache from two different cache stores and the 1st level cache prevents registering the same entity twice.

Try using a single cache store. EHCache supports in-memory caching with overflowing to disk, but it's just one cache store.


Top
 Profile  
 
 Post subject: Re: More than one row with the given identifier was found
PostPosted: Thu Dec 10, 2015 8:18 am 
Newbie

Joined: Thu Dec 10, 2015 5:19 am
Posts: 3
mihalcea_vlad wrote:
So you only use them to query the database. In that case it could be something related to the cache, especially if you use two stores for the same 2nd level cache. It could be that there is a synchronization issue and from time to time, the same entity is tried to be pushed from the 2nd level cache into the 1st level cache from two different cache stores and the 1st level cache prevents registering the same entity twice.

Try using a single cache store. EHCache supports in-memory caching with overflowing to disk, but it's just one cache store.

Sorry, i mean i already use in-memory cache with overflow to disk.


Top
 Profile  
 
 Post subject: Re: More than one row with the given identifier was found
PostPosted: Thu Dec 10, 2015 8:41 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
In this case, it could be some synchronization issue.
I think it's worth trying out with a newer Hibernate version and see if the problem persists.
Hibernate 3.3.1 is from 2008 and even if it's a bug, you get a better chance of getting a fix on 4.x or 5.x versions.


Top
 Profile  
 
 Post subject: Re: More than one row with the given identifier was found
PostPosted: Thu Dec 10, 2015 8:54 am 
Regular
Regular

Joined: Mon Oct 19, 2015 7:49 am
Posts: 61
Location: ChengDu China
This make me confused, the 1st level cache "org.hibernate.engine.StatefulPersistenceContext" use these two fields to cache all the objects
Code:
private Map<EntityKey, Object> entitiesByKey;
private ConcurrentMap<EntityKey, Object> proxiesByKey;

How could it retain duplicated objects?


Top
 Profile  
 
 Post subject: Re: More than one row with the given identifier was found
PostPosted: Fri Dec 11, 2015 3:01 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
It cannot hold duplicates but it throws an exception if somehow there is an action which would want to re-associate an entity that's already managed.


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