954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Hibernate Set<Enum> mapping

I´m sorry if I haven´t investigated enough but I´m not quite an expert in hibernate and I´m tired of looking for something that won´t really solve my specific problem. What I mean is that I am a bit short of time. I have a User class that represents every single user of the system.
Every User has a Set of Roles (which is an enum type) that indicates which resources the user can and cannot acces. Nothing complicated. I am using Hibernate to map this classes but I am having problems with this last part.
It doesn´t bother me at all how the Roles, say, field(which, I repeat, is an enum) is mapped in the DB (could be an int or a String) but it must definetly be an extra table, I guess, which has the fk of the user and the field which corresponds the role to the user, as one user has many roles assigned to it. I am using JPA annotations. I do not believe any sample code is necesary because this is that simple: The user class has a set of enum types which must be mapped with jpa Annotations.
I´m sorry for the bad english but it´s not my native language as you can see. Thanks a lot.

elSifa
Newbie Poster
5 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

idiom:

public enum Roles {

    KING, SLAVE, OBSERVER, NOTHING
}

real representation

// Decompiled by DJ v3.7.7.81 Copyright 2004 Atanas Neshkov  Date: 2010-04-23 22:51:41
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) fieldsfirst nonlb 
// Source File Name:   Roles.java


public final class Roles extends Enum {

    public static final Roles KING;
    public static final Roles SLAVE;
    public static final Roles OBSERVER;
    public static final Roles NOTHING;
    private static final Roles $VALUES[];

    public static Roles[] values() {
        return (Roles[])$VALUES.clone();
    }

    public static Roles valueOf(String name) {
        return (Roles)Enum.valueOf(Roles, name);
    }

    private Roles(String s, int i) {
        super(s, i);
    }

    static  {
        KING = new Roles("KING", 0);
        SLAVE = new Roles("SLAVE", 1);
        OBSERVER = new Roles("OBSERVER", 2);
        NOTHING = new Roles("NOTHING", 3);
        $VALUES = (new Roles[] {
            KING, SLAVE, OBSERVER, NOTHING
        });
    }
}


This is all, what can I help

quuba
Posting Pro
573 posts since Nov 2008
Reputation Points: 123
Solved Threads: 106
 

I really appreciate your reply. Sincerelly. However my real doubt doesn´t concern java, but hibernate. I have an enum like the first code you posted and a User class that has a private Set member which I need to map through hibernate jpa annotations. Today i will keep trying a few things as I have free time. If someone can direct me to some reading, I will be grateful.

elSifa
Newbie Poster
5 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

I have solved my problem already. I don´t know how to close thread.

elSifa
Newbie Poster
5 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

elSifa: please share your solution with us - I for one would certainly like to know the answer!

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
@CollectionOfElements(targetElement=Role.class)
@JoinTable
	(name="user_role_join_tbl",
	joinColumns=@JoinColumn(name="user_id"))
@Enumerated(EnumType.STRING)
@Column (name="role", nullable=false)
private Set<Role> roles = new HashSet<Role>();

This is it. Now I´m having another problem besides not knowing how to close the thread. I will start a new one to explain. See you there? Ahh... by the way, the "@CollectionOfElements" annotation is from an older version. Not the newest one, so now It would be something like "@ElementCollection".

elSifa
Newbie Poster
5 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

@quuba

#
// Decompiled by DJ v3.7.7.81 Copyright 2004 Atanas Neshkov Date: 2010-04-23 22:51:41
#
// Home Page : http://members.fortunecity.com/neshkov/dj.html - Check often for new version!
#
// Decompiler options: packimports(3) fieldsfirst nonlb
#
// Source File Name: Roles.java
#
 
#
 
#
public final class Roles extends Enum {


Are u extending enum ? as far as i know no inheritance for enums in java .Sorry for my comment .

msi_333
Newbie Poster
22 posts since Jun 2007
Reputation Points: 10
Solved Threads: 4
 

@msi_333
I did not, then compiler
http://java.sun.com/docs/books/tutorial/java/javaOO/enum.html
The compiler automatically adds some special methods when it creates an enum.Note: All enums implicitly extend java.lang.Enum...
@elSifa thank you for your patience

quuba
Posting Pro
573 posts since Nov 2008
Reputation Points: 123
Solved Threads: 106
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: