我正在使用休眠将数据映射到我在 Restful Web 服务中的 mysql 数据库(使用球衣)。
这是我的活动课:
@Entity
public class Activity {
private static final long serialVersionUID = 4937176332500551910L;
@Id
@GeneratedValue
private long id;
@Column(name = "short", unique = true, nullable = false)
private String key;
private String name;
private String description;
private float cost;
private int orderIndex = 0;
@ManyToOne(fetch = FetchType.LAZY)
private Activity parent;
@OneToMany(targetEntity = Activity.class, mappedBy = "parent", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JsonIgnore
private Collection<Activity> children;
@OneToMany(targetEntity = Project.class, mappedBy = "category", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JsonIgnore
private Collection<Project> projects;
@OneToMany(targetEntity = Task.class, mappedBy = "activity", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JsonIgnore
private Collection<Task> tasks;
此查询完美运行:
activities = session.createQuery("from Activity").list();
Json 数组的结果是:
[{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null},{"id":101,"key":"c","name":"Consulting","description":"Just in special cases, authorized by Rüdiger after discussion with customer.","cost":990.0,"orderIndex":3,"parent":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null}},{"id":102,"key":"d","name":"Development","description":"Usually this.","cost":690.0,"orderIndex":2,"parent":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null}},{"id":103,"key":"sales","name":"Sales","description":"Sales related activities","cost":0.0,"orderIndex":2,"parent":null},{"id":104,"key":"doit","name":"Genomför arbeten","description":"","cost":690.0,"orderIndex":1,"parent":{"id":103,"key":"sales","name":"Sales","description":"Sales related activities","cost":0.0,"orderIndex":2,"parent":null}},{"id":105,"key":"t","name":"Traveling","description":"Travel time","cost":300.0,"orderIndex":4,"parent":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null}},{"id":106,"key":"uni","name":"University Job","description":"Activities at university","cost":690.0,"orderIndex":3,"parent":null},{"id":107,"key":"work","name":"work","description":"One rate for everything: teaching, preparing, administration, meetings, etc.","cost":690.0,"orderIndex":1,"parent":{"id":106,"key":"uni","name":"University Job","description":"Activities at university","cost":690.0,"orderIndex":3,"parent":null}},{"id":108,"key":"v","name":"Vacation/Holidays","description":"Planned vacation (counting agains the 25 days)","cost":300.0,"orderIndex":5,"parent":{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null}}]
此查询也有效:
activities = session.createQuery("from Activity where id=100").list();
结果是:
[{"id":100,"key":"scrum","name":"Scrum","description":"","cost":0.0,"orderIndex":1,"parent":null}]
scrum
如您所见,活动是父母。
但此查询不起作用:
activities = session.createQuery("from Activity where id=101").list();
因为 id=101 的 Activity 是一种 Child Activity。
我收到了这个错误:
<html><head><title>Apache Tomcat/7.0.23 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 500 - </h1><HR size="1" noshade="noshade"><p><b>type</b> Exception report</p><p><b>message</b> <u></u></p><p><b>description</b> <u>The server encountered an internal error () that prevented it from fulfilling this request.</u></p><p><b>exception</b> <pre>org.codehaus.jackson.map.JsonMappingException: could not initialize proxy - no Session (through reference chain: java.util.ArrayList[0]->se.softwerk.timelog.model.Activity["parent"]->se.softwerk.timelog.model.Activity_$$_javassist_3["id"])
org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:218)
org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:183)
org.codehaus.jackson.map.ser.std.SerializerBase.wrapAndThrow(SerializerBase.java:140)
org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:158)
org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
org.codehaus.jackson.map.ser.std.StdContainerSerializers$IndexedListSerializer.serializeContents(StdContainerSerializers.java:122)
org.codehaus.jackson.map.ser.std.StdContainerSerializers$IndexedListSerializer.serializeContents(StdContainerSerializers.java:71)
org.codehaus.jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86)
org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:610)
org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:256)
org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:1604)
org.codehaus.jackson.jaxrs.JacksonJsonProvider.writeTo(JacksonJsonProvider.java:558)
com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy.writeTo(JacksonProviderProxy.java:160)
com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306)
com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1437)
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
</pre></p><p><b>root cause</b> <pre>org.hibernate.LazyInitializationException: could not initialize proxy - no Session
org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:149)
org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:195)
org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:185)
se.softwerk.timelog.model.Activity_$$_javassist_3.getId(Activity_$$_javassist_3.java)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.codehaus.jackson.map.ser.BeanPropertyWriter.get(BeanPropertyWriter.java:483)
org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:418)
org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
org.codehaus.jackson.map.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:446)
org.codehaus.jackson.map.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:150)
org.codehaus.jackson.map.ser.BeanSerializer.serialize(BeanSerializer.java:112)
org.codehaus.jackson.map.ser.std.StdContainerSerializers$IndexedListSerializer.serializeContents(StdContainerSerializers.java:122)
org.codehaus.jackson.map.ser.std.StdContainerSerializers$IndexedListSerializer.serializeContents(StdContainerSerializers.java:71)
org.codehaus.jackson.map.ser.std.AsArraySerializerBase.serialize(AsArraySerializerBase.java:86)
org.codehaus.jackson.map.ser.StdSerializerProvider._serializeValue(StdSerializerProvider.java:610)
org.codehaus.jackson.map.ser.StdSerializerProvider.serializeValue(StdSerializerProvider.java:256)
org.codehaus.jackson.map.ObjectMapper.writeValue(ObjectMapper.java:1604)
org.codehaus.jackson.jaxrs.JacksonJsonProvider.writeTo(JacksonJsonProvider.java:558)
com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy.writeTo(JacksonProviderProxy.java:160)
com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:306)
com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1437)
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)
com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
</pre></p><p><b>note</b> <u>The full stack trace of the root cause is available in the Apache Tomcat/7.0.23 logs.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/7.0.23</h3></body></html>
你能帮助我吗?