-->
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.  [ 10 posts ] 
Author Message
 Post subject: Data mapping based on current user
PostPosted: Fri Oct 21, 2005 9:03 am 
Beginner
Beginner

Joined: Thu Jun 02, 2005 9:36 am
Posts: 20
I want to get data based on the current user.

Let's say I have a Something object that has an id, name, description. It also has a field called "extra" as well. The id, name, description all come from the Something table so that's easy and no problem. The "extra" column comes from a table that has a somethingId and a userId and a third column which is the data I want populated in the Something object.

So in my application, I want to go get all the Something objects. Somehow I need to tell it what the current userId is so it can use that in the join to get the "extra" column.

Any ideas on how something like this could be done?

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 9:22 am 
Beginner
Beginner

Joined: Tue Nov 25, 2003 3:33 pm
Posts: 35
Its time you take a look at filters :-)

they allow you to do this kind of stuff.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 12:42 pm 
Beginner
Beginner

Joined: Thu Jun 02, 2005 9:36 am
Posts: 20
Thanks for the information. I'm trying to get a filter to do it, but it's not working. I've posted a new question to try to figure it out:

http://forum.hibernate.org/viewtopic.php?t=949146

Thank you.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 4:34 pm 
Beginner
Beginner

Joined: Thu Jun 02, 2005 9:36 am
Posts: 20
Okay, so I got the filter working, but now I'm back to the question of how to pull in the other field.

This is what I've been trying:

<class name="pkg.Something">
<id name="id" column="id" access="field">
<generator class="increment" />
</id>
<property name="name" />
<join inverse="true" table="Extra">
<key foreign-key="target" />
<property name="extra" insert="false" update="false" />
</join>
</class>

I just want to tack on this single extra field "extra" whenever I read in a Something object (so no insert or update). It's over on the Extra table which has a foreign-key back to Something called target. How do I get that field in? Right now, I'm getting an error because it's looking for extra on the Something table instead of the Extra table.

Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 4:58 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
If you have two Objects with a many-to-many relationship, and the association object contains additional (Extra) data - which is defined for the relationship itself, the correct way to map this would be to treat the association table as a top-level object.

Object A and B each have a one-to-many to AB and AB has 2 many-to-one associations - one to A and one to B.
Code:
<class name="A"...
  <id name="id" column="id" access="field">
   <generator class="increment" />
  </id>
  ...
  <set name="abs">
    <key column="A_ID"/>
    <one-to-many class="AB"/>
  </set>
</class>


<class name="B"...>
  <id name="id" column="id" access="field">
   <generator class="increment" />
  </id>
  ...
  <set name="abs>
    <key column="B_ID"/>
    <one-to-many class="AB"/>
  </set>
</class>
 

<class name="AB"...>
  ...
<many-to-one name="A" column="A_ID"/>
<many-to-one name="B" column="B_ID"/>
</class>


You would need to determine the proper inverse="" and cascade="" options for your particular model.

Section 7.3.3 Ternary Associations mentions this briefly "A second approach is to simply remodel the association as an entity class. This is the approach we use most commonly."
and also provides two alternatives.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 5:49 pm 
Beginner
Beginner

Joined: Thu Jun 02, 2005 9:36 am
Posts: 20
Sorry, that's not what I meant. I'm not dealing with a relationship necessarily. I just want to include a column from another table. I don't want the column involved in inserts or updates.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 24, 2005 9:04 am 
Beginner
Beginner

Joined: Thu Jun 02, 2005 9:36 am
Posts: 20
Well, it's not necessarily ideal, but I did find the formula attribute:

<property name="accessMask" formula="COALESCE((SELECT e.extra FROM Extra e WHERE e.target = id), 0)" />

And that seems to work fine.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 25, 2005 8:07 am 
Beginner
Beginner

Joined: Thu Jun 02, 2005 9:36 am
Posts: 20
So I go through all that trouble to figure out filters and now find out that you can't put a filter on a subclass. I'm using union-subclasses for pretty much everything.

Oh well... so I have to figure out a different way to do it I guess.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 25, 2005 8:18 am 
Beginner
Beginner

Joined: Thu Jun 02, 2005 9:36 am
Posts: 20
Wait. If I put a filter on the superclass, then all subclasses have the filter applied, right? That seems to be what's happening and that will work fine for me.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 28, 2005 9:50 am 
Beginner
Beginner

Joined: Thu Jun 02, 2005 9:36 am
Posts: 20
Ack! You can't put a filter on a many-to-one relationship. Ugh. I need every access to every entity to be filtered. Everything no matter how it's accessed. Is there an easy shortcut way to make that happen? All my objects inherit from Entity both in Java code and in the hibernate mappings. The problem is that putting a filter in Entity doesn't get used when traversing object relationships. So I put filters in every relationship, but now I find that for at least one of my relationships--I can't put a filter in.

I would have thought what I'm trying to do is fairly normal. I'm just trying to put in such that the current user can only access what they have permission on. Has anyone done that? Is there a way to do it?

Thanks.


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