![](/img/trans.png)
[英]Hibernate - failed to lazily initialize a collection of role: could not initialize proxy - no Session
[英]Failed to lazily initialize a collection of role could not initialize proxy - no Session
我正在使用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放进去的话,这个工作就很好了,但是我不想使用该访存类型,因为我认为效率不高。
其中一种选择是使用View中的Open Session,但它没有什么缺点,并且被许多人认为是不好的做法。 我建议阅读以下SO帖子以做出明智的选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.