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.

Recommended Answers

All 7 Replies

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

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<Role> 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.

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

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

@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".

@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 .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.