-->
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.  [ 6 posts ] 
Author Message
 Post subject: one object mapping to more than one table
PostPosted: Tue Oct 18, 2005 5:41 pm 
Newbie

Joined: Sat Jun 26, 2004 6:01 pm
Posts: 6
Hello,
I have an object whose fields belong to more than one table. I would like to know if and how to map it in the hbm.xml file. Based on a quick pass at the reference doc I'm not able to get any details on it. Could someone lead me with links or code snippets etc I would appreciate your help.

If I'm not clear in my question please let me know.

note: I do not have the luxury of changing the existing db table struture.

Thanks
Pradeep

Hibernate version:
3.1
Name and version of the database you are using:
oracle 9i


Top
 Profile  
 
 Post subject: perhaps
PostPosted: Wed Oct 19, 2005 3:01 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
perhaps you can model something with joined-subclass, where your class will be the subclass:
http://www.hibernate.org/hib_docs/v3/re ... edsubclass
or you could define one-to-one relationships and have properties from another table mapped to a component of your class

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject: one object mapping to more than one table
PostPosted: Thu Nov 10, 2005 4:22 pm 
Newbie

Joined: Sat Jun 26, 2004 6:01 pm
Posts: 6
I have this requirement addressed using

section 8.3.3. one to one in reference doc
A unidirectional one-to-one association on a join table.

also visited http://forum.hibernate.org/viewtopic.php?t=943843&

Thanks
Pradeep


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 10, 2005 4:32 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
Or use the join element of class. It has the advantage that the second table doesn't have to be modelled as a nested object in java.

Code:
<class name="First" table="TFirst">
  <id>
    ...
  </id>

  <property name="DataColInTFirst"/>

  <join table="TSecond">
    <key column="KeyColInTFirst"/>
    <property name="DataColInTSecond"/>
  </join>
</class>


Note that join must come after your other properties, maps, many-to-ones etc in your first class.


Top
 Profile  
 
 Post subject: how to map a complex object to multiple tables
PostPosted: Mon Nov 28, 2005 4:01 pm 
Newbie

Joined: Sat Jun 26, 2004 6:01 pm
Posts: 6
Domain model:
Claim object contains loan object

public class Claim {
private Long workorderId;
private Date responseDate;
private String submittedBy;
private Date claimDate;
private Loan loan;
}

public class Loan {
private String loanNumber;
private String state;
private Date customerChangeDate;
}


I have tables with the below given structure:
table name: workorder
workorder_seq (pk)
responseDate
loanNumber
state
submittedBy

table name: claim
workorder_seq (pk)
claimDate
customerChangeDate

workorder and claim table have one-to-one relationship

I would like to know how to create a mapping file with the above given
domain objects and table structure.

I read about the component and join elements for the mapping file. But I'm unable to figure out how to support the above structure in a mapping file.

Note: requirement is I cannot change either the domain model or the table structure.

Database used is oracle 9i
hibernate version used is 3.1

Thanks
Pradeep


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 28, 2005 4:40 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
I can't find an solution to your very strange problem. You could map Claim to workorder and join in customerChangeDate, then add a one-to-one to Loan, which maps to claim and joins in claimDate, but I don't know if that would work. It's a pity <component> doesn't allow a <join> element, but this problem has got to be very rare, no point in solving it if the solution is only going to be used once.

My solution would be to create a single class ClaimLoanImpl, which implements the Claim and Loan interfaces. Claim.getLoan() returns (Loan) this. Use hibernate to map both tables to ClaimLoanImpl (using join). Undoubtedly there's a better solution; you can't use even this solution if your not-changing-domain-model rule disallows you from changing Claim and Loan to interfaces.


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