This is the action of the code:
package org.demo.action;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import org.apache.struts2.ServletActionContext;
import org.demo.service.ISpecialtyService;
public class SpecialtyAction {
private ISpecialtyService specialtyservice;
public String getSpecialty () {
HttpServletRequest request = ServletActionContext.getRequest ();
String collegeName = request.getParameter ("collegeName");
HttpServletResponse response = ServletActionContext.getResponse ();
response.setCharacterEncoding ("UTF-8");
PrintWriter out;
try {
JSONArray jsonlist = JSONArray.fromObject (specialtyservice.getSpecialty (collegeName));
out = response.getWriter ();
out.println (jsonlist);
out.flush ();
out.close ();
} Catch (Exception e) {
/ / TODO Auto-generated catch block
e.printStackTrace ();
}
return null;
}
public ISpecialtyService getSpecialtyservice () {
return specialtyservice;
}
public void setSpecialtyservice (ISpecialtyService specialtyservice) {
this.specialtyservice = specialtyservice;
}
}
</ Pre>
This is the dao layer code:
package org.demo.dao.impl;
import java.util.List;
import org.demo.dao.BaseDAO;
import org.demo.dao.ISpecialtyDAO;
import org.demo.vo.Specialty;
import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
public class SpecialtyDAO extends BaseDAO implements ISpecialtyDAO {
public List getSpecialty (String college) {
Session session = getSession ();
String hql = "from Specialty s where s.college.name = '" + college + "'";
Query query = session.createQuery (hql);
List specialty = query.list ();
if (specialty! = null &&! specialty.isEmpty ()) {
for (Specialty spe: specialty) {
Hibernate.initialize (spe.getCollege ());
}
}
session.close ();
return specialty;
}
}
</ Pre>
This is the entity's configuration file:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="org.demo.vo.Specialty" table="specialty" catalog="ws_demo">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<many-to-one name="college" class="org.demo.vo.College" fetch="select" >
<column name="college" not-null="true" />
</many-to-one>
<property name="name" type="java.lang.String">
<column name="name" not-null="true" />
</property>
<property name="desc" type="java.lang.String">
<column name="desc" not-null="true" />
</property>
</class>
</hibernate-mapping>
I dao layer has been loaded before the session is closed related entities college property, in the action layer code can query the attributes associated with an entity, but converted into json or incorrect.
net.sf.json.JSONException: org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: org.demo.vo.College.specialties, no session or session was closed
没有评论:
发表评论