2

サーバーとして実行するとエラーが発生しました。これは私のエラーです:Tomcat HTTPステータス500 - プロキシを初期化できませんでした - セッションなし

HTTP Status 500 - could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->it.jack.fdd.model.Staff["staffType"]->it.jack.fdd.model.StaffType_$$_jvstbaa_15["type"]) 
 

 

 
type Exception report 
 

 
message could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->it.jack.fdd.model.Staff["staffType"]->it.jack.fdd.model.StaffType_$$_jvstbaa_15["type"]) 
 

 
description The server encountered an internal error that prevented it from fulfilling this request. 
 

 
exception 
 
org.codehaus.jackson.map.JsonMappingException: could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->it.jack.fdd.model.Staff["staffType"]->it.jack.fdd.model.StaffType_$$_jvstbaa_15["type"]) 
 
\t org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:218) 
 
\t org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:183) 
 
\t org.codehaus.jackson.map.ser.std.SerializerBase.wrapAndThrow(SerializerBase.java:140) 
 
\t org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:158) 
 
\t org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112) 
 
\t org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446) 
 
\t org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150) 
 
\t org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112) 
 
\t org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:72) 
 
\t org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:23) 
 
\t org.codehaus.jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86) 
 
\t org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:659) 
 
\t org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:271) 
 
\t org.codehaus.jackson.map.ObjectWriter.writeValue(ObjectWriter.java:325) 
 
\t org.codehaus.jackson.jaxrs.JacksonJsonProvider.writeTo(JacksonJsonProvider.java:556) 
 
\t com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy.writeTo(JacksonProviderProxy.java:160) 
 
\t com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:302) 
 
\t com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1510) 
 
\t com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419) 
 
\t com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409) 
 
\t com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409) 
 
\t com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558) 
 
\t com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733) 
 
\t javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
 
\t org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
 

 

 
root cause 
 
org.hibernate.LazyInitializationException: could not initialize proxy - no Session 
 
\t org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:165) 
 
\t org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:286) 
 
\t org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185) 
 
\t it.jack.fdd.model.StaffType_$$_jvstbaa_15.getType(StaffType_$$_jvstbaa_15.java) 
 
\t sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
 
\t sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 
 
\t sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
 
\t java.lang.reflect.Method.invoke(Method.java:497) 
 
\t org.codehaus.jackson.map.ser.BeanPropertyWriter.get(BeanPropertyWriter.java:483) 
 
\t org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:418) 
 
\t org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150) 
 
\t org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112) 
 
\t org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446) 
 
\t org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150) 
 
\t org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112) 
 
\t org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:72) 
 
\t org.codehaus.jackson.map.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:23) 
 
\t org.codehaus.jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86) 
 
\t org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:659) 
 
\t org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:271) 
 
\t org.codehaus.jackson.map.ObjectWriter.writeValue(ObjectWriter.java:325) 
 
\t org.codehaus.jackson.jaxrs.JacksonJsonProvider.writeTo(JacksonJsonProvider.java:556) 
 
\t com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy.writeTo(JacksonProviderProxy.java:160) 
 
\t com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:302) 
 
\t com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1510) 
 
\t com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1419) 
 
\t com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1409) 
 
\t com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:409) 
 
\t com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:558) 
 
\t com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:733) 
 
\t javax.servlet.http.HttpServlet.service(HttpServlet.java:729) 
 
\t org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

はそれをチェックアウト:java.util.ArrayList[0]->it.jack.fdd.model.Staff["staffType"]->it.jack.fdd.model.StaffType_$$_jvstbaa_15["type"])

を。これは、私は2つのモデルクラスを持っているので、私のデータベースの二つのテーブルに参加している、この2つのクラスは、次のとおりです。

Staff.java:

package it.jack.fdd.model; 
 
// Generated 30-nov-2016 0.17.09 by Hibernate Tools 4.3.1.Final 
 

 
import java.util.Date; 
 
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 javax.persistence.Temporal; 
 
import javax.persistence.TemporalType; 
 
import javax.xml.bind.annotation.XmlRootElement; 
 

 
import org.codehaus.jackson.annotate.JsonIgnore; 
 

 
/** 
 
* Staff generated by hbm2java 
 
*/ 
 
@XmlRootElement 
 
@Entity 
 
@Table(name = "staff", catalog = "fdd_dbproducts") 
 
