i have a problem with Spring-data-jpa project.
JavaConfig file
...
@Configuration
@EnableJpaRepositories("it.myproject.data")
@EnableTransactionManagement(mode = AdviceMode.ASPECTJ, proxyTargetClass = true)
@PropertySource("classpath:/it/myproject/application.properties")
public
class DBConfig
{
private static final
String PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN = "entitymanager.packages.to.scan";
@Resource
private
Environment environment;
@Bean
public
DataSource dataSource()
{
EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
return builder.setType(EmbeddedDatabaseType.H2).build();
}
@Bean
public
EntityManagerFactory entityManagerFactory()
{
HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
vendorAdapter.setGenerateDdl(true);
vendorAdapter.setShowSql(true);
LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
factory.setJpaVendorAdapter(vendorAdapter);
factory.setPackagesToScan(environment.getRequiredProperty(PROPERTY_NAME_ENTITYMANAGER_PACKAGES_TO_SCAN));
factory.setDataSource(dataSource());
factory.afterPropertiesSet();
return factory.getObject();
}
@Bean
public
PlatformTransactionManager transactionManager()
{
JpaTransactionManager txManager = new JpaTransactionManager();
txManager.setEntityManagerFactory(entityManagerFactory());
return txManager;
}
}
To test that all works fine I created this method
@Transactional
public
void doIt()
{
PersonDTO created = new PersonDTO();
created.setId(null);
created.setFirstName("Pluto");
created.setLastName("Paperino");
Person pippo= repositoryPersonService.create(created);
for (int i = 0; i < 10; i++) {
BookDTO bookDTO = new BookDTO();
bookDTO.setTitle("Fantasia" + i);
bookDTO.setPerson(pippo);
repositoryBookService.create(bookDTO);
}
repositoryPersonService.findAll().stream().forEach((Person t) -> {
System.out.println(t.getBooks());
});
}
My entity is:
@Entity
@Table(name = "persons")
public
class Person
implements Serializable
{
private static final
long serialVersionUID = 198765467898765L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private
Long id;
@Column(name = "creation_time", nullable = false)
@Temporal(javax.persistence.TemporalType.DATE)
private
Date creationTime;
@Column(name = "first_name", nullable = false)
private
String firstName;
@Column(name = "last_name", nullable = false)
private
String lastName;
@Column(name = "modification_time", nullable = false)
@Temporal(javax.persistence.TemporalType.DATE)
private
Date modificationTime;
@OneToMany(fetch = FetchType.LAZY)
private
List<Book> books;
@Version
private
long version = 0;
But i received this error message:
2014-04-09 12:31:54 TRACE LazyInitializationException:53 - failed to lazily initialize a collection of role: it.myproject.data.person.Person.books, could not initialize proxy - no Session org.hibernate.LazyInitializationException: failed to lazily initialize a collection of role: it.myproject.data.person.Person.books, could not initialize proxy - no Session
Can you help me? Thank you
repositoryPersonService.findAll()normally (i.e. without using a stream)?