-->
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.  [ 1 post ] 
Author Message
 Post subject: Spurious error using @Enumerated in a composite key
PostPosted: Wed Jul 07, 2010 8:11 am 
Newbie

Joined: Wed Sep 24, 2008 12:48 pm
Posts: 6
Dear All,

I'm getting some spurious errors on a live application (a few hundred out of 100k visits) and it looks like the problem is that Hibernate suddenly decides to change the internal mapping of some entities. These entities share something in common: they have a composite key which is mapped with the @IdClass annotation and an enum is part of the composite ID. We have other entities in the application with composite keys, but they don't show this behaviour, which looks like it's something related mapping enums in composite keys.

One oddity from the app (please don't point your "good practice" cannons to me :) ) is that all of the entities in this application "extend" from what some people here call DTO (although is not really a DTO). So we have a class that has the fields for all the data and then the real entity extends the DTO and adds the mapping annotation (please see below for an example).

The problem happens when we try to fetch an entity (in the example CoreObjectPriceItem) and instead of querying the enum that is part of the PK as a string, it uses the cardinal number. In the following example, PriceType is an Enum and you can see on the exception at the bottom that hibernate is trying to load the entity with the values {coreObjectId=21081, priceType=2} (note the cardinal value used to query for priceType), when in fact it should use something like {coreObjectId=21081, priceType="LIST"}.

We tested this with hibernate-entitymanager:3.4.0.GA and hibernate-entitymanager:3.5.3-Final (and their associated maven dependencies).

Has anyone had the same problem? Or could point me to a solution or a Jira?

Unfortunately, I can't reproduce this consistently in order to provide a running example of the bug :(.


Thanks a LOT!


"DTO"
Code:
public class PriceItem {

   private Long coreObjectId;

   private PriceType priceType;

   private BigDecimal price;

   //[...] getters and setters for each field [...]


Mapped entity
Code:
@Entity
@Table(name = "v_core_object_prices")
@IdClass(CoreObjectPriceItem.CoreObjectPriceComponentId.class)
@AttributeOverrides( {
      @AttributeOverride(name = "priceType", column = @Column(name = "PRICE_TYPE", nullable = false, updatable = false, insertable = false )),
      @AttributeOverride(name = "coreObjectId", column = @Column(name = "PRICE_CORE_OBJECT_ID", nullable = false, updatable = false, insertable = false)) })
@org.hibernate.annotations.Entity(mutable = false)
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY, region = CacheRegion.MEDIUM_ENTITY)
public class CoreObjectPriceItem extends PriceItem implements Serializable {

   @Id
   @Override
   public PriceType getPriceType() {
      return super.getPriceType();
   }

   @Id
   @Override
   public Long getCoreObjectId() {
      return super.getCoreObjectId();
   }

   @Column(name = "PRICE")
   @Override
   public BigDecimal getPrice() {
      return super.getPrice();
   }

   @Embeddable
   public static class CoreObjectPriceComponentId implements Serializable {
      public PriceType priceType;
      public Long coreObjectId;

      public CoreObjectPriceComponentId() {}

      public CoreObjectPriceComponentId(PriceType getPriceType, Long getCoreObjectId) {
         this.priceType = getPriceType;
         this.coreObjectId = getCoreObjectId;
      }

      @Enumerated(EnumType.STRING)
      public PriceType getPriceType() {
         return priceType;
      }

      // [...] plus extra getters, setters, hashcode and equals [...]

   }


[b]Stack trace[/n]
Code:
2010-07-07 10:26:09,096 [WARN] o.h.u.JDBCExceptionReporter SQL Error: 1722, SQLState: 42000 ip=xxxxxxxxx
2010-07-07 10:26:09,097 [ERROR] o.h.u.JDBCExceptionReporter ORA-01722: invalid number
ip=xxxxxxxxxxxx
2010-07-07 10:26:09,102 [INFO] o.h.e.d.DefaultLoadEventListener Error performing load command ip=xxxxxxxxxxxx
org.hibernate.exception.SQLGrammarException: could not load an entity: [uk.co.vodco.sherbet.discovery.pricing.vo.CoreObjectPriceItem#component[coreObjectId,priceType]{coreObjectId=21081, priceType=2}]
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:90) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.loader.Loader.loadEntity(Loader.java:1899) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:71) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:65) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3062) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.event.def.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:434) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.event.def.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:415) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.event.def.DefaultLoadEventListener.load(DefaultLoadEventListener.java:165) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.event.def.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:223) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.event.def.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:126) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.impl.SessionImpl.fireLoad(SessionImpl.java:906) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:874) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:590) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.type.ManyToOneType.assemble(ManyToOneType.java:219) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.collection.PersistentSet.initializeFromCache(PersistentSet.java:147) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.cache.entry.CollectionCacheEntry.assemble(CollectionCacheEntry.java:58) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.event.def.DefaultInitializeCollectionEventListener.initializeCollectionFromCache(DefaultInitializeCollectionEventListener.java:159) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.event.def.DefaultInitializeCollectionEventListener.onInitializeCollection(DefaultInitializeCollectionEventListener.java:71) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.impl.SessionImpl.initializeCollection(SessionImpl.java:1744) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.collection.AbstractPersistentCollection.forceInitialization(AbstractPersistentCollection.java:476) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.engine.StatefulPersistenceContext.initializeNonLazyCollections(StatefulPersistenceContext.java:867) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:264) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.loader.Loader.doList(Loader.java:2232) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.loader.Loader.listUsingQueryCache(Loader.java:2161) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.loader.Loader.list(Loader.java:2121) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1149) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102) [hibernate-core-3.3.2.GA.jar:3.3.2.GA]
        at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:67) [hibernate-entitymanager-3.4.0.GA.jar:3.4.0.GA]
        at uk.co.vodco.sherbet.discovery.ccp.CCPImpl.getBrand(CCPImpl.java:1227) [discovery-data-5.9.2.jar:na]



Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.