懒惰地初始化角色集合无法初始化代理 - 没有会话

时间:2014-10-27 18:27:06

标签: java spring hibernate session jpa

我正在使用spring mvc并使用spring数据jpa,并且我有这个错误failed to lazily initialize a collection of role could not initialize proxy - no Session我知道发生这种情况是因为我没有开放会话但我不知道如何连接到数据库后保持我的会话打开这是我目前的代码:

我的jpa配置类

@Configuration
@EnableSpringConfigured
@ComponentScan( basePackages = {"com.abc.domain", "com.abc.repository", "com.abc.service","com.abc.authenticate"})
@EnableJpaRepositories(basePackages="com.abc.repository")
public class ConfigJPA 
{

       @Bean
       public LocalContainerEntityManagerFactoryBean entityManagerFactory() 
               throws ClassNotFoundException 
       {

          LocalContainerEntityManagerFactoryBean  em  = 
                  new LocalContainerEntityManagerFactoryBean();

          em.setDataSource( dataSource() );
          em.setPackagesToScan("com.abc.domain");
          em.setPersistenceProviderClass(HibernatePersistence.class);
          em.setJpaProperties( asignarPropiedades() );

          return em;
       }

       //Propiedades Hibernate
       Properties asignarPropiedades() {

           Properties jpaProperties = new Properties();

           jpaProperties.put("hibernate.dialect", "org.hibernate.dialect.Oracle10gDialect");
           jpaProperties.put("hibernate.format_sql", true);
           jpaProperties.put("hibernate.ejb.naming_strategy", "org.hibernate.cfg.ImprovedNamingStrategy");
           jpaProperties.put("hibernate.show_sql", true);

           return jpaProperties;
       }


       @Bean
       public DataSource dataSource(){

          DriverManagerDataSource dataSource = 
                  new DriverManagerDataSource();

          dataSource.setDriverClassName("oracle.jdbc.driver.OracleDriver");

          //farmatodo22
            dataSource.setUrl("jdbc:oracle:thin:@127.0.0.1:1521:XE");
            dataSource.setUsername("DATBASE");
            dataSource.setPassword("mypassword");

          return dataSource;
       }

       @Bean
       public JpaTransactionManager transactionManager() 
               throws ClassNotFoundException 
       {

            JpaTransactionManager transactionManager = 
                    new JpaTransactionManager();

            transactionManager.setEntityManagerFactory(entityManagerFactory().getObject());

            return transactionManager;
        }
}

这是我的域类

用户类

@Entity
@Table(name="User")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;


    //**PRIMARY KEY**//

    @Id
    @SequenceGenerator(name="User_id_seq", sequenceName="SEQ_User")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="User_id_seq")
    @Column(name="ID_USER", unique=true, nullable=false, precision=8)
    private Long idUser;




    @ManyToOne
    @JoinColumn(name="id_ldap_server", nullable = false)
    private ServerLdap serverLdap;




    @ManyToMany
    @JoinTable
    (
      name="Usuario_Rol",
      joinColumns = 
      {
        @JoinColumn (name="ID_USER", referencedColumnName="ID_USER")
      },
      inverseJoinColumns = 
      {
        @JoinColumn (name="id_rol", referencedColumnName="id_rol")
      }
    )
    private List<Rol> roles;

这是我的角色类

    @Entity
    @Table(name="ROL")
    public class Rol implements Serializable 
    {

    private static final long serialVersionUID = 1L;


    //***PRIMARY KEY***///

    @Id
    @SequenceGenerator(name="ROL_ID_GENERATOR", sequenceName="SEQ_ROL")
    @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="ROL_ID_GENERATOR")
    @Column(name="id_rol", unique=true, nullable=false, precision=8)
    private Long idRol;




    @ManyToMany(mappedBy="roles")
    private List<User> users;

}

我试图在User和Rol之间做一个ManyToMany实现,如果我把fetchType = EAGER但我不想使用那个fetch类型,因为我觉得效率不高,这个工作很好。

1 个答案:

答案 0 :(得分:1)

其中一个选项是使用在视图中打开会话,但它有一些缺点,被一些人认为是一种不好的做法。我建议阅读以下SO帖子以做出明智的选择。