File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes hibernate composite key mapping question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "hibernate composite key mapping question" Watch "hibernate composite key mapping question" New topic
Author

hibernate composite key mapping question

sai jay
Greenhorn

Joined: Apr 14, 2006
Posts: 2
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 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?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


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)*)".


There is your cause. Your mapping for Account does not match the DTD. Fix that and it should be fine.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
sai jay
Greenhorn

Joined: Apr 14, 2006
Posts: 2
I fixed the exception. Still it does not do the sql I need. Can you guide me here with the mapping?
Sebastian Hennebrueder
Ranch Hand

Joined: Sep 09, 2004
Posts: 49
select account.* from account a,dictionary d
where a.account_type=d.dict_code
and s.sub_code='ACCT_TYPE'

You do not define/alias the s in s.su....

In adition you do not make use of HQL.
Something like
select a from Account a where a.dictionary.sub_code = ?
would be more HQL like.

Regards Sebastian


Things get always more complicated as expected.<br />Author of eBook Hibernate 3 Developer Guide by example<br />Tutorials about Hibernate, EJB, Struts, JSF <a href="http://www.laliluna.de" target="_blank" rel="nofollow">www.laliluna.de</a>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: hibernate composite key mapping question