-->
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.  [ 2 posts ] 
Author Message
 Post subject: Using the many-to-one mapping to return just a few fields
PostPosted: Wed May 09, 2012 6:48 pm 
Newbie

Joined: Wed May 09, 2012 6:24 pm
Posts: 2
Hi i could use some help here. I'm using Hibernate to integrate with a Payments and Payees table.
The Payee table has alot of columns and i need them all the mapped to the Payee object. Check.
But when i populate the Payments object i only want Payee's moreStuff field ( b/c i will be serializing the Payment object later on and don't want all the bulk )
So i feel like i need the many-to-one mapping. But i need something to specify that i just want the moreStuff String field not the tonsOfStuff field(s).
Does this make sense? Is there a way to do this with Hibernate?

Code:
<hibernate-mapping package="com.something">
    <class name="Payment" table="Payments" schema="dbo" catalog="APX2000">
        <id name="paymentId" type="int">
            <column name="paymentId" />
            <generator class="native" />
        </id>
        <property name="stuff" type="string">
            <column name="stuff" />
        </property>       
        <many-to-one name="payee" class="Payee" fetch="select" access="property">
            <column name="payeeId" />
        </many-to-one>
    </class>
</hibernate-mapping>

<hibernate-mapping package="com.something">
    <class name="Payee" table="Payees" schema="dbo" catalog="APX2000">
        <id name="payeeId" type="int">
            <column name="payeeId" />
            <generator class="native" />
        </id>
        <property name="moreStuff" type="string">
            <column name="moreStuff" />
        </property>
        <property name="tonsOfStuff" type="string">
            <column name="tonsOfStuff" />
        </property>
    </class>
</hibernate-mapping>

And here's the database schema:
Code:
CREATE TABLE Payments
(
   paymentId int IDENTITY(1,1) NOT NULL PRIMARY KEY,
   stuff varchar(20),
   payeeId int FOREIGN KEY REFERENCES Payees(payeeId),
)

CREATE TABLE Payees
(
   payeeId int IDENTITY(1,1) NOT NULL PRIMARY KEY,
   moreStuff varchar(20),
   tonsOfStuff varchar(20),
)


Top
 Profile  
 
 Post subject: Re: Using the many-to-one mapping to return just a few fields
PostPosted: Thu May 10, 2012 3:42 pm 
Newbie

Joined: Wed May 09, 2012 6:24 pm
Posts: 2
I have been unsuccessful. I tried this in the Payment mapping file:
Code:
       
<join table="Payees">
           <key column="payeeId"/>
           <property name="moreStuff" column="moreStuff"/>
</join>

But it didn't work. So i tried the @SecondaryTables Annotations syntax.
But i got an error saying this only works if you are doing a JOIN between two primary keys.
Now thats weird, its very common in the SQL universe to do a join on a Foreign key to a Primary Key. Perhaps i'm missing something.

Its pretty important for performance to control the fields returned on a JOIN. Returning all of them every time ( i.e. instantiating the whole Class on a many-to-one or one-to-one mapping ) seems very wasteful. Thoughts? Disagreements?


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