-->
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: Many-to-many mapping using non-primary key columns
PostPosted: Mon Mar 02, 2009 8:57 am 
Newbie

Joined: Fri Feb 27, 2009 6:42 pm
Posts: 10
Hi guys,

I have two tables -- INSTALL_BUILD_RESULTS and BUILD_RESULTS. IBR has a unique column (non-primary key) called Install_Build_Id and BR has a unique column (non-primary key again) called Build_Id. Both are varchars.

A third table, LINK_BUILD_TO_INSTALL_BUILD is a table simply consisting of the two columns mentioned above, used together as a composite key.

Could somebody check my XMLs for the classes and see if there's an error? Any time I try to run a query, I'm receiving a DataException --

BuildResult.hbm.xml --
<hibernate-mapping>
<class name="persistence.BuildResult" table="BUILD_RESULT" schema="dbo" catalog="MSSRELENG_NEWER">
....
<property name="buildId" type="java.lang.String">
<column name="Build_Id" not-null="true" unique="true" />
</property>
....
<set name="installBuildResults" table="LINK_BUILD_TO_INSTALL_BUILD" cascade="all">
<key column="Build_Id" />
<many-to-many column="Install_Build_Id" property-ref="installBuildId" class="persistence.InstallBuildResult" />
</set>
</class>
</hibernate-mapping>

InstallBuildResult.hbm.xml --
<hibernate-mapping>
<class name="persistence.InstallBuildResult" table="INSTALL_BUILD_RESULT" schema="dbo" catalog="MSSRELENG_NEWER">
....
<property name="installBuildId" type="java.lang.String">
<column name="Install_Build_Id" not-null="true" unique="true" />
</property>
....
<set name="buildResults" table="LINK_BUILD_TO_INSTALL_BUILD" cascade="all">
<key column="Install_Build_Id" />
<many-to-many column="Build_Id" property-ref="buildId" class="persistence.BuildResult" />
</set>
</class>
</hibernate-mapping>

and lastly, LinkBuildToInstallBuild.hbm.xml --
<hibernate-mapping>
<class name="persistence.LinkBuildToInstallBuild" table="LINK_BUILD_TO_INSTALL_BUILD" schema="dbo" catalog="MSSRELENG_NEWER">
<composite-id name="id" class="persistence.LinkBuildToInstallBuildId">
<key-many-to-one name="buildResult" class="persistence.BuildResult">
<column name="Build_Id" />
</key-many-to-one>
<key-many-to-one name="installBuildResult" class="persistence.InstallBuildResult">
<column name="Install_Build_Id" />
</key-many-to-one>
</composite-id>
<many-to-one name="buildResult" class="persistence.BuildResult" update="false" insert="false" fetch="select">
<column name="Build_Id" not-null="true" />
</many-to-one>
<many-to-one name="installBuildResult" class="persistence.InstallBuildResult" update="false" insert="false" fetch="select">
<column name="Install_Build_Id" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>

Simple test code trying to get some results from the db --
LinkBuildToInstallBuildDAO lbtip = new LinkBuildToInstallBuildDAO();
Session hibernateSession = lbtip.getSession();
Criteria crit = hibernateSession.createCriteria(LinkBuildToInstallBuild.class);
crit.list();

errors out with -- >

org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:77)
org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
org.hibernate.loader.Loader.doList(Loader.java:2223)
org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
org.hibernate.loader.Loader.list(Loader.java:2099)
org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
beans.vijalTests.main(vijalTests.java:14)

Any tips, suggestions?

Regards,
Vijal


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 02, 2009 9:28 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Like you have used property-ref in the many-to-many mappings in BuildResult and InstallBuildResult, you should use the property-ref in the many-to-one mappings of LinkBuildToInstallBuild also. But the <key-many-to-one> tag doesn't seems to have a property-ref attribute. So I think you will have to use <key-property> for the composite ids and with the <many-to-one> you can link to the objects. Like:
Code:
<class name="persistence.LinkBuildToInstallBuild" table="LINK_BUILD_TO_INSTALL_BUILD" schema="dbo" catalog="MSSRELENG_NEWER">
    <composite-id name="id" class="persistence.LinkBuildToInstallBuildId">
        <key-property name="buildId">
            <column name="Build_Id" />
        </key-property>
        <key-property name="installBuildId">
            <column name="Install_Build_Id" />
        </key-property>
    </composite-id>
    <many-to-one name="buildResult" class="persistence.BuildResult" property-ref="buildId" update="false" insert="false" fetch="select">
        <column name="Build_Id" not-null="true" />
    </many-to-one>
    <many-to-one name="installBuildResult" class="persistence.InstallBuildResult" property-ref="installBuildId" update="false" insert="false" fetch="select">
        <column name="Install_Build_Id" not-null="true" />
    </many-to-one>
</class>

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 02, 2009 11:17 am 
Newbie

Joined: Fri Feb 27, 2009 6:42 pm
Posts: 10
Hi Litty,

Thank you for the quick response but I'm still having difficulties.

Quote:
Queries cannot be excuted.

