Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3444078
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:52:13+00:00 2026-05-18T08:52:13+00:00

Iam still getting the exception lazyinitializationexception. Yes, i know that it means, that the

  • 0

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.

How Else can i do that ?

P.S. : I am using HibernateEntityManger with jpa with annotated class.

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…)

An user can have mutiples roles ….
Users Entity

@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<Role> roles = new HashSet<Role>(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<Role> getRoles() {
    return this.roles;
}
public void setRoles(Set<Role> roles) {
    this.roles = roles;
}

/*@Override toString/equals/hascode  */

}

Role Entity

@Entity
@Table(name = "ROLES")
public class Role implements GenericDomain {
private Long    id;
private String  name;
private String  comment;
private Set<User> users = new HashSet<User>(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<User> getUsers() {
    return this.users;
}
public void setUsers(Set<User> users) {
    this.users = users;
}

/*@Override toString/equals/hascode  */

}

mails

@Entity
@Table(name = “mails”)
public class Mail implements GenericDomain{

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;   }
}

user controller

@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";
}    }

mail controller

@Controller
@SessionAttributes("mail")
@RequestMapping("/portal/mail")
public class MailController{

@Autowired
private MailService mailService;

@RequestMapping(value = "ajaxLoad", method = RequestMethod.GET)
public @ResponseBody List<Mail> list(@RequestParam(value = "type", required = true) String type){
    return mailService.getUserMails((Long) WebHelper.getPrincipal().getUser().getId(),type);
}   

}

my web.xml

<filter>
    <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
    <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>SpringOpenEntityManagerInViewFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>   

my edit.jsp for user

<select >
<c:forEach items="${allRoles}" var="role">
    <option value="${role.id}" <c:if test="${fn:contains(roleSelected, role)}">selected="selected"</c:if> >${role.name}</option>
</c:forEach>
</select>

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T08:52:13+00:00Added an answer on May 18, 2026 at 8:52 am

    removing all the eager and adding this solve my problem

        <mvc:interceptors>
        <bean class="org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor">  
            <property name="entityManagerFactory" ref="entityManagerFactory" />  
        </bean>
    </mvc:interceptors>
    

    the filter didnt work

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I am getting an exception thrown that my Test project cannot open the
I am getting comfortable writing regular queries in SPARQL, but I'm still having trouble
I am still new to sharepoint and would like to know if it is
I am trying to pass an exception to an HttpHandler by doing the following:
I am still very new to Ruby (reading through the Pickaxe and spending most
I am still having problems with figuring out how to create winforms in a
I must be dense. After asking several questions on StackOverflow, I am still at
After reading Evan's and Nilsson's books I am still not sure how to manage
You guys were very helpful yesterday. I am still a bit confused here though.
After reading the Test-and-Set Wikipedia entry , I am still left with the question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.