public class Staff implements java.io.Serializable { 
 

 
\t private Integer idstaff; 
 
\t private StaffType staffType; 
 
\t private String name; 
 
\t private String surname; 
 
\t private Date birthDate; 
 
\t private String phone; 
 
\t private boolean gender; 
 
\t private StaffLogin staffLogin; 
 
\t private Set<RtStaffDispenser> rtStaffDispensers = new HashSet<RtStaffDispenser>(0); 
 

 
\t public Staff() { 
 
\t } 
 

 
\t public Staff(StaffType staffType, String name, String surname, Date birthDate, String phone, boolean gender) { 
 
\t \t this.staffType = staffType; 
 
\t \t this.name = name; 
 
\t \t this.surname = surname; 
 
\t \t this.birthDate = birthDate; 
 
\t \t this.phone = phone; 
 
\t \t this.gender = gender; 
 
\t } 
 

 
\t public Staff(StaffType staffType, String name, String surname, Date birthDate, String phone, boolean gender, 
 
\t \t \t StaffLogin staffLogin, Set<RtStaffDispenser> rtStaffDispensers) { 
 
\t \t this.staffType = staffType; 
 
\t \t this.name = name; 
 
\t \t this.surname = surname; 
 
\t \t this.birthDate = birthDate; 
 
\t \t this.phone = phone; 
 
\t \t this.gender = gender; 
 
\t \t this.staffLogin = staffLogin; 
 
\t \t this.rtStaffDispensers = rtStaffDispensers; 
 
\t } 
 

 
\t @Id 
 
\t @GeneratedValue(strategy = IDENTITY) 
 

 
\t @Column(name = "idstaff", unique = true, nullable = false) 
 
\t public Integer getIdstaff() { 
 
\t \t return this.idstaff; 
 
\t } 
 

 
\t public void setIdstaff(Integer idstaff) { 
 
\t \t this.idstaff = idstaff; 
 
\t } 
 

 
\t @ManyToOne(fetch = FetchType.LAZY) 
 
\t @JoinColumn(name = "fkstaff_type_staff", nullable = false) 
 
\t @JsonIgnore 
 
\t public StaffType getStaffType() { 
 
\t \t return this.staffType; 
 
\t } 
 

 
\t public void setStaffType(StaffType staffType) { 
 
\t \t this.staffType = staffType; 
 
\t } 
 

 
\t @Column(name = "name", nullable = false, length = 45) 
 
\t public String getName() { 
 
\t \t return this.name; 
 
\t } 
 

 
\t public void setName(String name) { 
 
\t \t this.name = name; 
 
\t } 
 

 
\t @Column(name = "surname", nullable = false, length = 45) 
 
\t public String getSurname() { 
 
\t \t return this.surname; 
 
\t } 
 

 
\t public void setSurname(String surname) { 
 
\t \t this.surname = surname; 
 
\t } 
 

 
\t @Temporal(TemporalType.DATE) 
 
\t @Column(name = "birth_date", nullable = false, length = 10) 
 
\t public Date getBirthDate() { 
 
\t \t return this.birthDate; 
 
\t } 
 

 
\t public void setBirthDate(Date birthDate) { 
 
\t \t this.birthDate = birthDate; 
 
\t } 
 

 
\t @Column(name = "phone", nullable = false, length = 45) 
 
\t public String getPhone() { 
 
\t \t return this.phone; 
 
\t } 
 

 
\t public void setPhone(String phone) { 
 
\t \t this.phone = phone; 
 
\t } 
 

 
\t @Column(name = "gender", nullable = false) 
 
\t public boolean isGender() { 
 
\t \t return this.gender; 
 
\t } 
 

 
\t public void setGender(boolean gender) { 
 
\t \t this.gender = gender; 
 
\t } 
 

 
\t @OneToOne(fetch = FetchType.LAZY, mappedBy = "staff") 
 
\t @JsonIgnore 
 
\t public StaffLogin getStaffLogin() { 
 
\t \t return this.staffLogin; 
 
\t } 
 

 
\t public void setStaffLogin(StaffLogin staffLogin) { 
 
\t \t this.staffLogin = staffLogin; 
 
\t } 
 

 
\t @OneToMany(fetch = FetchType.LAZY, mappedBy = "staff") 
 
\t @JsonIgnore 
 
\t public Set<RtStaffDispenser> getRtStaffDispensers() { 
 
\t \t return this.rtStaffDispensers; 
 
\t } 
 

 
\t public void setRtStaffDispensers(Set<RtStaffDispenser> rtStaffDispensers) { 
 
\t \t this.rtStaffDispensers = rtStaffDispensers; 
 
\t } 
 

 
}

とStaffType.java:

package it.jack.fdd.model; 
 
// Generated 30-nov-2016 0.17.09 by Hibernate Tools 4.3.1.Final 
 

 
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.OneToMany; 
 
import javax.persistence.Table; 
 
import javax.xml.bind.annotation.XmlRootElement; 
 

 
import org.codehaus.jackson.annotate.JsonIgnore; 
 

 
/** 
 
* StaffType generated by hbm2java 
 
*/ 
 
