Hello. Can you please tell how this exception is solved: javax.el.ELException: org.hibernate.LazyInitializationException: could not initialize proxy - no Session
Here is the jsp code:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ page isELIgnored="false" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%--<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>--%> <html> <style> .prokrutka{ overflow: auto; } </style> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Company list</title> <link href="<c:url value='/static/css/bootstrap.css' />" rel="stylesheet"></link> <link href="<c:url value='/static/css/app.css' />" rel="stylesheet"></link> </head> <body class="prokrutka"> <div class="generic-container"> <%--<%@include file="authheader.jsp" %>--%> <div class="panel panel-default"> <!-- Default panel contents --> <div class="panel-heading"><span class="lead">List of company </span></div> <table class="table table-hover"> <thead> <tr> <th>Id</th> <th>CompanyName</th> <th>Phone number</th> <th>City name</th> </tr> </thead> <tbody> <c:forEach items="${companies}" var="companies"> <tr> <td>${companies.companyId}</td> <td>${companies.companyName}</td> <td>${companies.phoneNumber}</td> <td>${companies.getCityName()}</td> </tr> </c:forEach> </tbody> </table> </div> </div> </body> </html> controller code:
@Controller @RequestMapping("/admin/getAllcompany") public class GetAllCompany { @Autowired CompanyService companyService; @RequestMapping(method = RequestMethod.GET) public String listCompany(ModelMap model) { List<Companies> companies = companyService.getAll(); model.addAttribute("companies", companies); return "/admin/getAllcompany"; } } Entity Code Companies:
@Entity @Table(name = "COMPANIES") public class Companies { @Id @Column(name = "COMPANY_ID") @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "COMPANIES_SEQ") @SequenceGenerator(name = "COMPANIES_SEQ", sequenceName = "COMPANIES_SEQ") private long companyId; @Column(name = "COMPANY_NAME", nullable = false, length = 30) private String companyName; @Column(name = "PHONE_NUMBER", length = 30) private String phoneNumber; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "CITY_ID") private Cities city; @OneToMany(fetch = FetchType.LAZY, mappedBy = "company") public List<Buses> buses = new ArrayList<Buses>(); @OneToMany(fetch = FetchType.LAZY, mappedBy = "company") private List<Drivers> drivers = new ArrayList<Drivers>(); @OneToMany(fetch = FetchType.LAZY, mappedBy = "company") public List<Routes> routes = new ArrayList<Routes>(); public Companies() { } public String getCityName(){ return city.getCityName(); } public long getCompanyId() { return companyId; } public String getCompanyName() { return companyName; } public String getPhoneNumber() { return phoneNumber; } public Cities getCity() { return city; } public List<Buses> getBuses() { return buses; } public List<Drivers> getDrivers() { return drivers; } public List<Routes> getRoutes() { return routes; } public void setCompanyId(long companyId) { this.companyId = companyId; } public void setCompanyName(String companyName) { this.companyName = companyName; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public void setCity(Cities city) { this.city = city; } public void setBuses(List<Buses> buses) { this.buses = buses; } public void setDrivers(List<Drivers> drivers) { this.drivers = drivers; } public void setRoutes(List<Routes> routes) { this.routes = routes; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Companies companies = (Companies) o; if (companyId != companies.companyId) return false; if (companyName != null ? !companyName.equals(companies.companyName) : companies.companyName != null) return false; if (phoneNumber != null ? !phoneNumber.equals(companies.phoneNumber) : companies.phoneNumber != null) return false; if (city != null ? !city.equals(companies.city) : companies.city != null) return false; return true; } @Override public int hashCode() { int result = (int) (companyId ^ (companyId >>> 32)); result = 31 * result + (companyName != null ? companyName.hashCode() : 0); result = 31 * result + (phoneNumber != null ? phoneNumber.hashCode() : 0); result = 31 * result + (city != null ? city.hashCode() : 0); return result; } } repository code:
@Repository public class CompaniesRepository extends AbstractRepository<Companies> { public CompaniesRepository() { super(Companies.class); } public Companies findByCompanyName(String companyName) { Criteria criteria = createEntityCriteria(); criteria.add(Restrictions.eq("companyName", companyName)); return (Companies) criteria.uniqueResult(); } }