org.hibernate.LazyInitializationException:懒得初始化一个角色集合:,无法初始化代理 - 没有Session

时间:2017-12-22 17:00:39

标签: hibernate rest jersey jax-rs

我开发了一个在java中提供Web服务的应用程序,使用:JAX-RS,Jersey,Hibernate Spring IOC。

我的pom.xml如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ma.recap</groupId>
<artifactId>recap</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>recap</name>
<description>Récap...</description>

<repositories>
    <repository>
        <id>maven2-repository.java.net</id>
        <name>Java.net Repository for Maven</name>
        <url>http://download.java.net/maven/2/</url>
        <layout>default</layout>
    </repository>
</repositories>

<!-- DEPENDENCIES -->
<dependencies>
    <!-- JAVA EE -->
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>7.0</version>
    </dependency>

    <!-- LOG4J -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>

    <!-- HIBERNATE CORE -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.3.11.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.3.11.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.common</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>4.0.4.Final</version>
    </dependency>

    <!-- SLF4J -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.13</version>
    </dependency>

    <!-- MYSQL CONNECTOR -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.9</version>
    </dependency>

    <!-- SPRING -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>1.7.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
    </dependency>
    <dependency>
        <groupId>com.zaxxer</groupId>
        <artifactId>HikariCP</artifactId>
        <version>2.2.5</version>
    </dependency>

    <!-- JERSEY -->
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-bundle</artifactId>
        <version>1.18.1</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.8</version>
    </dependency>
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.7.7</version>
    </dependency>

    <!-- Jersey + Spring -->
    <dependency>
        <groupId>com.sun.jersey.contribs</groupId>
        <artifactId>jersey-spring</artifactId>
        <version>1.8</version>
        <exclusions>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
            </exclusion>
            <exclusion>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-framework-bom</artifactId>
            <version>4.1.4.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<!-- BUILD -->
<build>
    <plugins>
        <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
            </configuration>
        </plugin>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.3</version>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
    </plugins>
</build>

我有两个课程UserAnnouncement

User.java

package ma.recap.entities;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.codehaus.jackson.annotate.JsonProperty;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

import ma.recap.constant.IConstantColumn;
import ma.recap.constant.IConstantTable;

@Entity
@Table(name = IConstantTable.USER)
public class User implements Serializable {

private static final long serialVersionUID = -8129150115850871941L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonProperty
private long id;

@Column(name = IConstantColumn.SSOID, length = 50, nullable = false, unique = true, updatable = true)
@JsonProperty
private String ssoId;

@JsonProperty
@LazyCollection(LazyCollectionOption.TRUE)
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, targetEntity = Announcement.class, mappedBy = "user")
private List<Announcement> announcements = new ArrayList<Announcement>();

// CONSTRUCTORS
public User() {
}

// GETTERS AND SETTERS
}

Annoucement.java

package ma.recap.entities;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.codehaus.jackson.annotate.JsonProperty;

import ma.recap.constant.IConstantColumn;
import ma.recap.constant.IConstantTable;


@Entity
@Table(name = IConstantTable.ANNOUNCEMENT)
public class Announcement implements Serializable {

private static final long serialVersionUID = -1352563627736191841L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@JsonProperty
private long id;

@Column(name = IConstantColumn.TITLE, length = 256, nullable = false, unique = false, updatable = true)
@JsonProperty
private String title;


@JsonProperty
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER, optional = false, targetEntity = User.class)
@JoinColumn(nullable = false, unique = false, updatable = true)
private User user;


// CONSTRUCTORS
public Announcement() {
}

// GETTERS AND SETTERS 
}

我的Web服务通过其ssoid检索用户,并且根据User类的定义,Announcement列表标记为LAZY,因为我不想从中获取数据这个清单。

我的道:

    @Override
    public User findBySSoId(String ssoId) {
         Criteria criteria = getSession().createCriteria(User.class);
         criteria.add(Restrictions.eq("ssoId", ssoId));

         return (User) criteria.uniqueResult();
    }

当我调用WS时,我遇到以下错误:

org.hibernate.LazyInitializationException: failed to lazily initialize a 
collection of role: ma.recap.entities.User.announcements, could not 
initialize proxy - no Session
enter code here

1 个答案:

答案 0 :(得分:0)

当您从DAO方法返回时,会话将关闭,并且所有实体都与其分离。这导致了Lazy init异常。您应该使用交易 - https://docs.spring.io/spring/docs/4.2.x/spring-framework-reference/html/transaction.html