-->
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.  [ 8 posts ] 
Author Message
 Post subject: Hibernate composite key mapping question
PostPosted: Thu Apr 13, 2006 6:00 pm 
Beginner
Beginner

Joined: Mon Mar 27, 2006 3:44 pm
Posts: 26
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:
3.01

Mapping documents:
Account
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.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.maintenance.Account"
table="account"
>
<cache usage="read-only"/>

<id
name="account"
type="java.lang.String"
column="account"
>
<meta attribute="use-in-equals">true</meta>
<generator class="assigned" />
</id>

<property
name="status"
type="java.lang.String"
column="status_"
not-null="true"
length="1"
/>
<property
name="accountType"
type="java.lang.String"
column="account_type"
not-null="true"
length="1"
/>
<property
name="omniType"
type="java.lang.String"
column="omni_type"
not-null="true"
length="1"
/>
<property
name="accntParam"
type="java.lang.String"
column="accnt_param"
not-null="true"
/>
<property
name="accntHeader"
type="java.lang.String"
column="accnt_header"
not-null="true"
/>
<property
name="accntCurr"
type="java.lang.String"
column="accnt_curr"
not-null="true"
/>
<property
name="accntCont"
type="java.lang.String"
column="accnt_cont"
not-null="true"
/>
<property
name="accntCharge"
type="java.lang.String"
column="accnt_charge"
not-null="true"
/>
<sql-query name="getAllAccounts">
<return alias="account" class="com.maintenance.Account"/
>
SELECT {account.*}
FROM Account account
JOIN Dictionary dictionary
ON account.account_type = dictionary.sub_code AND dictionary.dict_code='
ACCT_TYPE'
</sql-query>
<many-to-one
name="dictAccount"
class="Dictionary">
<formula>accountType</formula>
<formula>'ACCT_TYPE'</formula>
</many-to-one>


<!-- Associations -->


</class>
</hibernate-mapping>

Dictionary
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.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.maintenance.Dictionary"
table="dictionary"
>
<cache usage="read-only"/>

<composite-id name="comp_id" class="com.maintenance.DictionaryPK">
<key-property
name="DictCode"
column="dict_code"
/>
<key-property
name="SubCode"
column="sub_code"
/>
</composite-id>

<property
name="codeDesc"
type="java.lang.String"
column="code_desc"
not-null="true"
length="30"
/>
<property
name="subDesc"
type="java.lang.String"
column="sub_desc"
not-null="true"
length="30"
/>
<property
name="updateUser"
type="java.lang.String"
column="update_user"
not-null="true"
length="12"
/>
<property
name="updateDate"
type="java.sql.Timestamp"
column="update_date"
not-null="true"
length="26"
/>

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


</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

accList = session.createQuery( "from Account ")
Full stack trace of any exception that occurs:
[#|2006-04-13T16:46:29.453-0500|INFO|sun-appserver-pe8.1_01|javax.enterprise.system.stream.out|_ThreadID=21;|16:46:29,452 ERROR XMLH
elper:61 - Error parsing XML: XML InputStream(366) The content of element type "class" must match "(meta*,subselect?,cache?,synchron
ize*,comment?,tuplizer*,(id|composite-id),discriminator?,natural-id?,(version|timestamp)?,(property|many-to-one|one-to-one|component
|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,((join*,subclass*)|joined-subclass*|union-subclass*
),loader?,sql-insert?,sql-update?,sql-delete?,filter*,resultset*,(query|sql-query)*)".
|#]

[#|2006-04-13T16:46:29.464-0500|WARNING|sun-appserver-pe8.1_01|javax.enterprise.system.stream.err|_ThreadID=21;|org.hibernate.Mappin
gException: Could not read mappings from resource: com/maintenance/Account.hbm.xml
at org.hibernate.cfg.Configuration.addClass(Configuration.java:505)
at com.maintenance.PersistenceManager.init(PersistenceManager.java:30)
at com.maintenance.AccountBean.ejbCreate(AccountBean.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.sun.ejb.containers.StatelessSessionContainer.createStatelessEJB(StatelessSessionContainer.java:410)
at com.sun.ejb.containers.StatelessSessionContainer.access$100(StatelessSessionContainer.java:75)
at com.sun.ejb.containers.StatelessSessionContainer$SessionContextFactory.create(StatelessSessionContainer.java:597)
at com.sun.ejb.containers.util.pool.NonBlockingPool.getObject(NonBlockingPool.java:168)
at com.sun.ejb.containers.StatelessSessionContainer._getContext(StatelessSessionContainer.java:359)
at com.sun.ejb.containers.BaseContainer.getContext(BaseContainer.java:1072)
at com.sun.ejb.containers.BaseContainer.preInvoke(BaseContainer.java:772)
at com.sun.ejb.containers.EJBObjectInvocationHandler.invoke(EJBObjectInvocationHandler.java:126)
at $Proxy37.getRow(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTie.java:123)
at com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatchToServant(CorbaServerRequestDispatcherImpl.java:6
48)
at com.sun.corba.ee.impl.protocol.CorbaServerRequestDispatcherImpl.dispatch(CorbaServerRequestDispatcherImpl.java:192)
at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequestRequest(CorbaMessageMediatorImpl.java:1709)
at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:1569)
at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleInput(CorbaMessageMediatorImpl.java:951)
at com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:181)
at com.sun.corba.ee.impl.protocol.CorbaMessageMediatorImpl.handleRequest(CorbaMessageMediatorImpl.java:721)
at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.dispatch(SocketOrChannelConnectionImpl.java:473)
at com.sun.corba.ee.impl.transport.SocketOrChannelConnectionImpl.doWork(SocketOrChannelConnectionImpl.java:1262)
at com.sun.corba.ee.impl.orbutil.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:409)
Caused by: org.hibernate.MappingException: invalid mapping
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:424)
at org.hibernate.cfg.Configuration.addClass(Configuration.java:502)
... 30 more
Caused by: org.xml.sax.SAXParseException: The content of element type "class" must match "(meta*,subselect?,cache?,synchronize*,comm
ent?,tuplizer*,(id|composite-id),discriminator?,natural-id?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-
component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,((join*,subclass*)|joined-subclass*|union-subclass*),loader?
,sql-insert?,sql-update?,sql-delete?,filter*,resultset*,(query|sql-query)*)".
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(ErrorHandlerWrapper.java:236)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(ErrorHandlerWrapper.java:172)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:382)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(XMLErrorReporter.java:316)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.handleEndElement(XMLDTDValidator.java:2048)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.endElement(XMLDTDValidator.java:932)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanEndElement(XMLNSDocumentScannerImpl.java:719)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(XMLDocumentFrag
mentScannerImpl.java:1685)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:3
68)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:834)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:764)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:148)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1242)
at org.dom4j.io.SAXReader.read(SAXReader.java:465)
at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:421)
... 31 more
|#]

Name and version of the database you are using:
Ingres 2.6

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


The SQL I need is as follows
select account.* from account a,dictionary d
where a.account_type=d.dict_code
and s.sub_code='ACCT_TYPE'

Can anybody tell me what is wrong with the mapping?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 14, 2006 11:46 am 
Beginner
Beginner

Joined: Mon Mar 27, 2006 3:44 pm
Posts: 26
I have searched the forums but can't find this problem. Has anybody seen this exception?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 11:57 am 
Beginner
Beginner

Joined: Mon Mar 27, 2006 3:44 pm
Posts: 26
I can't believe that this is such a complicated mapping nobody has faced


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 12:13 pm 
Expert
Expert

Joined: Thu Sep 22, 2005 10:29 am
Posts: 285
Location: Almassera/Valencia/Spain/EU/Earth/Solar system/Milky Way/Local Group/Virgo Supercluster
The <sql-query> must be the last element in the mapping file.

You only has to swap order with <many-to-one>.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 5:53 pm 
Beginner
Beginner

Joined: Mon Mar 27, 2006 3:44 pm
Posts: 26
That fixed it. I am able to proceed now. Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 6:19 pm 
Beginner
Beginner

Joined: Mon Mar 27, 2006 3:44 pm
Posts: 26
As you can see from my mapping I have the following line
<many-to-one
name="dictAccount"

I don't have this dictAccount property defined in my java file and I don't have getter and setters for this. Should I define it?
My query is simple
accList = session.createQuery( "from Account ")
Using the named sql query does the job for me but I want to do the same via mapping
I get the following exception now

[#|2006-04-19T16:31:18.268-0500|WARNING|sun-appserver-pe8.1_01|javax.enterprise.
system.stream.err|_ThreadID=11;|org.hibernate.PropertyNotFoundException: Could not find a getter for dictAccount in class com.maintenance.Account
at org.hibernate.property.BasicPropertyAccessor.createGetter(BasicPropertyAccessor.java:282)
at org.hibernate.property.BasicPropertyAccessor.getGetter(BasicPropertyAccessor.java:275)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 1:01 pm 
Beginner
Beginner

Joined: Mon Mar 27, 2006 3:44 pm
Posts: 26
I need all the details only from the account table. I need the dictionary table only for the join. Any idea?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 1:55 pm 
Beginner
Beginner

Joined: Mon Mar 27, 2006 3:44 pm
Posts: 26
Since I am new to hibernate, I bought the hibernate in action book hoping it will have some good legacy examples. It does not help, though it is a pretty good book to get started. Let me know if you guys can help here.


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