-->
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.  [ 10 posts ] 
Author Message
 Post subject: Middlegen not generate seperate composite key (PK) class
PostPosted: Tue Jan 03, 2006 7:58 am 
Newbie

Joined: Tue Oct 25, 2005 12:28 pm
Posts: 18
Location: London
Hi fellow Hibernaters,
I am having an issue with seperate composite primary key classes. The problem is, I don't want them as I feel they complicate my unit tests. Maybe its just me, but still, I don't want them generated by Middlgen.

I am using Hibernate 2.1 and Middelgen 2.1.

I can't find a better description of what the following configurations to the Hibernate plugin do, so perhaps I am barking up the wrong tree:

plainCompoundKeys and genIntegratedCompositeKeys.

By default they are set to true and false respectively [ref http://boss.bekk.no/boss/middlegen/plug ... rnate.html].

The genIntegratedCompositeKeys setting I think I need to set to true, thinking this will not generate a seperate composite PK class, rather just add the naked params into my constructors, to allow basic instantiation of POJOs i.e. without having to instantiate lots of seperate composite PK classes first.

Firstly, is my understanding correct?

Secondly, can anyone suggest please how I can configure build.xml to not generate seperate composite PK classes, since the experiments I have done don't seem to make a difference to the *-prefs.properties file.

Thanks again in advance.

Mike


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 04, 2006 6:42 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
I strongly suggest you use separate classes for your composite keys. It really is a much better approach. Given you appear to not agree - Middlegen can generate the mapping document so that they use Integrated Composite keys. Yes genIntegratedCompositeKeys setting you have found is the correct one to set appropriately.

You should see the mapping document not include the class name in the composite-id tags when this is set.

The *-prefs file is set from the GUI not the ANT Build script. They are separate entities. I suggest you delete the old artifacts (mapping and classes) and try again with the integrated set to true.

I had commited a number of enhancements to Middlegen since 2.1 was released. I suggest you use the CVS version.


Top
 Profile  
 
 Post subject: tried seperate pk classes but new problem - class cast
PostPosted: Fri Jan 13, 2006 6:38 am 
Newbie

Joined: Tue Oct 25, 2005 12:28 pm
Posts: 18
Location: London
Hi David, thanks for your suggestion. I will go along with the composite key classes.

I regenerated all my mappings and classes, including my new PK classes but I have encounted another problem. It is the following exception:

net.sf.hibernate.PropertyAccessException: exception getting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) getter of com.blah.ods_schema_mapping.hibernate.OdsCustomerPK.?

I adding hibernate.properties to my classpath, containing "hibernate.cglib.use_reflection_optimizer=false" which took me to a more descriptive error message, to which I am stuck:

IllegalArgumentException occurred calling getter of com.ods_schema_mapping.hibernate.OdsCustomerPK.customerId

The type in the OdsCustomerPk is BigDecimal. I generated all the code so assumed this should work out of the box. I also tried regenerating all the code, using Integer rather than BigDecimal but get the same error.

Full trace is:

net.sf.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.ods_schema_mapping.hibernate.OdsCustomerPK.customerId
at net.sf.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:110)
at net.sf.hibernate.type.ComponentType.getPropertyValue(ComponentType.java:179)
at net.sf.hibernate.type.ComponentType.getPropertyValues(ComponentType.java:205)
at net.sf.hibernate.type.ComponentType.nullSafeGetValues(ComponentType.java:164)
at net.sf.hibernate.type.ComponentType.nullSafeSet(ComponentType.java:151)
at net.sf.hibernate.loader.Loader.bindPositionalParameters(Loader.java:753)
at net.sf.hibernate.loader.Loader.prepareQueryStatement(Loader.java:793)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:269)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:138)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:941)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:961)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:59)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:51)
at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:413)
at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:2131)
at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:2001)
at net.sf.hibernate.impl.SessionImpl.internalLoadOneToOne(SessionImpl.java:1955)
at net.sf.hibernate.type.OneToOneType.resolveIdentifier(OneToOneType.java:71)
at net.sf.hibernate.type.EntityType.resolveIdentifier(EntityType.java:208)
at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:2219)
at net.sf.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:319)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:309)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:138)
at net.sf.hibernate.loader.Loader.doList(Loader.java:1063)
at net.sf.hibernate.loader.Loader.list(Loader.java:1054)
at net.sf.hibernate.hql.QueryTranslator.list(QueryTranslator.java:854)
at net.sf.hibernate.impl.SessionImpl.find(SessionImpl.java:1554)
at net.sf.hibernate.impl.QueryImpl.list(QueryImpl.java:49)
at test.com.ods_schema_mapping.hibernate.TestGetSessionFactory.testMain(TestGetSessionFactory.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:474)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:342)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:194)
Caused by: java.lang.IllegalArgumentException: object is not an instance of declaring class
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at net.sf.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:96)
... 43 more

I am using the 2.1 release i.e. not the latest code.

PK code file:

public class OdsCustomerPK implements Serializable {

/** identifier field */
private Integer customerId;

/** identifier field */
private Integer customerVersId;

/** full constructor */
public OdsCustomerPK(Integer customerId, Integer customerVersId) {
this.customerId= customerId;
this.customerVersId= customerVersId;
}
..........
}

and the pojo is:

