-->
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: How to map an attribute that is "sort of" a collec
PostPosted: Mon Aug 16, 2004 10:07 am 
Beginner
Beginner

Joined: Thu Feb 26, 2004 11:45 am
Posts: 46
I'm in the midst of mapping the beginnings of a fairly comprehensive domain model. Having good luck, have worked in several tables and at least one of our packages in the domain.

Now a perhaps silly question that i hope somebody can just nudge me in the right direction.


I have a Client class that has names, addresses, points of contacts and collections of all sorts of stuff. And i have it all working. However i have one seemingly simple attribute that i can't seem to get my head around.

Simply this.

Client contains a PrivilegePlan (our own class). PrivilegePlan has several little helper methods but in the end, the intereresting persistant portion of this object is a collection of "Features", which are just integers. The presence or absence of these integers indicate what the client can do.

There isn't really a PrivilegePlan table that makes sense. Privileges in the absence of who owns them aren't very interesting. And we don't have "named" privileged plans, since the combinations are limitless.

So, how do i map this? The client attribute is getPlan and setPlan (there isn't a call to get a collection of integers at the client level). PrivilegePlan is meant to be selfcontained.

So, visually, i can see how it would map to a table. Columns might be ClientID,FeatureID, and that's it.

But how do i define the mapping file? I've thought of:

Adding a get/setPrivs at the client level which sets and gets a "Set". That would work, but i'd rather not add a hibernate helper method into a class for only that purpose.

I suppose i could try to have PrivilegePlan implement Set, not sure what all methods i would need to support. Or i could have PrivilegePlan extend a concrete Set class, but i try to be picky about what i extend since i can only pick one (in Java).

I'm hoping that this is an ordinary scenario and there is something quite simple i'm sort of missing. Thanks for any input.


I'm not getting any errors or anything like that, so i'm not sure that any information other than i'm using 2.12 is helpful at this point.

Hibernate version:2.12

Mapping documents:

Code between sessionFactory.openSession() and session.close():

Full stack trace of any exception that occurs:

Name and version of the database you are using:

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 16, 2004 10:09 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
The helper method is actually not a bad idea - if you want to keep your code clean you can use a custom PropertyAccssor which transforms to/from a Set/Map


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 16, 2004 10:10 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Can you reformulate your problem
I haven't really understand it. (post a simple POJO model)

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 16, 2004 12:56 pm 
Beginner
Beginner

Joined: Thu Feb 26, 2004 11:45 am
Posts: 46
Quote:
The helper method is actually not a bad idea - if you want to keep your code clean you can use a custom PropertyAccssor which transforms to/from a Set/Map


I've added the helper method and added a set to the mapping file, and it appears to work fine. I probably should not worry so much about the helper method, i just get a little anal sometimes. It's just that i've got a bunch of engineers who will use any methods that exist, and then i have to live with them. As it is, i've had to add helper methods to handle marshalling and unmarshalling our whole domain so we can distribute our objects using SOAP/XML. So I suppose i've already moved in that direction, but i'm constantly trying to find ways to get rid of these helper methods, and not add new ones.

I will look into the custom PropertyAccessor to see if that will help me out.

Quote:
Can you reformulate your problem
I haven't really understand it. (post a simple POJO model)


I'd be happy to, thank you for taking the time and interest to read my post. I've cut these way way back. The Client that i use has many many attributes and lists of contacts, etc etc. However, for now, i'm interested in the PrivPlan. Basically PrivPlan is a set of integers. So, really i need to be able to store and retrieve a set of integers for this given clientid. Presumably it would be a separate table with PartyID,FeatureID as a key.

The mapping file below assumes that i add a method in SmallClient basically like below where the set will contain the collection of integers.
Code:
public Set getPrivs();
public void setPrivs(Set privs);


However, I was wondering how to this without adding a helper method in SmallClient that returns a List or Set of integers.

Code:
public class SmallClient {

  private String Name;
  private PrivPlan privilegePlan;
  private long ClientID;

  public void setPrivilegePlan(PrivPlan privilegePlan) {
    this.privilegePlan = privilegePlan;
  }

  public void setClientID(long ClientID) {
    this.ClientID = ClientID;
  }

  public PrivPlan getPrivilegePlan() {
    return privilegePlan;
  }

  public long getClientID() {
    return ClientID;
  }

  public void setName(String Name) {
    this.Name = Name;
  }

  public String getName() {
    return Name;
  }


public class PrivPlan   {
  private Set featurePriv = Collections.synchronizedSet(new HashSet());

  public PrivPlan() {
  }

  public void addPrivilege(int featureID){
    featurePriv.add(new Integer(featureID));
  }
  public void removePrivilege(int featureID){
    featurePriv.remove(new Integer(featureID));
  }
  public void clearPrivilegePlan(){
    featurePriv.clear();
  }
  public boolean isPrivileged(int featureID){
    return featurePriv.contains(new Integer(featureID));
  }
}


The Mapping File for SmallClient:

               <subclass name="SmallClient"  discriminator-value="SmallClient">
                  <property name="name" />
                     </array>
                  <set name="privs" table="CLIENT_PRIVS">
                  <key column="PartyID"/>
                  <element column="FeatureID" type="integer"/>
                  </set>                   
                   
               </subclass>




This is all very helpful. I actually have what i think is a similar problem to solve next. A "FeePlan". In this case i have a collection of FeeObjects that have several attributes to them. Amount, Feature, Category. This collection is also tied to the Client. I'm looking mapping it using a set with composite-element within it. Hoping that that will do the trick for me.

Again, thanks to both of you for your help.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 16, 2004 1:05 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
If you are happy with how an extra accessor method works and want to avoid it, a custom PropertyAccessor is exactly the right thing to use.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Aug 17, 2004 6:33 pm 
Beginner
Beginner

Joined: Thu Feb 26, 2004 11:45 am
Posts: 46
Quote:
If you are happy with how an extra accessor method works and want to avoid it, a custom PropertyAccessor is exactly the right thing to use.


works, very very nice ... thank you.


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.