Reputation: 1695
I'm working with spring mvc and using spring data jpa, and i'm having this error failed to lazily initialize a collection of role could not initialize proxy - no Session
i know that this happens because i dont have a open session but i dont know how to keep my session open after i connect to the database this is my code so far:
My jpa config class
@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;
}
}
this are my domain classes
User class
@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;
This is my role class
@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;
}
I'm trying to make a ManyToMany realtion between User and Rol, this work nice if i put fetchType = EAGER but i dont want to use that fetch type because i think is not efficient.
Upvotes: 1
Views: 3691
Reputation: 3129
One of the options is to use is Open Session in View but it had few drawbacks and is considered as a bad practice by several people. I would suggest reading the following SO posts to make an informed choice.
Upvotes: 1