@XmlRootElement 
 
@Entity 
 
@Table(name = "staff_type", catalog = "fdd_dbproducts") 
 
public class StaffType implements java.io.Serializable { 
 

 
\t private Integer idstaffType; 
 
\t private String type; 
 
\t private Set<Staff> staffs = new HashSet<Staff>(0); 
 

 
\t public StaffType() { 
 
\t } 
 

 
\t public StaffType(String type) { 
 
\t \t this.type = type; 
 
\t } 
 

 
\t public StaffType(String type, Set<Staff> staffs) { 
 
\t \t this.type = type; 
 
\t \t this.staffs = staffs; 
 
\t } 
 

 
\t @Id 
 
\t @GeneratedValue(strategy = IDENTITY) 
 

 
\t @Column(name = "idstaff_type", unique = true, nullable = false) 
 
\t public Integer getIdstaffType() { 
 
\t \t return this.idstaffType; 
 
\t } 
 

 
\t public void setIdstaffType(Integer idstaffType) { 
 
\t \t this.idstaffType = idstaffType; 
 
\t } 
 

 
\t @Column(name = "type", nullable = false, length = 45) 
 
\t public String getType() { 
 
\t \t return this.type; 
 
\t } 
 

 
\t public void setType(String type) { 
 
\t \t this.type = type; 
 
\t } 
 

 
\t @OneToMany(fetch = FetchType.LAZY, mappedBy = "staffType") 
 
\t @JsonIgnore 
 
\t public Set<Staff> getStaffs() { 
 
\t \t return this.staffs; 
 
\t } 
 

 
\t public void setStaffs(Set<Staff> staffs) { 
 
\t \t this.staffs = staffs; 
 
\t } 
 

 
}

そして私はスタッフとStaffType間のいずれかに多くの関係を持っているので、私はこのエラーを持っています。実際に

@OneToMany(fetch = FetchType.LAZY, mappedBy = "staffType") 
@JsonIgnore 
public Set<Staff> getStaffs() { 
    return this.staffs; 
} 

は、使用してそれが動作@JsonIgnore: これが問題です!私の質問は、@ JsonIgnoreなしでどうすれば修正できますか? @JsonIgnoreを使用すると、結果のjsonファイルにfkstaff_type_staffという外部キーが含まれないためです。どのように私はそれを修正することができます?おかげさまで

PS:これは私のBaseDaoImpl.javaです:

package it.jack.fdd.dao.impl; 
 

 
import it.jack.fdd.dao.interfaces.BaseDao; 
 
import it.jack.fdd.model.Product; 
 
import it.jack.fdd.model.Staff; 
 
import it.jack.fdd.model.User; 
 
import it.jack.fdd.util.HibernateUtilLezione; 
 

 
import java.io.Serializable; 
 
import java.util.Calendar; 
 
import java.util.Date; 
 
import java.util.List; 
 

 
import org.hibernate.HibernateException; 
 
import org.hibernate.Query; 
 
import org.hibernate.Session; 
 
import org.hibernate.SessionFactory; 
 
