1

mysqlデータベースからのデータをマップするためにhibernateを使用しています。

これは私の質問です:

long id = (Long) session.createQuery("Select category.id from Project as p where p.id =:projectId").setLong("projectId", projectId).uniqueResult();

activities = session.createQuery("Select name from Activity as a where a.parent.id =:activityId").setLong("activityId", id).list();

正常に動作します。これらの2つのクエリを1つのクエリにマージするにはどうすればよいですか?


補遺

アクティビティクラス:

@Entity
public class Activity implements BaseEntry, Comparable<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;

    @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;

プロジェクトクラス:

@Entity
public class Project implements BaseEntry, Comparable<Project> {

    private static final long serialVersionUID = 1911160043392595450L;

    @Id
    @GeneratedValue
    private long id;

    @Column(name = "short", unique = true, nullable = false)
    private String key;

    @Column(nullable = false)
    private String name;

    @ManyToOne(optional = false, fetch = FetchType.EAGER)
    private Activity category;

私が使用した:

activities = session.createQuery("Select a.name from Activity as a where a.parent.id = (Select p.category.id from Project as p where p.id =:projectId)")
                    .setLong("projectId", projectId).list();

そしてエラーが発生しました:

Apache Tomcat/7.0.23-エラーレポート

HTTPステータス500-

タイプ例外レポート

メッセージ

説明サーバーで内部エラー()が発生したため、この要求を実行できませんでした。

例外

org.codehaus.jackson.map.JsonMappingException:プロキシを初期化できませんでした-セッションがありません(参照チェーンを介して: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)

根本的な原因

org.hibernate.LazyInitializationException:プロキシを初期化できませんでした-セッションがありません
    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(ネイティブメソッド)
    sun.reflect.NativeMethodAccessorImpl.invoke(不明なソース)
    sun.reflect.DelegatingMethodAccessorImpl.invoke(不明なソース)
    java.lang.reflect.Method.invoke(不明なソース)
    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)

根本原因の完全なスタックトレースが利用可能です

4

1 に答える 1

0

これらのクエリをマージする最も簡単な方法は、サブクエリを使用することです。

Select a.name from Activity as a where a.parent.id = 
    (Select p.category.id from Project as p where p.id =:projectId)
于 2012-09-08T12:51:49.823 に答える