-->
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: Session.get() behaves strangely when using composite id.
PostPosted: Wed Jul 04, 2012 2:11 pm 
Newbie

Joined: Wed May 27, 2009 11:11 am
Posts: 1
Hi,
I'm trying to perform get on the session for object which declares composite id without a mapped composite identifier.
Hibernate version is 3.5.5.

Fetching code is generic and reads container objects wrapping actual data:
Code:
ClassMetadata metadata = session.getSessionFactory().getClassMetadata(wrapper.getDomainClass());
Serializable id = metadata.getIdentifier(wrapper, EntityMode.POJO);
return session.get(wrapper.getDomainClass(), id, LockOptions.UPGRADE);

Code doesn't know anything about actual mapping so it has to consult metadata about the id.

If mapping is defined like:
Code:
<hibernate-mapping default-access="field">
  <class name="Wrapper"
      entity-name="Data"
      table="DATA">
    <composite-id>
      <key-property name="identifier" column="identifier" />
      <key-property name="version" column="version" />
    </composite-id>

    <component name="domainObject" class="Data">
      <property name="source" column="source" />
    </component>               
  </class>
</hibernate-mapping>

without composite identifier class, id is equal to the object itself and is equal to wrapper reference.
When I do session.get() instead of fetching object from the database, it returns back same object as was passed in id (not an equal object, but same instance of object).

Solution that I found so far is introduce mapped composite identifier and change mapping to:
Code:
<hibernate-mapping default-access="field">
  <class name="Wrapper"
      entity-name="Data1"
      table="DATA_1">
    <composite-id  class="SurrogateKey" mapped="true">
      <key-property name="identifier" column="identifier" />
      <key-property name="version" column="version" />
    </composite-id>

    <component name="domainObject" class="Data">
      <property name="source" column="source" />
    </component>               
  </class>
</hibernate-mapping>

SurrogateKey is defined as object with two fields and equals/hashcode as required.
After that id returned by metadata.getIdentifier() is an instance of SurrogateKey and session.get() fetches object from the database if it exists.

The problem with mapping fix is that property names for criteria and HQL change from identifier to id.identifier and that is actually breaking a lot of existing code.

  • Is there a way to make session.get() work without declaring Id class (I know this is discouraged, but amount of changes needed might be prohibitive)?
  • Could the alternative be telling hibernate to treat properties as before, without adding id. in front of them?
  • Any other options/workarounds available?
  • Pointers to relevant docs?


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.