public class OdsCustomer implements Serializable {

/** identifier field */
private com.ods_schema_mapping.hibernate.OdsCustomerPK comp_id;
..........
/** full constructor */
public OdsTransRepo(com.ods_schema_mapping.hibernate.OdsCustomerPK comp_id,
..........
}


Any ideas please Hibernate community? Could this be a bug? Should I use 3.0?

Thanks in advance

Mike

_________________
Cheers,
MikeR


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 13, 2006 6:34 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Please show me the mapping that was generated. I personally use long or java.lang.Long types for integer keys. In anycase, there is a miss match here most likely in the types your using.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 19, 2006 10:24 am 
Newbie

Joined: Tue Oct 25, 2005 12:28 pm
Posts: 18
Location: London
david wrote:
Please show me the mapping that was generated. I personally use long or java.lang.Long types for integer keys. In anycase, there is a miss match here most likely in the types your using.


Hi David, please excuse the delay - doing several projects at once ;)

Here is the generated mappings file. See anything wrong with it?


<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >

<hibernate-mapping>
<!--
Created by the Middlegen Hibernate plugin 2.1

http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
-->

<class
name="com.ods_schema_mapping.hibernate.OdsCustomer"
table="ODS_TRANS_REPO"
>
<meta attribute="implement-equals" inherit="false">true</meta>

<composite-id name="comp_id" class="com.ods_schema_mapping.hibernate.OdsCustomerPK">
<key-property
name="transId"
column="TRANS_ID"
type="java.lang.Integer"
length="38"
/>
<key-property
name="transVersId"
column="TRANS_VERS_ID"
type="java.lang.Integer"
length="38"
/>
</composite-id>

<property
name="clearanceProductType"
type="java.lang.String"
column="CLEARANCE_PRODUCT_TYPE"
length="4"
/>
<property
name="repoCallInd"
type="java.lang.String"
column="REPO_CALL_IND"
length="1"
/>
<property
name="repoInterestNativeAmt"
type="java.lang.Integer"
column="REPO_INTEREST_NATIVE_AMT"
length="28"
/>
<property
name="repoLoanDayQty"
type="int"
column="REPO_LOAN_DAY_QTY"
length="5"
/>
<property
name="repoRateType"
type="java.lang.String"
column="REPO_RATE_TYPE"
length="1"
/>
<property
name="repoLoanRate"
type="java.lang.Integer"
column="REPO_LOAN_RATE"
length="27"
/>
<property
name="repoInterestAmt"
type="java.lang.Integer"
column="REPO_INTEREST_AMT"
length="28"
/>
<property
name="repoVariableInterest"
type="java.lang.Integer"
column="REPO_VARIABLE_INTEREST"
length="27"
/>
<property
name="repoOpenInd"
type="java.lang.String"
column="REPO_OPEN_IND"
length="1"
/>
<property
name="repoTradeType"
type="java.lang.String"
column="REPO_TRADE_TYPE"
length="3"
/>
<property
name="repoSpread"
type="java.lang.Integer"
column="REPO_SPREAD"
length="27"
/>
<property
name="processAddId"
type="java.lang.String"
column="PROCESS_ADD_ID"
not-null="true"
length="16"
/>
<property
name="processUpdateId"
type="java.lang.String"
column="PROCESS_UPDATE_ID"
not-null="true"
length="16"
/>
<property
name="stampAddDtime"
type="java.sql.Timestamp"
column="STAMP_ADD_DTIME"
not-null="true"
length="7"
/>
<property
name="stampUpdateDtime"
type="java.sql.Timestamp"
column="STAMP_UPDATE_DTIME"
not-null="true"
length="7"
/>

<!-- Associations -->
<!-- derived association(s) for compound key -->
<!-- end of derived association(s) -->

<!-- bi-directional one-to-one association to OdsTran -->
<one-to-one
name="odsTran"
class="com.ods_schema_mapping.hibernate.OdsTran"
outer-join="auto"
constrained="true"
/>

</class>
</hibernate-mapping>

_________________
Cheers,
MikeR


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 19, 2006 10:51 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
It looks fine to me. The only issue is the size of the Key elements - thats a big big big number. I assume you have tried storing small numbers in the datastore first.

In anycase, I am not sure whats wrong. Maybe a silce of code will help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 20, 2006 7:02 am 
Newbie

Joined: Tue Oct 25, 2005 12:28 pm
Posts: 18
Location: London
Yikes, length 38. That was set by default by middlegen.

I've looked through the other mappings I have and the length varies, from 26 to 38 for java.lang.Integer types.

Could this be causing a problem do you think?

I can send some of the generated mappings and code files to you David if you don't mind. I am reluctant to post them on the forum without first obsfucating (and risking masking the problem) the mappings.

I don't want to breach my companies copyright policy and risk being fired for revealing the structure of our schema.

Thanks

_________________
Cheers,
MikeR


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 20, 2006 10:08 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Middlegen reports the length as it gets it from the meta data of the JDBC driver. Maybe a driver issue at play. What database?

You can send it to me directly if you like - understand your concerns. You could also try a small sample table (not related to a company system) to try and reduce it to a smaller working set.


Top
 Profile  
 
 Post subject: how to access composite key class fields in queries?
PostPosted: Thu Feb 02, 2006 11:21 am 
Newbie

Joined: Thu Feb 02, 2006 11:12 am
Posts: 3
david,
can you give an example of using composite keys in queries. For instance, if I have a class myClass and myClassId it's composite key class how can i access access a field in the myClassId class.

A query like " from myClass c where c.id.test_field..." does not seem to work...(id, ofcourse is the field representing myClassId in myClass)

Also, how can we create join associations in this case when the required entities are in myClassId?
thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 02, 2006 9:42 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
A field in the compoisite key is just a 'dot' away. Its a specialised component/embedded so its access the same way.

Joins can be default as a key-many-to-one in the composite key (not my prefered approach) or as a separate many-to-one with update false, insert false attributes. Try them out and see what fits the best.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.