Reason:
field [buildId] not found on persistence.LinkBuildToInstalllBuildId


my LinkBuildToInstallBuildId only references BuildResult buildResult and InstallBuildResult installBuildResult in the constructor, hence the difficulties.

I'm hoping it can pick up these objects based on the buildId and installBuildId because they're unique values in the originall Link_Build_To_Install_Build table.

Any idea where to go from here?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 02, 2009 12:02 pm 
Newbie

Joined: Fri Feb 27, 2009 6:42 pm
Posts: 10
Not sure if this helps any but here's what I was trying to imitate when I was building my original hbm.xml file for the LinkBuildToBuildResult table --

http://forum.hibernate.org/viewtopic.ph ... manytomany


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 03, 2009 12:47 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Quote:
field [buildId] not found on persistence.LinkBuildToInstalllBuildId

This means that your LinkBuildToInstalllBuildId class does not have a field buildId.


Quote:
my LinkBuildToInstallBuildId only references BuildResult buildResult and InstallBuildResult installBuildResult in the constructor, hence the difficulties.

No. LinkBuildToInstallBuildId is just a identifier class. It need not refer to the BR and IBR objects. These references you can put in the LinkBuildToInstallBuild object which is your actual persistent object.

So ur Id class will look like:

Code:
public class LinkBuildToInstallBuildId implements Serializable {
     long buildId;
     long installBuildId;
//Getter Setters
}

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 03, 2009 11:16 am 
Newbie

Joined: Fri Feb 27, 2009 6:42 pm
Posts: 10
Litty, it works like a charm. Thank you!! :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Mar 03, 2009 1:43 pm 
Newbie

Joined: Fri Feb 27, 2009 6:42 pm
Posts: 10
I hope it's alright to still be on this thread -- so when I go to map another table along with using the InstallBuildResult table mentioned above, running queries results in the following error:

Exception in thread "main" java.lang.NullPointerException
at org.hibernate.persister.entity.AbstractEntityPersister.loadByUniqueKey(AbstractEntityPersister.java:1645)
at org.hibernate.type.EntityType.loadByUniqueKey(EntityType.java:608)
at org.hibernate.type.EntityType.resolve(EntityType.java:382)

To elaborate more specifically, I have a second table LinkInstallBuildResultToSmoketestResult which I set up identical to the way I have the tables set up above. This connects the non-primary key installBuildId w/ another non-primary key smoketestRunId in its table, and even executing simple queries such as "from LinkInstallBuildResultToSmoketestResult" results in the error above. Thoughts?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 04, 2009 12:31 am 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Plz post the mapping of tht entity as well.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 04, 2009 11:39 am 
Newbie

Joined: Fri Feb 27, 2009 6:42 pm
Posts: 10
Updated InstallBuildResult --
<class name="persistence.InstallBuildResult" table="INSTALL_BUILD_RESULT" schema="dbo" catalog="MSSRELENG_NEWER">
<id name="installBuildResultId" type="java.lang.Integer">
<column name="Install_Build_Result_Id" />
<generator class="assigned" />
</id>
<many-to-one name="productRelease" class="persistence.ProductRelease" fetch="select">
<column name="Release_Id" />
</many-to-one>
<many-to-one name="installBuildProject" class="persistence.InstallBuildProject" fetch="select">
<column name="Install_Build_Project_Id" not-null="true" />
</many-to-one>
<many-to-one name="operationalStatus" class="persistence.OperationalStatus" fetch="select">
<column name="Result_Status_Id" />
</many-to-one>
<many-to-one name="machine" class="persistence.Machine" fetch="select">
<column name="Host_Id" not-null="true" />
</many-to-one>
<many-to-one name="runningQue" class="persistence.RunningQue" fetch="select">
<column name="Running_Que_Id" />
</many-to-one>
<many-to-one name="archiveStatus" class="persistence.ArchiveStatus" fetch="select">
<column name="Archive_Status_Id" not-null="true" />
</many-to-one>
<property name="installBuildId" type="java.lang.String">
<column name="Install_Build_Id" not-null="true" unique="true" />
</property>
<property name="duration" type="java.lang.Double">
<column name="Duration" precision="53" scale="0" />
</property>
<property name="isForced" type="java.lang.Boolean">
<column name="IsForced" not-null="true" />
</property>
<property name="isPromoted" type="java.lang.Boolean">
<column name="IsPromoted" not-null="true" />
</property>
<property name="lastModified" type="java.sql.Timestamp">
<column name="Last_Modified" length="23" not-null="true" />
</property>
<property name="postingDirectory" type="java.lang.String">
<column name="Posting_Directory" length="1024" />
</property>
<property name="runResultsLink" type="java.lang.String">
<column name="Run_Results_Link" />
</property>
<property name="startTime" type="java.sql.Timestamp">
<column name="Start_Time" length="23" not-null="true" />
</property>
<property name="endTime" type="java.sql.Timestamp">
<column name="End_Time" length="23" not-null="true" />
</property>
<set name="smoketestResults" table="LINK_INSTALL_BUILD_RESULT_TO_SMOKETEST_RESULT" cascade="all">
<key column="Install_Build_Id" />
<many-to-many column="SmokeTest_Run_Id" property-ref="smokeTestRunId" class="persistence.SmoketestResult" />
</set>
<set name="buildResults" table="LINK_BUILD_TO_INSTALL_BUILD" cascade="all">
<key column="Install_Build_Id" />
<many-to-many column="Build_Id" property-ref="buildId" class="persistence.BuildResult" />
</set>
</class>

SmokeTestResult
<class name="persistence.SmoketestResult" table="SMOKETEST_RESULT" schema="dbo" catalog="MSSRELENG_NEWER">
<id name="smokeTestRunId" type="java.lang.String">
<column name="SmokeTest_Run_Id" />
<generator class="assigned" />
</id>
<many-to-one name="operationalStatus" class="persistence.OperationalStatus" fetch="select">
<column name="Result_Status_Id" />
</many-to-one>
<many-to-one name="machine" class="persistence.Machine" fetch="select">
<column name="Host_Id" not-null="true" />
</many-to-one>
<many-to-one name="runningQue" class="persistence.RunningQue" fetch="select">
<column name="Running_Que_Id" />
</many-to-one>
<many-to-one name="archiveStatus" class="persistence.ArchiveStatus" fetch="select">
<column name="Archive_Status_Id" not-null="true" />
</many-to-one>
<many-to-one name="version" class="persistence.Version" fetch="select">
<column name="version_Id" />
</many-to-one>
<property name="smokeTestResultId" type="java.lang.Integer">
<column name="SmokeTest_Result_Id" not-null="true" />
</property>
<property name="duration" type="java.lang.Double">
<column name="Duration" precision="53" scale="0" />
</property>
<property name="isForced" type="java.lang.Boolean">
<column name="IsForced" />
</property>
<property name="isPromoted" type="java.lang.Boolean">
<column name="IsPromoted" />
</property>
<property name="lastModified" type="java.sql.Timestamp">
<column name="Last_Modified" length="23" not-null="true" />
</property>
<property name="postingDirectory" type="java.lang.String">
<column name="Posting_Directory" />
</property>
<property name="runResultsLink" type="java.lang.String">
<column name="Run_Results_Link" />
</property>
<property name="startTime" type="java.sql.Timestamp">
<column name="Start_Time" length="23" />
</property>
<property name="endTime" type="java.sql.Timestamp">
<column name="End_Time" length="23" />
</property>
<set name="smoketestResultStatuses" inverse="true">
<key>
<column name="SmokeTest_Run_Id" not-null="true" />
</key>
<one-to-many class="persistence.SmoketestResultStatus" />
</set>
<set name="installBuildResults" table="LINK_INSTALL_BUILD_RESULT_TO_SMOKETEST_RESULT" cascade="all">
<key column="SmokeTest_Run_Id" />
<many-to-many column="Install_Build_Id" property-ref="installBuildId" class="persistence.InstallBuildResult" />
</set>
</class>

LinkInstallBuildResultToSmoketestResult
<class name="persistence.LinkInstallBuildResultToSmoketestResult" table="LINK_INSTALL_BUILD_RESULT_TO_SMOKETEST_RESULT" schema="dbo" catalog="MSSRELENG_NEWER">
<composite-id name="id" class="persistence.LinkInstallBuildResultToSmoketestResultId">
<key-property name="installBuildId">
<column name="Install_Build_Id" />
</key-property>
<key-property name="smoketestRunId">
<column name="SmokeTest_Run_Id" />
</key-property>
</composite-id>
<many-to-one name="installBuildResult" class="persistence.InstallBuildResult" property-ref="installBuildId" update="false" insert="false" fetch="select">
<column name="Install_Build_Id" not-null="true" />
</many-to-one>
<many-to-one name="smoketestResult" class="persistence.SmoketestResult" property-ref="smokeTestRunId" update="false" insert="false" fetch="select">
<column name="SmokeTest_Run_Id" not-null="true" />
</many-to-one>
</class>

when I go to execute an HQL, I receive the nullpointerexception


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 04, 2009 11:58 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Try the following:
Code:
   <class name="persistence.LinkInstallBuildResultToSmoketestResult"
      table="LINK_INSTALL_BUILD_RESULT_TO_SMOKETEST_RESULT" schema="dbo"
      catalog="MSSRELENG_NEWER">
      <composite-id name="id"
         class="persistence.LinkInstallBuildResultToSmoketestResultId">
         <key-many-to-one name="installBuildResult" class="persistence.InstallBuildResult" column="Install_Build_Id"/>

         <key-many-to-one name="smoketestResult" class="persistence.SmoketestResult" column="SmokeTest_Run_Id"/>
      </composite-id>
   </class>

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: Many-to-many mapping using non-primary key columns
PostPosted: Tue Nov 27, 2012 5:05 am 
Newbie

Joined: Tue Nov 27, 2012 5:03 am
Posts: 1
Could you post the annotated version of this mapping please :)


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.