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: Mapping Question
PostPosted: Mon Nov 19, 2007 12:30 pm 
Newbie

Joined: Mon Nov 19, 2007 11:10 am
Posts: 3
I have the following tables:

Customer

StupidTableIDontCareAbout

StupidTableIDontCareAbout has columns:

- CustomerID int IDENTITY(1,1)
- CustomerPropertyICareAbout varchar(32)

Now, I want my Customer Business Object to have a CustomerPropertyICareAbout, without needing a Business Object modeling the StupidTableIDontCareAbout.

Is there a way to map this?

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 19, 2007 1:41 pm 
Beginner
Beginner

Joined: Mon Feb 19, 2007 4:22 am
Posts: 22
Location: Poland
I am not sure if I understood you well, but maybe <join> could help.

Look at the documentation:
Quote:
5.1.18: Using the <join> element, it is possible to map properties of one class to several tables

is this what you want to do?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 19, 2007 1:44 pm 
Expert
Expert

Joined: Sat Jan 17, 2004 2:57 pm
Posts: 329
Location: In the basement in my underwear
Use a join table.

An example from our code using annotations is that at the class level we declare:

Code:
@SecondaryTables({@SecondaryTable(name = "WE_PROPERTY_PERIOD", pkJoinColumns = {@PrimaryKeyJoinColumn(name = "WE_PROPERTY_PERIOD_ID", referencedColumnName = "WORKFLOW_EVENT_ID")})})


which sets up the table and tells hibernate what column you're joining on.

And then for the property in question:
Code:
    @ManyToOne(fetch = FetchType.EAGER, optional = false)
    @JoinColumn(name = "LOCATION_PROPERTY_ID", unique = false, nullable = false, insertable = true, updatable = false, table = "WE_PROPERTY_PERIOD")


which denotes the table and column that the property maps to.

_________________
Some people are like Slinkies - not really good for anything, but you still can't help but smile when you see one tumble down the stairs.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 19, 2007 3:16 pm 
Newbie

Joined: Mon Nov 19, 2007 11:10 am
Posts: 3
Okay, I think join is what I want, but I can't find many examples of <join table=""> usage that don't assume that both tables have the same primary key.

Code:
<class name="Shipment" table="shipment" >
    <id name="ID" column="ship_num">
      <generator class="native" />
    </id>

<!--...-->
    <join table="customer" >
      <key>
        <column name="customer_id" >
      </key>
      <property name='CustomerNotes' column='notes'/>
    </join>


This fails because it tries to join customer.customer_id to shipment.ship_num. How can I specify, in a mapping file, the foreign key?[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 19, 2007 5:02 pm 
Beginner
Beginner

Joined: Mon Feb 19, 2007 4:22 am
Posts: 22
Location: Poland
Use property-ref attribute of key.

Code:
<key property-ref="propertyName">


Quote:
5.1.19. key property-ref (optional): Specifies that the foreign key refers to columns that are not the primary key of the orginal table.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 19, 2007 6:22 pm 
Newbie

Joined: Mon Nov 19, 2007 11:10 am
Posts: 3
This mapping (with property-ref)

Code:
<join table="customer" >
      <key property-ref="customer_id" column="customer_id" />
      <property name="CustomerNotes" column="notes"/>
</join> 


Doesn't work (at least not with NHibernate)

SQL Join this generates:

Code:
inner join customer shipment0_1_ on shipment0_.ship_num=shipment0_1_.customer_id


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.