-->
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: Storing specific property of a child object instead of child
PostPosted: Thu Mar 25, 2004 12:04 pm 
Newbie

Joined: Tue Mar 09, 2004 2:44 pm
Posts: 15
I was wondering if Hibernate could be configured to handle the following situation. I know of one solution that will probably work the best, but was just curious as to that capabilities of Hibernate.

A person can have many jobs. Each job has a load of information about it (a job object is realtively large). I do not want my user object to hold a Collection of job objects for this reason. However, I would like the user object to hold a Collection of job titles (Strings). The database structure would be a standard user table --< user_jobs >-- job. The job table holds job titles. I am aware of how to map user to jobs, but not how to only pull out only the job_title from the job object.

Also, to add complication, what if I wanted something from a table further joined off of jobs - like the city which is connected through a zip-code lookup table?

The solution I mentioned above would be to create a view that would summarize the information I need, and map that view with a many-to-one in the user mapping. Is this the best/only way to accomplish this?

_________________
Thanks,
CJ


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 25, 2004 7:01 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
http://www.hibernate.org/41.html

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 26, 2004 10:10 am 
Newbie

Joined: Tue Mar 09, 2004 2:44 pm
Posts: 15
I see how that would work, but I like the view solution better. Having a Job object extend JobTitle or having a JobInfo object that simply wraps one String seems unnecessary (although I can't really offer a good reason why).

By sending that link, I assume there is no way to do what I asked in a mapping. Something like:

Code:
<class name="User" table="user">
        <set name="jobtitles" table="user_jobs" type="string">
            <key column="userid" />
            <many-to-many column="jobid" class="HibernateExJob"  property="title"/>
        </set>
        ...

or
    <set name="jobtitles" table="user_jobs" type="string">
        <key column="userid" />
        <many-to-many column="jobid">
            <join-table-property table="job" joincolumn="jobid" column="title">
        </many-to-many>
        ...

_________________
Thanks,
CJ


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 31, 2004 9:52 am 
Newbie

Joined: Tue Mar 09, 2004 2:44 pm
Posts: 15
I went with a view and retrieving the data worked perfectly fine. However, since Hibernate flushes data from memory to the database at certain times, if I update the property being populated from a view, I get an SQL error (non-updatable view) which I totally expect and understand. Is there a way in hibernate to prevent a Collection from being a persisted property? I know the <property> tag has update="false" and insert="false", but I don't see a similar item for a set.

My user object holds a set of String "accesses". I have mapped it as follows:

Code:
<set name="accesses" table="user_accesses">
            <key column="userid" />
            <element column="access" type="java.lang.String" />
        </set>


where the table is actually a view in SQL server.

If I add an access to the in-memory user (temporary access granted), I do not want it saved (and cannot get it saved given this setup). My code does not break when I do this, but an error is logged. Is there a way around this?

_________________
Thanks,
CJ


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 31, 2004 9:54 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
inverse="true"


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 31, 2004 2:46 pm 
Newbie

Joined: Tue Mar 09, 2004 2:44 pm
Posts: 15
Okay, that works fine.
Separately, I have an affiliation object mapped as follows:

Code:
<class name="HibernateExAffiliation" table="userjobs">
     <id column="ujid" name="affId" type="int">
   <generator class="identity" />
     </id>
     <many-to-one name="user" column="usernum" class="HibernateExUser" />
     <many-to-one name="job" column="jobid" class="HibernateExJob" />
     <set name="accesses" table="user_disc">
   <key column="ujid" />
   <element column="disc_id" type="java.lang.String" />
     </set>
</class>


If I set the set "accesses" with inverse="true", the accesses will be read-only and not updated in the database even if they are in memory, correct? I will say I didn't think so, originally, but my attempts to prove my thoughts were fruitless.

Now, let's assume again here I wish to add a temporary access (or delete one) to an affiliation object, say until the user confirms that the change should be made. In order to have a confirmation UI, I must generate a copy of the retrieved affiliation, manipulate/display that version, and then save it to the session (using one "Long Session")? Optionally, I can use two sessions, destroying the original session after retrieval, modifying the retrieved object, opening a new session, and updating it with the original object (which has been altered), right?

And this works fine as long as I don't evict the parent from the cache. Doing so causes all the children (accesses) to be deleted from the database. Is there a way to evict an ENTIRE object, or do I somehow have to evict an objects children, then the object (or does that cause deltion problems, as well)?

Most of the examples I saw in the documentation refered to <many-to-many> and <many-to-one> sets, not <elements>.

_________________
Thanks,
CJ


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.