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;
}
}
嗯,我是春天新手。当我尝试编译这些代码时,我得到了下面提到的错误。所以请帮我解决这个问题。提前谢谢!
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"])
答案 0 :(得分:2)
你的问题中的细节不足以帮助你立即解决,但你应该
LazyInitializationException
- 这是一个常见的新手问题。这是一个运行时异常,而不是编译时间@Transactional
注释。查看您的代码,您正在尝试将JPA实体rihght序列化为JSON,因此open-session-in-view对您有用,但是您需要非常小心。此模式仅在非常简单的应用程序中有用。
答案 1 :(得分:0)
LazyInitializationException
和@ManyToMany
个对象时,会发生 @OneToMany
Hibernate在(@ManyToMany
和@OneToMany
)的情况下加载LAZY
因此,在获取@ManyToMany
或@OneToMany
关系对象时,您应首先在DAO层中进行迭代,然后以其他方式获取数据,显示LazyInitializationException