-->
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.  [ 17 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Hibernate Mapping for Collection<Enum> ?
PostPosted: Mon Feb 28, 2005 12:11 pm 
Newbie

Joined: Mon Feb 28, 2005 10:39 am
Posts: 1
Hibernate version:
3.0beta3

Hello,

I was wondering how I can map a one-to-many relationship with a Collection<Enum> on the many side.

for example, I have a User class:

Code:
@Entity
public class User {
    private String username;
    private String password;
    private Set<Role> roles;

   // getters and setters...

    public final Set<Role> getRoles() {
        return this.roles;
    }

    public final void setRoles(Set<Role> roles) {
        this.roles = roles;
    }
}


and a Role enumeration like this :

Code:
public enum Role {
    ADMINISTRATOR, MODERATOR, USER;
}



I use a custom RoleUserType to persist Role (see "UserType for persisting an Enum with a VARCHAR column" at http://www.hibernate.org/265.html).

Thanks.

PS: I'd like to use annotations as much as I can, so if you know how to do this with annotations...


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 28, 2005 12:21 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Should be easy. A collection of values may use a UserType as the element type.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 11:16 am 
Newbie

Joined: Mon Oct 11, 2004 11:29 am
Posts: 11
Any way to do this using ejb3 annotations?

Code:
@OneToMany(targetEntity=Enum.class) Set<SomeEnum> getFoo()
gives
Code:
org.hibernate.MappingException: Association references unmapped class: java.lang.Enum

and
Code:
@OneToMany Set<SomeEnum> getFoo()
gives
Code:
Association references unmapped class: <enum class>

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 1:07 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Collections of primitive types are not supported yet, sorry.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 1:10 pm 
Newbie

Joined: Mon Oct 11, 2004 11:29 am
Posts: 11
Is this a limitation of hibernate or ebj3 spec?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 1:12 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This will not be part of the spec, and Hibernate will provide this feature as a specific extension.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 1:13 pm 
Newbie

Joined: Mon Oct 11, 2004 11:29 am
Posts: 11
Are there any suggested work arounds that will stay within the ejb3 spec?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 1:15 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
public class MyEnumedEntity {
@Id public Integer getId() ...
public Enum getEnum() ...
}

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 2:02 pm 
Newbie

Joined: Mon Oct 11, 2004 11:29 am
Posts: 11
First let me say thanks for all the help emmanuel.

If I use MyEnumEntity it will be mapped to its own table correct? So I will have three tables instead of two.

Also, using previous example my model will contain Set<MyEnumEntity> and not Set<Role> correct? This I think can be fixed with interceptors to sync between the two.

Are my assumptions correct?

I am really trying to avoid having another table in the database though.

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 2:15 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
why 3 tables?

User
MyEntityEnum

instead of
User
EnumTable

If you don't want any extra table you could probably write a usertype serializing your enums ( ENUM1, ENUM2 ) in 1 column

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 2:23 pm 
Newbie

Joined: Mon Oct 11, 2004 11:29 am
Posts: 11
Maybe I am missing something...
If I map MyEntityEnum as an entity wont my tables look like this?

[User id, firstname, lastname, foo]
[UserEntityLink userId, enumId]
[MyEntityEnum id]

When I was using hibernate instead of ejb3 I used a type that mapped the enum to a varchar so I would only have the top two tables.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 3:43 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
The assoc table is unnecessary for a OneToMany association.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 3:51 pm 
Newbie

Joined: Mon Oct 11, 2004 11:29 am
Posts: 11
Ohhh daah, I was looking at it the wrong way.

emmanuel wrote:
public class MyEnumedEntity {
@Id public Integer getId() ...
public Enum getEnum() ...
}


Here you meant the id is referring to the user's id not some arbitrary enum id. Get it now. Thanks!

I wonder why the ejb3 spec doesnt support such a common usecase.

Thanks a lot!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 4:04 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
no id is refering to an arbitrary id, but the userid will be in the table tnaks to

@OneToMany
@joinColumn(name="userId")
Set...

This kind of case may be common in small apps, but not in EE apps

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 22, 2005 5:16 pm 
Newbie

Joined: Mon Oct 11, 2004 11:29 am
Posts: 11
Sorry to waste your time Emmanuel. Ive been head deep into jboss xml files all morning and my head is not in the right place. I see what you are saying now.

Do you see any clean way to keep the model clean. I was thinking a set decorator that can dynamically convert from the enum wrapper into the enum. Not sure how feasible that is until I try it. What do you think?

Quote:
This kind of case may be common in small apps, but not in EE apps

I meant the Collection<Enum> not the user/role deal.

Thanks!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 17 posts ]  Go to page 1, 2  Next

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.