This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of The Little Book of Impediments (e-book only) and have Tom Perry on-line!
See this thread for details.
Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unknown name value for enum class hibernate exception

 
Pramod Kumar
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a enum

public enum UserRole {
puser,duser
}

In User class I have getters and setters,

@Column(name = "USER_ROLE")
public UserRole getRole() {
return role;
}

public void setRole(UserRole role) {
this.role = role;
}

and in my db that column is declared as VARCHAR, I am able to insert values in to the db, and it is stored as integers, 0(puser) and 1(duser)

I am setting the enum values as below

UserRole userRole = UserRole.PUSER;

userForm.setRole(userRole);

I am able to insert the values.

The problem is when I retreive it is giving exception

Query q = em.createNamedQuery("User.byUserId");
q.setParameter("userId", userId);

-->user = (User) getSingleResult(q); I am getting Exception here.


No enum const class com.UserRole.1

java.lang.IllegalArgumentException: Unknown name value for enum class com.UserRole: 1
 
Pramod Kumar
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got the solution need to put @Enumerated(EnumType.STRING)
 
Michael Dube
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did someone have a way to map it in a List/Set?
I have a user with many roles

@OneToMany(cascade=CascadeType.ALL, targetEntity=Role.class)
@JoinTable(name="user_roles", joinColumns={@JoinColumn(name="user")} )
@OrderBy(value="role")
private Set<Role> roles;

My class Role is a enum. I don't have table to store the role. I have a column role in the table user_roles

I miss something somewhere!

[ February 20, 2008: Message edited by: Michael Dube ]
[ February 20, 2008: Message edited by: Michael Dube ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic