-->
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: Store PersistentEnums in One to Many
PostPosted: Thu Nov 06, 2003 12:14 pm 
Newbie

Joined: Thu Nov 06, 2003 12:03 pm
Posts: 2
I have a one to many relationship in my DB. The User table has a one to many relationship with User_Roles table. The User_Roles table has the user_id as a foreign key and the int column to identify the actual user role.

On the java side the Role object is implemented as Hibernate PersistentEnum, with a standard enum implementation. The User object has a collection of Role objects (PersistentEnum's). The problem comes when I want to map this collection. I am trying to map it as a one-to-many collection. Below is the mapping doc extract.

<bag name="roles" table="user_roles">
<key column="USER_ID_FK"/>
<one-to-many class="com.matchboxsoftware.tickettout.domain.Role"/>
</bag>

The immediate obvious problem is that my PersistentEnum does not have a zero argument constructor....the nature of enums. My question is, is it possible to even persist my enum in a one to many type relationship? If so how would I do it? Or should I simply implement the Role object as a standard class (not what I really want to do)?

Any help would be greatly appreciated.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 06, 2003 2:03 pm 
Regular
Regular

Joined: Tue Sep 16, 2003 11:35 am
Posts: 93
Location: San Francisco, CA
This is how you do it:

Code:
      <set
         name="roles"
         table="the_table"
         inverse="false"
         cascade="delete"
         >

         <key
            column="user_id"
            >
         </key>

         <element
            type="my.EnumClass"
            column="enum_int_value"
            />

      </set>


Top
 Profile  
 
 Post subject: Still not working
PostPosted: Thu Nov 13, 2003 10:54 am 
Newbie

Joined: Thu Nov 06, 2003 12:03 pm
Posts: 2
I tried the aforementioned solution but it's still not working. I am still getting an "Object Role must declare a default (no argument) constructor" exception from Hibernate.

Here is the pretty standard PersistentEnum class.

Code:
public class Role implements PersistentEnum {
    private final int roleEnumID;

    private Role(int roleEnumID){

        this.roleEnumID = roleEnumID;
    }
   
    public static final Role USER    = new Role(0);
    public static final Role APPROVER = new Role(1);
    public static final Role EVENTADMIN   = new Role(2);
    public static final Role ADMIN   = new Role(3);

    public int toInt() {
        return roleEnumID;
    }

    public static Role fromInt(int code) {
        switch (code) {
            case 0: return USER;
            case 1: return APPROVER;
            case 2: return EVENTADMIN;
            case 3: return ADMIN;
            default: throw new RuntimeException("Unknown User Role");
        }
    }


}


Here is the mapping doc.

<bag name="roles" table="user_roles" inverse="false" cascade="delete" >
<key column="USER_ID_FK"/>
<element type="com.matchboxsoftware.tickettout.domain.Role" column="ROLE_ENUM_ID"/>
</bag>


Top
 Profile  
 
 Post subject: Was this every fixed?
PostPosted: Fri Feb 06, 2004 4:08 pm 
Beginner
Beginner

Joined: Mon Sep 08, 2003 10:21 pm
Posts: 40
Location: Honolulu, HI
Hello,

Did anyone ever find a solution to this? I'm running into the same problem. I also wonder if I'm solving the problem the right way.

Any hints would be greatly appreciated!

Thanks,
Seth


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 4:13 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Well, I never actually believed this one..... do you have a stack trace?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 4:24 pm 
Newbie

Joined: Wed Nov 12, 2003 3:45 pm
Posts: 8
Try adding

Code:
private Role(){}


to your Role class?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 4:41 pm 
Beginner
Beginner

Joined: Mon Sep 08, 2003 10:21 pm
Posts: 40
Location: Honolulu, HI
Hi Gavin,

I'm in the middle of trying this right now. Sorry for the confusion from my previous post. I should have said "I'm trying this as well" instead of "I'm having the same problem".

I was led down the wrong path a bit by trying to get xdoclet to handle this. Apparently, xdoclet will only map a class to a one-to-many. For this example to work, we need an element.

I'm using xdoclet's merge capabilities to place this into my mapping file:

<set
name="activities"
table="BUSINESS_ACTIVITIES"
lazy="true"
inverse="false"
cascade="delete"
sort="unsorted"
>

<key
column="business_id"
/>

<element
column="activity_id"
type="com.hic.eboss.Activity"
/>
</set>

A good sign is that the SchemaGenerator seemed to do the right thing and created a BUSINESS_ACTIVITIES table.

I'll post a followup when I get the test running.

Thanks again,
Seth


Top
 Profile  
 
 Post subject: All Good
PostPosted: Fri Feb 06, 2004 5:05 pm 
Beginner
Beginner

Joined: Mon Sep 08, 2003 10:21 pm
Posts: 40
Location: Honolulu, HI
Seems like it works great! Now if I can get xdoclet to manage this relationship for me.... :)

Thanks for your always prompt help. Good luck with the book!

public void testDB() throws Exception {
Session session = SessionManager.getSession();
Transaction txn = SessionManager.getTransaction();
biz.setName("foo");
biz.addActivity(Activity.COMMISSIONS);
biz.setType(BizType.SOLE_PROPRIETORSHIP);
session.save(biz);
session.flush();
txn.commit();
SessionManager.closeSession();

String id = biz.getId();

session = SessionManager.getSession();
biz = (Business) session.load(Business.class, id);

assertTrue(biz.getActivities().contains(Activity.COMMISSIONS));

SessionManager.closeSession();
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 5:23 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Can't you do it with:

/**
* @hibernate.collection-element column="something" type="PersistentEnum"
* @hibernate.set
*/

Or something like that?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 06, 2004 6:18 pm 
Beginner
Beginner

Joined: Mon Sep 08, 2003 10:21 pm
Posts: 40
Location: Honolulu, HI
Thanks! That worked great. I didn't realize I could replace collection-one-to-many with collection-element.

I really appreciate it. Thanks again.

Seth


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.