Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to solve lazyinitializationexception not using fetch=FetchType.EAGER?
    primarykey
    data
    text
    <p>Iam still getting the exception lazyinitializationexception. Yes, i know that it means, that the session is closed while I or something else is trying to access the collection. No, the OpenEntityManagerInViewFilter did not work. Yes, @ManyToOne(fetch=FetchType.EAGER) helped but i dont want to use it, cause it will fecth aumotically all the time.</p> <p>How Else can i do that ?</p> <p>P.S. : I am using HibernateEntityManger with jpa with annotated class.</p> <p>UPADATE here is my code, first of all I have 4 tables : users(id,first_name,last_name,email....) roles(id,name,comment....) users_roles(user_id,role_id) mails(id,user_id,subject,message,to_id...)</p> <p>An user can have mutiples roles .... Users Entity</p> <pre><code>@Entity @Table(name = "USERS") public class User implements GenericDomain{ public static final String _ID = "id"; private Long id; private String firstName; private String lastName; private String email; private Set&lt;Role&gt; roles = new HashSet&lt;Role&gt;(0); /* Constructors */ public User() { } @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name = "ID", unique = true, nullable = false) public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } @Column(name="FIRST_NAME", nullable = false, length = 64) @NotEmpty @Length(min = 4, max = 45) public String getFirstName() { return this.firstName; } public void setFirstName(String firstname) { this.firstName = firstname; } @Column(name="LAST_NAME", nullable = false, length = 64) @NotEmpty @Length(min = 4, max = 45) public String getLastName() { return this.lastName; } public void setLastName(String lastname) { this.lastName = lastname; } @Column(name="EMAIL", unique = false, length = 64) @Email @NotEmpty @Length(min = 4, max = 45) public String getEmail() { return this.email; } public void setEmail(String email) { this.email = email; } @ManyToMany(fetch=FetchType.EAGER) @JoinTable(name = "USERS_ROLES" , joinColumns = { @JoinColumn(name = "user_id") } , inverseJoinColumns = { @JoinColumn(name = "role_id") } ) public Set&lt;Role&gt; getRoles() { return this.roles; } public void setRoles(Set&lt;Role&gt; roles) { this.roles = roles; } /*@Override toString/equals/hascode */ </code></pre> <p>}</p> <p>Role Entity</p> <pre><code>@Entity @Table(name = "ROLES") public class Role implements GenericDomain { private Long id; private String name; private String comment; private Set&lt;User&gt; users = new HashSet&lt;User&gt;(0); public Role() { } @Id @GeneratedValue(strategy=GenerationType.AUTO) @Column(name = "ID", unique = true, nullable = false) public Long getId() { return id; } public void setId(Long id) { this.id = id; } @Column(name="NAME", nullable = false, length = 64) @NotEmpty @Length(min = 1, max = 32) public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name="COMMENT", nullable = true, length = 256) @Length(min = 0, max = 255) public String getComment() { return this.comment; } public void setComment(String comment) { this.comment = comment;} @ManyToMany(cascade=CascadeType.REFRESH,fetch=FetchType.EAGER) @JoinTable( name = "USERS_ROLES" , joinColumns = { @JoinColumn(name = "role_id") } , inverseJoinColumns = { @JoinColumn(name = "user_id") } ) public Set&lt;User&gt; getUsers() { return this.users; } public void setUsers(Set&lt;User&gt; users) { this.users = users; } /*@Override toString/equals/hascode */ </code></pre> <p>}</p> <p>mails</p> <p>@Entity @Table(name = "mails") public class Mail implements GenericDomain{</p> <pre><code>private Long id; private String mailSubject; private String mailContent; private Long receiverId; private User user = null; public Mail(){ } @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "ID", nullable = false) public Long getId(){ return this.id; } public void setId(Long id){ this.id = id;} @Column(name = "MAILSUBJECT", nullable = false, length = 63) @Length(max = 63) public String getMailSubject(){ return this.mailSubject; } public void setMailSubject(String mailSubject){ this.mailSubject = mailSubject; } @Column(name = "MAILCONTENT", nullable = true, length = 255) @Length(max = 255) public String getMailContent(){ return this.mailContent; } public void setMailContent(String mailContent){ this.mailContent = mailContent; } @Column(name = "RECEIVERID") public Long getReceiverId(){ return this.receiverId; } public void setReceiverId(Long receiverId){ this.receiverId = receiverId; } @ManyToOne(fetch=FetchType.EAGER) @JoinColumn(name = "USER_ID") @NotNull public User getUser(){ return this.user; } public void setUser(User user){ this.user = user; } } </code></pre> <p>user controller</p> <pre><code>@Controller @RequestMapping("/admin/user") @SessionAttributes("user") public class UserController { private UserService userService; private RoleService roleService; @Autowired public UserController(UserService userService, RoleService roleService) { this.userService = userService; this.roleService = roleService; } @RequestMapping(value = "edit", method = RequestMethod.GET) public String editUser(@RequestParam(value="id", required = true) Long id, ModelMap model) { model.addAttribute("allRoles", roleService.getAll()); model.addAttribute("user", userService.getOne(id)); return "/admin/user/edit"; } } </code></pre> <p>mail controller</p> <pre><code>@Controller @SessionAttributes("mail") @RequestMapping("/portal/mail") public class MailController{ @Autowired private MailService mailService; @RequestMapping(value = "ajaxLoad", method = RequestMethod.GET) public @ResponseBody List&lt;Mail&gt; list(@RequestParam(value = "type", required = true) String type){ return mailService.getUserMails((Long) WebHelper.getPrincipal().getUser().getId(),type); } </code></pre> <p>}</p> <p>my web.xml</p> <pre><code>&lt;filter&gt; &lt;filter-name&gt;SpringOpenEntityManagerInViewFilter&lt;/filter-name&gt; &lt;filter-class&gt;org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;SpringOpenEntityManagerInViewFilter&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; </code></pre> <p>my edit.jsp for user</p> <pre><code>&lt;select &gt; &lt;c:forEach items="${allRoles}" var="role"&gt; &lt;option value="${role.id}" &lt;c:if test="${fn:contains(roleSelected, role)}"&gt;selected="selected"&lt;/c:if&gt; &gt;${role.name}&lt;/option&gt; &lt;/c:forEach&gt; &lt;/select&gt; </code></pre> <p>With all that, i edit.jsp for user is working fine with lazy=false. With FetchType.EAGER am not able to get any of my mails, am getting into a cycle stackovrflow, without FetchType.EAGER i got that lazy exception.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload