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?