java - Hibernate: failed to lazily initialize a collection -



java - Hibernate: failed to lazily initialize a collection -

i getting below error: when trying save data.

org.hibernate.lazyinitializationexception: failed lazily initialize collection of role: com.test.model.user.userrole, not initialize proxy - no session

i have listed classes have used.

a user.java:

import java.util.hashset; import java.util.set; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.fetchtype; import javax.persistence.id; import javax.persistence.onetomany; import javax.persistence.table; @entity @table(name = "users", catalog = "test") public class user { private string username; private string password; private boolean enabled; private set<userrole> userrole = new hashset<userrole>(0); public user() { } public user(string username, string password, boolean enabled) { this.username = username; this.password = password; this.enabled = enabled; } public user(string username, string password, boolean enabled, set<userrole> userrole) { this.username = username; this.password = password; this.enabled = enabled; this.userrole = userrole; } @id @column(name = "username", unique = true, nullable = false, length = 45) public string getusername() { homecoming this.username; } public void setusername(string username) { this.username = username; } @column(name = "password", nullable = false, length = 60) public string getpassword() { homecoming this.password; } public void setpassword(string password) { this.password = password; } @column(name = "enabled", nullable = false) public boolean isenabled() { homecoming this.enabled; } public void setenabled(boolean enabled) { this.enabled = enabled; } @onetomany(fetch = fetchtype.lazy, mappedby = "user") public set<userrole> getuserrole() { homecoming this.userrole; } public void setuserrole(set<userrole> userrole) { this.userrole = userrole; } }

and userrole.java:

import static javax.persistence.generationtype.identity; import javax.persistence.column; import javax.persistence.entity; import javax.persistence.fetchtype; import javax.persistence.generatedvalue; import javax.persistence.id; import javax.persistence.joincolumn; import javax.persistence.manytoone; import javax.persistence.table; import javax.persistence.uniqueconstraint; @entity @table(name = "user_roles", catalog = "test", uniqueconstraints = uniqueconstraint(columnnames = { "role", "username" })) public class userrole{ private integer userroleid; private user user; private string role; public userrole() { } public userrole(user user, string role) { this.user = user; this.role = role; } @id @generatedvalue(strategy = identity) @column(name = "user_role_id", unique = true, nullable = false) public integer getuserroleid() { homecoming this.userroleid; } public void setuserroleid(integer userroleid) { this.userroleid = userroleid; } @manytoone(fetch = fetchtype.lazy) @joincolumn(name = "username", nullable = false) public user getuser() { homecoming this.user; } public void setuser(user user) { this.user = user; } @column(name = "role", nullable = false, length = 45) public string getrole() { homecoming this.role; } public void setrole(string role) { this.role = role; } }

and have dao implemented:

@transactional @override @suppresswarnings("unchecked") public list<user> listusers() { homecoming this.sessionfactory.getcurrentsession() .createcriteria(user.class).list(); }

i listing users in controller "listusers" method. , add together model.addobject("users", listusers);.

in view, utilize next code:

<c:foreach items="${users}" var="user"> username: ${user.username} pass: ${user.password} role: ${user.userrole} </c:foreeach>

but got above error (because of ${user.userrole} variable):

so how can print out role of users (like: normal user, admin)?

your session available execute criteria. after that, it's closed , it's not available. proxied fields depend on session recovered later, since session closed, exception message lazyinitializationexception.

to solve it, retrieve info while session still open, or not mark such fields fetchtype.lazy.

java spring hibernate

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -