Reputation: 11
package com.ccdm.vo;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import static javax.persistence.GenerationType.IDENTITY;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnore;
/**
* ViewModel generated by hbm2java
*/
@Entity
@Table(name = "lb", catalog = "ccdmdb")
public class LoadBal implements java.io.Serializable {
private static final long serialVersionUID = -767868186084580713L;
private Integer lbID;
@JsonIgnore
private AppUser appUser;
private ViewModel templates;
private String lbname;
private String lbdescription;
private String lbtype;
private int clientId;
public LoadBal() {
}
public LoadBal(AppUser appUser, String lbname, String lbdescription, String lbtype, ViewModel templates,
int clientId) {
this.appUser = appUser;
this.lbname = lbname;
this.lbdescription = lbdescription;
this.lbtype = lbtype;
this.templates = templates;
this.clientId = clientId;
System.out.println(this.lbname);
System.out.println(this.lbdescription);
System.out.println(this.lbtype);
System.out.println(this.templates);
System.out.println(this.clientId);
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "LB_ID", unique = true, nullable = false)
public Integer getlbID() {
return this.lbID;
}
public void setlbID(Integer lbID) {
this.lbID = lbID;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "USER_ID", nullable = false)
public AppUser getAppUser() {
return this.appUser;
}
public void setAppUser(AppUser appUser) {
this.appUser = appUser;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "VM_ID", nullable = false)
public ViewModel getTemplates() {
System.out.println("here2" + templates);
return this.templates;
}
public void setTemplates(ViewModel VmTemplates) {
System.out.println("here3" + VmTemplates);
this.templates= VmTemplates;
}
@Column(name = "LB_NAME", nullable = false, length = 100)
public String getlbname() {
return this.lbname;
}
public void setlbname(String lbname) {
this.lbname = lbname;
}
@Column(name = "LB_DESCRIPTION", nullable = false, length = 100)
public String getlbdescription() {
return this.lbdescription;
}
public void setlbdescription(String lbdescription) {
this.lbdescription = lbdescription;
}
@Column(name = "LB_TYPE", nullable = false, length = 100)
public String getlbtype() {
return this.lbtype;
}
public void setlbtype(String lbtype) {
this.lbtype = lbtype;
}
@Column(name = "CLIENT_ID", nullable = false)
public int getClientId() {
return this.clientId;
}
public void setClientId(int clientId) {
this.clientId = clientId;
}
}
package com.ccdm.vo;
// Generated 19 Dec, 2016 5:50:57 PM by Hibernate Tools 5.2.0.CR1
import static javax.persistence.GenerationType.IDENTITY;
import java.util.HashSet;
import java.util.Set;
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.OneToMany;
import javax.persistence.Table;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
/**
* ViewModel generated by hbm2java
*/
@Entity
@Table(name = "view_model", catalog = "ccdmdb")
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
public class ViewModel implements java.io.Serializable {
private static final long serialVersionUID = -7778681960884580713L;
private Integer vmId;
@JsonIgnore
private AppUser appUser;
@JsonIgnore
private OperatingSystem operatingSystem;
@JsonIgnore
private VirtualMachine virtualMachine;
private String name;
private int clientId;
@JsonIgnore
private Set<SolutionVmMap> solutionVmMaps = new HashSet<SolutionVmMap>(0);
@JsonIgnore
private Set<LoadBal> VmTemplates = new HashSet<LoadBal>(0);
public ViewModel() {
}
public ViewModel(AppUser appUser, OperatingSystem operatingSystem, VirtualMachine virtualMachine, String name,
int clientId) {
this.appUser = appUser;
this.operatingSystem = operatingSystem;
this.virtualMachine = virtualMachine;
this.name = name;
this.clientId = clientId;
}
public ViewModel(AppUser appUser, OperatingSystem operatingSystem, VirtualMachine virtualMachine, String name,
int clientId, Set<SolutionVmMap> solutionVmMaps, Set<LoadBal> VmTemplates) {
this.appUser = appUser;
this.operatingSystem = operatingSystem;
this.virtualMachine = virtualMachine;
this.name = name;
this.clientId = clientId;
this.solutionVmMaps = solutionVmMaps;
this.VmTemplates = VmTemplates;
System.out.println("vmtem------" + VmTemplates);
}
@Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "VM_ID", unique = true, nullable = false)
public Integer getVmId() {
return this.vmId;
}
public void setVmId(Integer vmId) {
this.vmId = vmId;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "USER_ID", nullable = false)
public AppUser getAppUser() {
return this.appUser;
}
public void setAppUser(AppUser appUser) {
this.appUser = appUser;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "OS_ID", nullable = false)
public OperatingSystem getOperatingSystem() {
return this.operatingSystem;
}
public void setOperatingSystem(OperatingSystem operatingSystem) {
System.out.println("os" + operatingSystem);
this.operatingSystem = operatingSystem;
}
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "VIRTUAL_MACHINE_ID", nullable = false)
public VirtualMachine getVirtualMachine() {
return this.virtualMachine;
}
public void setVirtualMachine(VirtualMachine virtualMachine) {
this.virtualMachine = virtualMachine;
}
@Column(name = "NAME", nullable = false, length = 100)
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@Column(name = "CLIENT_ID", nullable = false)
public int getClientId() {
return this.clientId;
}
public void setClientId(int clientId) {
this.clientId = clientId;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "viewModel")
public Set<SolutionVmMap> getSolutionVmMaps() {
return this.solutionVmMaps;
}
public void setSolutionVmMaps(Set<SolutionVmMap> solutionVmMaps) {
this.solutionVmMaps = solutionVmMaps;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "templates")
public Set<LoadBal> getTemplates() {
return this.VmTemplates;
}
public void setTemplates(Set<LoadBal> VmTemplates) {
this.VmTemplates = VmTemplates;
}
}
well, I'm new to spring. When I try to compile these codes, I'm getting the below-mentioned error. so please help me to sort out this problem. Thanks in advance!
org.springframework.http.converter.HttpMessageNotWritableException: Could not write content: failed to lazily initialize a collection of role: com.ccdm.vo.ViewModel.templates, could not initialize proxy - no Session (through reference chain: com.ccdm.infrastructure.model.CCDMResponse["content"]->java.util.ArrayList[0]->com.ccdm.vo.ViewModel["templates"]); nested exception is com.fasterxml.jackson.databind.JsonMappingException: failed to lazily initialize a collection of role: com.ccdm.vo.ViewModel.templates, could not initialize proxy - no Session (through reference chain:
\\\com.ccdm.infrastructure.model.CCDMResponse["content"]->java.util.ArrayList[0]->com.ccdm.vo.ViewModel["templates"])
Upvotes: 1
Views: 841
Reputation: 86
LazyInitializationException
is occurred while you getting @ManyToMany
and @OneToMany
objects
Hibernate is LAZY loading in case of(@ManyToMany
and @OneToMany
)
So while getting @ManyToMany
or @OneToMany
relation objects you should first iterate in DAO layer , and get the data other wise it shows LazyInitializationException
Upvotes: 0
Reputation: 12212
It's not enough details in your question to help you resolve it right away, however you should
LazyInitializationException
- it's a common newbie problem. And it's a runtime exception, not compile time@Transactional
annotation on your code.Looking at your code, you're trying to serialize JPA entity rihght away to JSON so open-session-in-view will work for you, however you need to be VERY CAREFUL. This pattern is useful only in very simple applications.
Upvotes: 3