Hibernate:无法初始化代理 – 没有Session(通过引用链……)
注意:在任何人抱怨这是重复之前,请务必仔细阅读内容而不用标题来判断。 另外,请务必仔细阅读您的参考问题和答案,看看它是否重复。 根据我的经验,这个问题可以在不同的环境下发生。 举个例子,在jsp
使用下面代码的人的答案会有所不同,因为使用Spring
会有所不同,而且对于那些DB很小而且负载很好的人会有所不同。 不,我的情况不是他们中的任何一个。
我正在使用Hibernate
和Jersey
编写REST
api。 请看下面的代码。
VerificationCodeJSONService.java – JSON服务类
@Path("/verificaion_code") public class VerificationCodeJSONService { @GET @Path("/getAllVerificationCodes") @Produces(MediaType.APPLICATION_JSON) public List getAllVerificationCodes() { VerificationCodeService verificationCodeService=new VerificationCodeService(); List list = verificationCodeService.getAllVerificationCodes(); return list; } }
VerificationCodeService.java – 服务层
public class VerificationCodeService { private static VerificationCodeDAOInterface verificationCodeDAOInterface; public VerificationCodeService() { verificationCodeDAOInterface = new VerificationCodeDAOImpl(); } public List getAllVerificationCodes() { Session session = verificationCodeDAOInterface.openCurrentSession(); Transaction transaction = null; List verificaionCodes = new ArrayList(); try { transaction = verificationCodeDAOInterface.openTransaction(session); verificaionCodes = verificationCodeDAOInterface.getAllVerificationCodes(session); transaction.commit(); } catch (Exception ex) { ex.printStackTrace(); } finally { session.close(); } return verificaionCodes; } }
VerificationCodeDAOImpl.java – 数据库层
public class VerificationCodeDAOImpl implements VerificationCodeDAOInterface{ private static final SessionFactoryBuilder sessionFactoryBuilder = SessionFactoryBuilder.getInstance(); @Override public List getAllVerificationCodes(Session session) { List verificaionCodes=(List)session.createQuery("from VerificaionCode").list(); return verificaionCodes; } }
VerificationCode.java – DAO层
public class VerificaionCode implements java.io.Serializable { private Integer idverificaionCode; private Patient patient; private String code; private Date dateCreated; private Date lastUpdated; public VerificaionCode() { } public VerificaionCode(Patient patient, String code, Date lastUpdated) { this.patient = patient; this.code = code; this.lastUpdated = lastUpdated; } public VerificaionCode(Patient patient, String code, Date dateCreated, Date lastUpdated) { this.patient = patient; this.code = code; this.dateCreated = dateCreated; this.lastUpdated = lastUpdated; } public Integer getIdverificaionCode() { return this.idverificaionCode; } public void setIdverificaionCode(Integer idverificaionCode) { this.idverificaionCode = idverificaionCode; } public Patient getPatient() { return this.patient; } public void setPatient(Patient patient) { this.patient = patient; } public String getCode() { return this.code; } public void setCode(String code) { this.code = code; } public Date getDateCreated() { return this.dateCreated; } public void setDateCreated(Date dateCreated) { this.dateCreated = dateCreated; } public Date getLastUpdated() { return this.lastUpdated; } public void setLastUpdated(Date lastUpdated) { this.lastUpdated = lastUpdated; } }
Patient.java – DAO层
public class Patient implements java.io.Serializable { private Integer idpatient; private DiabetesType diabetesType; private Language language; private String customId; private String diabetesOther; private String firstName; private String lastName; private String email; private Date dob; private String parentEmail; private String gender; private Date diagnosedDate; private Double height; private Double weight; private String heightUnit; private String weightUnit; private String theme; private String userName; private String password; private Date dateCreated; private Date lastUpdated; public Patient() { } public Patient(DiabetesType diabetesType, Language language, String customId, String firstName, String email, Date dob, String gender, String theme, String userName, String password, Date lastUpdated) { this.diabetesType = diabetesType; this.language = language; this.customId = customId; this.firstName = firstName; this.email = email; this.dob = dob; this.gender = gender; this.theme = theme; this.userName = userName; this.password = password; this.lastUpdated = lastUpdated; } public Integer getIdpatient() { return this.idpatient; } public void setIdpatient(Integer idpatient) { this.idpatient = idpatient; } public DiabetesType getDiabetesType() { return this.diabetesType; } public void setDiabetesType(DiabetesType diabetesType) { this.diabetesType = diabetesType; } public Language getLanguage() { return this.language; } public void setLanguage(Language language) { this.language = language; } public String getCustomId() { return this.customId; } public void setCustomId(String customId) { this.customId = customId; } public String getDiabetesOther() { return this.diabetesOther; } public void setDiabetesOther(String diabetesOther) { this.diabetesOther = diabetesOther; } public String getFirstName() { return this.firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return this.lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return this.email; } public void setEmail(String email) { this.email = email; } public Date getDob() { return this.dob; } public void setDob(Date dob) { this.dob = dob; } public String getParentEmail() { return this.parentEmail; } public void setParentEmail(String parentEmail) { this.parentEmail = parentEmail; } public String getGender() { return this.gender; } public void setGender(String gender) { this.gender = gender; } public Date getDiagnosedDate() { return this.diagnosedDate; } public void setDiagnosedDate(Date diagnosedDate) { this.diagnosedDate = diagnosedDate; } public Double getHeight() { return this.height; } public void setHeight(Double height) { this.height = height; } public Double getWeight() { return this.weight; } public void setWeight(Double weight) { this.weight = weight; } public String getHeightUnit() { return this.heightUnit; } public void setHeightUnit(String heightUnit) { this.heightUnit = heightUnit; } public String getWeightUnit() { return this.weightUnit; } public void setWeightUnit(String weightUnit) { this.weightUnit = weightUnit; } public String getTheme() { return this.theme; } public void setTheme(String theme) { this.theme = theme; } public String getUserName() { return this.userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return this.password; } public void setPassword(String password) { this.password = password; } public Date getDateCreated() { return this.dateCreated; } public void setDateCreated(Date dateCreated) { this.dateCreated = dateCreated; } public Date getLastUpdated() { return this.lastUpdated; } public void setLastUpdated(Date lastUpdated) { this.lastUpdated = lastUpdated; } }
下面是我上面的POJO的Hibernate
映射文件
VerificaionCode.hbm.xml
Patient.hbm.xml
Stores the basic information of the patient
但是,当我通过http://localhost:8080/example_rest/rest/verificaion_code/getAllVerificationCodes
运行此代码时,我收到以下错误。
could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->beans.VerificaionCode["patient"]->beans.Patient_$$_jvst40f_7["diabetesType"])
如您所见, diabetesType
是患者表中的Object
(外键),与VerificationCode
无关。 我该如何解决这个问题?
请注意,这是一个REST
API。 所以我不能像在Web应用程序中那样在JSP中加载它们。
更新
正如@Kayaman推荐的那样,我通过制作null
来进行更新。 请检查以下代码。 我注意到verificaionCodes.get(i).getPatient().set...(null)
产生与上面相同的错误,所以我尝试下面开始正常工作。
VerificationCodeService.java
public List getAllVerificationCodes() { Session session = verificationCodeDAOInterface.openCurrentSession(); Transaction transaction = null; List verificaionCodes = new ArrayList(); try { transaction = verificationCodeDAOInterface.openTransaction(session); verificaionCodes = verificationCodeDAOInterface.getAllVerificationCodes(session); transaction.commit(); } catch (Exception ex) { ex.printStackTrace(); } finally { session.close(); for(int i=0;i<verificaionCodes.size();i++) { Patient p = new Patient(); System.out.println(verificaionCodes.get(i).getPatient().getIdpatient()); Integer idpatient = verificaionCodes.get(i).getPatient().getIdpatient(); p.setIdpatient(idpatient); verificaionCodes.get(i).setPatient(p); } } return verificaionCodes; }
- 如何使用JPA / hibernate创建索引,并使用mappedsupperclass中的字段和来自具体实体的字段
- 错误的布尔映射Hibernate(ArrayIndexOutOfBoundsException)
- Hibernate Native SQL Query检索实体和集合
- Hibernate可以返回除List之外的结果对象的集合吗?
- 为什么我的JPA注释类没有隐式发现?
- Hibernate @Proxy(lazy = false)注释有什么作用?
- make Eclipse equals()/ hashCode()使用getters
- 使用Hibernate查询:冒号被视为参数/转义冒号
- 如何为json响应添加和忽略字段