• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Unknown name value for enum class hibernate exception

 
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)
 
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 ]
 
Do not threaten THIS beaver! Not even with this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic