-->
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.  [ 21 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Using primary key classes with hibernate
PostPosted: Fri Sep 05, 2003 7:18 pm 
Beginner
Beginner

Joined: Tue Sep 02, 2003 12:15 pm
Posts: 33
For performance reasons, I set up all of my value objects with primary keys to handle the key definition as well as the 1..* relationships within the value objects. We had just waaaaaaay too much data coming back and lazy loading was out of the question. Anyway, I'm not completely sure how to map the key into the vo with the hbm. Also, when I do a load with the session, do I just pass in the key or do I need to do something else? Thanks in advance.

Here's my hbm file, key class, vo, and dao.

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.tab.core.shared.vo.Debtor" table="Debtor">
<id name="key" type="com.tab.core.shared.vo.DebtorPK" column="debtor_id">
<generator class="assigned"/>
</id>
<property name="comment" column="comment" type="string"/>
<property name="numInvoices" column="no_inv" type="integer"/>
<property name="state" column="state" type="string"/>
<property name="sic" column="sic" type="string"/>
<property name="phone" column="tele" type="string"/>
<property name="street2" column="street2" type="string"/>
<property name="street1" column="street1" type="string"/>
<property name="duns" column="duns" type="string"/>
<property name="averagePay" column="avg_pay" type="double"/>
<property name="status" column="status" type="string"/>
<property name="country" column="country" type="string"/>
<property name="city" column="city" type="string"/>
<property name="contact" column="contact" type="string"/>
<property name="creditLimit" column="credit_lim" type="double"/>
<property name="zip" column="zip" type="string"/>
<property name="fax" column="fax" type="string"/>
<property name="name2" column="name2" type="string"/>
<property name="contactPhone" column="c_tele" type="string"/>
<property name="name" column="name1" type="string"/>
</class>
</hibernate-mapping>



*********************************************************
Here's the Key class

public final class DebtorPK extends CacheableKey implements Serializable
{
private String debtorId;

public DebtorPK()
{
setControllerClass("DebtorDAO.class");
setDependentClass(Debtor.class);
}

public DebtorPK(final String debtorId)
{
this();
this.debtorId = debtorId;
}

public String getDebtorId()
{
return debtorId;
}

public void setDebtorId(final String debtorId)
{
this.debtorId = debtorId;
}
}

************************************************************
The value object is just the standard getters/setters.

************************************************************
Here's a snipped out of the HibernateDAO

public final MutableVO findByPrimaryKey(Key obj) throws Exception
{
Session session = getSession();
MutableVO retVal = (MutableVO)session.load(obj.getDependentClass(), obj);
closeSession(session);
return retVal;
}


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 06, 2003 7:42 pm 
Beginner
Beginner

Joined: Tue Sep 02, 2003 12:15 pm
Posts: 33
Well, I found an example like this. I'll try this when I get to work and see if this is the way to do it.


<id name="key" type="com.tab.core.shared.vo.DebtorPK" >
<key-property name="debtorId" type="string" column="debtor_id"/>
</id>


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 06, 2003 8:10 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
That is not a valid mapping. <key-property> is a subelement of <composite-id>, not <id>.


Top
 Profile  
 
 Post subject: Whats my best option?
PostPosted: Sun Sep 07, 2003 12:42 am 
Beginner
Beginner

Joined: Tue Sep 02, 2003 12:15 pm
Posts: 33
gavin wrote:
That is not a valid mapping. <key-property> is a subelement of <composite-id>, not <id>.


How do I map the primary key column(s) to the key class? I am probably missing some good examples. Do you know of any or can you give me a way to do this properly?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2003 12:44 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Perhaps you just wish to use the standard "custom identifier type" pattern from the wiki?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2003 12:49 am 
Beginner
Beginner

Joined: Tue Sep 02, 2003 12:15 pm
Posts: 33
I'll check it out. Sorry if this is a question that has been answered before. I am new to hibernate and I really would like to use it instead of the straight jdbc I am currently using. Thanks for the help.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2003 12:55 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
As outlined in the documentation, you could utilize a component composite key.
Code:
<class name="com.tab.core.shared.vo.Debtor" table"Debtor">
    <composite-id name="key" class="com.tab.core.shared.vo.DebtorPK">
        <key-property name="debtorId" column="debtor_id" type="string"/>
    </composite-id>
    ....
</class>


Check out the section on this usage, as there are a lot of considerations to tis type of implementation.
http://www.hibernate.org/hib_docs/reference/html/components.html#components-s2-3


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2003 1:00 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Or what steve said ;)

Either one should work.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2003 1:02 am 
Beginner
Beginner

Joined: Tue Sep 02, 2003 12:15 pm
Posts: 33
I think you hit the nail on the head with the composite identifier. That looks like exactly what I was looking for. I must have missed it when I was reading through all the docs. I will give it a try on monday when I get in to work. Thanks for all the help. I'll be back if I get messed up again... DOH!!


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2003 1:07 am 
Beginner
Beginner

Joined: Tue Sep 02, 2003 12:15 pm
Posts: 33
I think the custom identifier will work well with some of the legacy conversions we have to make. We tap into some old databases that stored dates as integers and other weird items like that. We currently convert them as we pull them out of the database so the programmer knows the correct datatype they are dealing with. The database names aren't always intuitive enough to know that an int is really a date among other things unless you are familiar with the system. As for the keys, I think it might be hard to write identifiers for all of my keys. Thats extra work I'm trying to avoid. I'll most likely go the composite identifer or composite key approach for my primary key classes and the custom identifier for the other type conversions I need to do. The more I'm learning about hibernate, the more I like it. I didn't realize just how flexible it is. Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2003 2:01 am 
Beginner
Beginner

Joined: Tue Sep 02, 2003 12:15 pm
Posts: 33
Hmmmm... I think I got a bit confused again. Is there anything like the custom identifier that I can use to do type conversions on the property of a value object? Seems they used ints for everything in one of the databases. I need to convert some ints to Dates, int to Double, and some others. Is this possible?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2003 2:07 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Sure. This is a typical use case for a UserType.


Top
 Profile  
 
 Post subject: I have a new issue
PostPosted: Fri Sep 12, 2003 12:43 pm 
Beginner
Beginner

Joined: Tue Sep 02, 2003 12:15 pm
Posts: 33
I have some primary key fields in the database that are serial type. How can I map these to my composite-id? Here's what a sample mapping file of mine looks like. The addendem_id is a serial type in the database. When I try to do an insert, I get an error saying the primary key field is null.

Thanks in advance.

Nic


<hibernate-mapping>
<!-- com.tab.core.shared.vo.MC_AchAddendem root -->
<class name="com.tab.core.shared.vo.MC_AchAddendem" table="ach_addendem">
<composite-id name="key" class="com.tab.core.shared.vo.MC_AchAddendemPK">
<key-property name="id" column="addendem_id" type="integer"/>
</composite-id>
<property name="sequenceNumber" column="sequence_num" type="integer"/>
<component name="returnCode" class="com.tab.core.shared.vo.MC_AchReturnCodePK">
<property name="returnCode" column="return_code" type="string"/>
</component>
<component name="achTransaction" class="com.tab.core.shared.vo.MC_AchTransactionPK">
<property name="achId" column="ach_id" type="integer"/>
</component>
<property name="data" column="data" type="string"/>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: I have a new issue
PostPosted: Fri Sep 12, 2003 12:45 pm 
Beginner
Beginner

Joined: Tue Sep 02, 2003 12:15 pm
Posts: 33
I have some primary key fields in the database that are serial type. How can I map these to my composite-id? Here's what a sample mapping file of mine looks like. The addendem_id is a serial type in the database. When I try to do an insert, I get an error saying the primary key field is null.

Thanks in advance.

Nic


<hibernate-mapping>
<!-- com.tab.core.shared.vo.MC_AchAddendem root -->
<class name="com.tab.core.shared.vo.MC_AchAddendem" table="ach_addendem">
<composite-id name="key" class="com.tab.core.shared.vo.MC_AchAddendemPK">
<key-property name="id" column="addendem_id" type="integer"/>
</composite-id>
<property name="sequenceNumber" column="sequence_num" type="integer"/>
<component name="returnCode" class="com.tab.core.shared.vo.MC_AchReturnCodePK">
<property name="returnCode" column="return_code" type="string"/>
</component>
<component name="achTransaction" class="com.tab.core.shared.vo.MC_AchTransactionPK">
<property name="achId" column="ach_id" type="integer"/>
</component>
<property name="data" column="data" type="string"/>
</class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject: Generator needed for composit-id element
PostPosted: Fri Sep 12, 2003 12:52 pm 
Beginner
Beginner

Joined: Tue Sep 02, 2003 12:15 pm
Posts: 33
From reading your old forum, it looks like you were going to allow a generator element to be placed within the <composite-id> tag. Is this still coming? Is this what I need?

Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 21 posts ]  Go to page 1, 2  Next

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.