2017-12-22 41 views
0

JAX-RS、Jersey、Hibernate Spring IOCを使用して、JavaでWebサービスを提供するアプリケーションを開発します。org.hibernate.LazyInitializationException:遅延コレクションの遅延初期化に失敗しました。プロキシを初期化できませんでした - セッションなし

次のように私の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> 

私は2つのクラス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としてマークされています。

マイDAO:私はWSを呼び出すとき

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

     return (User) criteria.uniqueResult(); 
    } 

は、私は次のエラーを持っている:あなたはDAOメソッドから戻ると

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

このスレッドを確認してください。 https://stackoverflow.com/questions/21574236/org-hibernate-lazyinitializationexception-could-not-initialize-proxy-no-sess @Transactionalでクラスに注釈を付ける –

+0

ありがとうございます!私は問題をgetCurrentSession()の代わりにopenSession()を使って解決しました。 – Mahmoud

答えて

関連する問題