-->
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.  [ 11 posts ] 
Author Message
 Post subject: Possibilities for customized property mapping
PostPosted: Wed Jun 07, 2006 9:45 am 
Newbie

Joined: Wed Apr 26, 2006 5:21 am
Posts: 14
HI!

We have a legacy application with >300 tables, where ALL the properties of one Business Object are stored in a HashMap (global var), so get/set methods or field access is not an option.
For each table, we have to define a composite id and a timestamp (version) column.
We are looking for a generic class to serve (read/write properties) all tables.

I have searched in the forum and found several solutions to this, which I have all tried:

1. Composite UserType
Because the composite user type has to offer a list of its properties (getPropertyNames()), we would need one of these for each table. That would be a lot of extra classes.

2. PropertyAccessor/Getter/Setter
At runtime, the Getters and Setters are only supplied with the object, not with the property name. So, a generic Getter and Setter can't be implemented. We would need a Getter and Setter for each property in each class -> more than 6000 objects.

3. Dynamic component
Dynamic component can fill a HashMap, which sounds good at first. Problem is, we have to map composite-id and timestamp seperate in the mapping XML and Hibernate always kills the entire HashMap when writing the specified (all other) properties into it. Since the columns of the composite-id and timestamp also live in the HashMap, they are being lost in this process.

Are there any other mapping alternatives, while keeping our properties in that global HashMap?

Thanks!

Hibernate version:
3.2.0 cr2

Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 07, 2006 8:31 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Use a dynamic component. You shouldn't be putting your DB id into it, though. You need something like
Code:
<class name="GlobalProperties" ...>
  <composite-id ...>
        <key-property name="id1" .../>
        <key-property name="id2" .../>
  </composide-id>

  <timestamp column="ts" .../>

  <dynamic-component name="userAttributes">
    <property name="foo" column="FOO" type="string"/>
    <property name="bar" column="BAR" type="integer"/>
    <many-to-one name="baz" class="Baz" column="BAZ_ID"/>
  </dynamic-component>
</class>

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 08, 2006 1:09 am 
Newbie

Joined: Wed Apr 26, 2006 5:21 am
Posts: 14
HI!

Yes, I thought about that when evaluating 3. But still it is preferrable that ALL properties are in the HashMap.

Is there another alternative?

Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 08, 2006 1:53 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you want the GlobalProperties object to be an entity, then you must have ids. However, you can map the id and timestamp twice: put them into the dynamic-component as well as the correct <composite-id> and <timestamp> elements. To do that, you have to map them using the formula attribute of <property>, instead of the column attribute as in my example.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 08, 2006 3:33 am 
Newbie

Joined: Wed Apr 26, 2006 5:21 am
Posts: 14
HI!

Thanks, this helped.

Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 08, 2006 4:17 am 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
If you want the GlobalProperties object to be an entity, then you must have ids. However, you can map the id and timestamp twice: put them into the dynamic-component as well as the correct <composite-id> and <timestamp> elements. To do that, you have to map them using the formula attribute of <property>, instead of the column attribute as in my example.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 08, 2006 10:26 am 
Newbie

Joined: Wed Apr 26, 2006 5:21 am
Posts: 14
HI!

Unfortunately, there is a problem with your suggestion:

Native SQL queries do not work anymore.

Hibernate now fetches the id ("catalog") and timestamp columns for the formulas when using HQL by using those columns 2 times in the select, but not when using native SQL:

Mapping:
<property name="CATALOG" type="string" formula="catalog"/>
<property name="TIMESTAMP" type="byte[]" formula="timestamp"/>

HQL example:
select catalog0_.catalog as catalog0_, catalog0_.timestamp as timestamp0_, catalog0_.ID as ID0_, catalog0_.INSDATE as INSDATE0_, catalog0_.INSUSER as INSUSER0_, catalog0_.WAEHRUNG as WAEHRUNG0_, catalog0_.MARKETPLACE as MARKETPL7_0_, catalog0_.FIRMA as FIRMA0_, catalog0_.NAME as NAME0_, catalog0_.SUPPLIERID as SUPPLIERID0_, catalog0_.DATUM as DATUM0_, catalog0_.BUYERID as BUYERID0_, catalog0_.MEDIUM as MEDIUM0_, catalog0_.SPRACHE as SPRACHE0_, catalog0_.VERSION as VERSION0_, catalog0_.BEMERKUNG as BEMERKUNG0_, catalog0_.FORMAT as FORMAT0_, catalog0_.UPDDATE as UPDDATE0_, catalog0_.UPDUSER as UPDUSER0_, catalog0_.catalog as formula0_, catalog0_.timestamp as formula1_ from catalog catalog0_ where CATALOG='ENO-S-0000'

When I use native SQL like this:
session.createSQLQuery("select * from catalog").addEntity("catalog").uniqueResult();

then the pseudo formula columns are of course null, leading to a null pointer exception:

Exception in thread "P2plus-0" java.lang.NullPointerException
at org.hibernate.loader.DefaultEntityAliases.intern(DefaultEntityAliases.java:133)
at org.hibernate.loader.DefaultEntityAliases.getSuffixedPropertyAliases(DefaultEntityAliases.java:106)
at org.hibernate.loader.DefaultEntityAliases.<init>(DefaultEntityAliases.java:52)
at org.hibernate.loader.ColumnEntityAliases.<init>(ColumnEntityAliases.java:16)
at org.hibernate.loader.custom.SQLCustomQuery.<init>(SQLCustomQuery.java:182)
at org.hibernate.engine.query.NativeSQLQueryPlan.<init>(NativeSQLQueryPlan.java:43)
at org.hibernate.engine.query.QueryPlanCache.getNativeSQLQueryPlan(QueryPlanCache.java:113)
at org.hibernate.impl.AbstractSessionImpl.getNativeSQLQueryPlan(AbstractSessionImpl.java:137)
at org.hibernate.impl.AbstractSessionImpl.list(AbstractSessionImpl.java:142)
at org.hibernate.impl.SQLQueryImpl.list(SQLQueryImpl.java:164)
at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:756)



Thomas


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 08, 2006 5:23 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Are you using an XML dataset? addEntity(String) is for use with XML data, not data in a RDBMS. Use addEntity(Catalog.class) and try it again.

_________________
Code tags are your friend. Know them and use them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 09, 2006 1:43 am 
Newbie

Joined: Wed Apr 26, 2006 5:21 am
Posts: 14
HI!

Same result.

Thomas


Top
 Profile  
 
 Post subject: any resolution to this??
PostPosted: Mon Aug 28, 2006 5:25 pm 
Newbie

Joined: Wed Dec 15, 2004 9:45 am
Posts: 7
I get this same problem trying to use a native query. Did you find something that fixed this??

Thanx,
LES


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 28, 2006 5:39 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
You could use {}-notation or return-property to list all the properties of the class with the formulae, instead of using implicit column naming. It's a lot more verbose and takes more maintenance if your class changes, but it will work. If you have a class with a property realValue and a formula derivedValue, this is what you need:
Code:
<sql-query name="YourSQLQuery">
  <return class="YourClass" alias="x"/>
  select col1 as {x.realValue},
    col2 + col3 + 'otherstuff' as {x.derivedValue}
  from yourtable
  where id = :id
</sql-query>

_________________
Code tags are your friend. Know them and use them.


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