import org.hibernate.Transaction; 
 

 
public class BaseDaoImpl<T> implements BaseDao<T> { 
 

 
\t public BaseDaoImpl() { 
 
\t \t super(); 
 
\t } 
 

 
\t /* (non-Javadoc) 
 
\t * @see it.unisalento.se.dao.impl.BaseDao#save(T) 
 
\t */ 
 
\t public int save(T entity) { 
 
\t 
 
\t \t int id = 0; 
 
\t \t 
 
\t \t try { 
 
\t \t \t 
 
\t \t \t 
 
\t \t \t /* 
 
\t \t \t * DIFFERENZA TRA openSession() e getCurrentSession): 
 
\t \t \t * 
 
\t \t \t \t When you create a hibernate session using any of the sessionFactory.openSession(...) 
 
\t \t \t \t methods the session factory will 'bind' the session to the current context. 
 
\t \t \t \t The default context is 'thread' which means the sesion factory will bind the session 
 
\t \t \t \t to the thread from which openSession(...) is called. 
 

 
\t \t \t \t This is useful because you can later call sessionFactory.getCurrentSession() which will 
 
\t \t \t \t return the session that is bound to the currently running thread. 
 

 
\t \t \t \t You can use other predefined current_session_context_class values such as 'jta' which will 
 
\t \t \t \t bind the session to the currently running JTA transaction (if you're running in an 
 
\t \t \t \t application server that supports JTA this is really useful). Or you can write your 
 
\t \t \t \t own implementation of org.hibernate.context.CurrentSessionContext and use that implementation 
 
\t \t \t \t to manage the current session context (not really advisable unless you absolutely need to). 
 
\t \t \t * 
 
\t \t \t */ 
 
\t \t \t 
 
\t \t \t 
 
\t \t \t //Prep work 
 
\t \t \t //SessionFactory sessionFactory = HibernateUtil.getSessionFactory(); 
 
\t \t \t 
 
\t \t \t Session session = HibernateUtilLezione.openSession(); //dall'esempio mio fa getCurrentSession() 
 
\t \t \t Transaction tx = session.beginTransaction(); 
 
\t \t \t 
 
\t \t \t id = (Integer) session.save(entity); 
 
\t \t \t tx.commit(); 
 
\t \t \t session.close(); 
 
\t \t }catch(HibernateException e){ 
 
\t \t \t e.printStackTrace(); 
 
\t \t } 
 
\t \t return id; 
 
\t } 
 

 
\t /* (non-Javadoc) 
 
\t * @see it.unisalento.se.dao.impl.BaseDao#getAll(java.lang.Class) 
 
\t */ 
 
\t public List<T> getAll(Class clazz) { 
 
\t \t 
 
\t \t Session session = HibernateUtilLezione.openSession(); 
 
\t \t Transaction tx = session.beginTransaction(); 
 
\t \t 
 
\t \t @SuppressWarnings("unchecked") 
 
\t \t List<T> list = session.createQuery("from "+clazz.getName()).list(); 
 
\t \t tx.commit(); 
 
\t \t session.close(); 
 
\t \t \t \t 
 
\t \t return list; 
 
\t } 
 

 
\t /* (non-Javadoc) 
 
\t * @see it.unisalento.se.dao.impl.BaseDao#getById(int, java.lang.Class) 
 
\t */ 
 
\t public T getById(int id, Class clazz) { 
 
\t \t try{ 
 
\t \t 
 
\t \t \t Session session = HibernateUtilLezione.openSession(); 
 
\t \t \t Transaction tx = session.beginTransaction(); 
 
\t \t \t 
 
\t \t \t @SuppressWarnings("unchecked") 
 
\t \t \t T returnObj = (T) session.get(clazz.getName(), id); 
 
\t \t \t /* 
 
\t \t \t System.out.println("vediamo "+session.getIdentifier(returnObj)); 
 
\t \t \t System.out.println("vediamo2 "+session.getEntityName(returnObj)); 
 
\t \t \t */ 
 
\t \t \t tx.commit(); 
 
\t \t \t session.close(); 
 
\t \t \t 
 
\t \t \t return returnObj; 
 
\t \t } 
 
\t \t catch(HibernateException e){ 
 
\t \t \t e.printStackTrace(); 
 
\t \t \t return null; 
 
\t \t } 
 
\t \t 
 
\t } 
 
\t 
 
\t /* (non-Javadoc) 
 
\t * @see it.unisalento.se.dao.impl.BaseDao#delete(int, java.lang.Class) 
 
\t */ 
 
\t public void delete (int id, Class clazz){ 
 
\t \t 
 
\t \t Session session = HibernateUtilLezione.openSession(); 
 
\t \t Transaction tx = session.beginTransaction(); 
 
\t \t 
 
\t \t T t = (T) session.get(clazz, id); 
 
\t \t session.delete(t); 
 
\t \t tx.commit(); 
 
\t \t session.close(); 
 
\t \t 
 
\t } 
 

 

 
/* 
 
\t public Serializable getByUID(Staff stf) { 
 
\t \t // TODO Auto-generated method stub 
 
\t \t return null; 
 
\t } 
 
\t */ 
 

 
\t public void update(T entity) { 
 
\t \t try { 
 
\t \t \t Session session = HibernateUtilLezione.openSession(); 
 
\t \t \t Transaction tx = session.beginTransaction(); 
 
\t \t \t \t \t 
 
\t \t \t session.update(entity); 
 
\t \t \t tx.commit(); 
 
\t \t \t session.close(); 
 
\t \t } 
 
\t \t catch(HibernateException e){ 
 
\t \t \t e.printStackTrace(); 
 
\t \t } 
 
\t \t 
 
\t } 
 
\t 
 
}

答えて

2

JPAセッションが閉じた後のシリアル化が起こっています。ジャクソンはレイジーフィールドにアクセスしようとしますが、セッションが閉じられているため例外がスローされます。

どちらか:

  • は完全にちょうどJPAセッションが終了する前にそれにアクセスすることによって、フィールドを読み込み、または
  • は熱心なフィールド(fetch = FetchType.EAGER
+0

おかげで、どのように私ができるを宣言するを宣言しますフィールドは熱心?あなたは私に例を挙げることができますか? –

+0

(BaseDaoImpl.javaが追加されました) –

+0

フィールドの注釈に 'fetch = FetchType.EAGER'があります。 – Radu

関連する問題