我有以下實體:LazyInitializationException中使用Spring數據JpaRepositories
用戶:
@Entity
public class User {
@Id
@Column(nullable = false)
private String email = "";
@Column(nullable = false)
private String nickname = "";
@Column(nullable = false)
private String password = "";
@ManyToMany(cascade = CascadeType.ALL)
private List<NewsSource> newsSources;
// getters and setters
}
新聞來源:
@Entity
public class NewsSource {
@Id
@Column(nullable = false)
private URL url;
private LocalDateTime updateTime;
@OneToMany(cascade = CascadeType.ALL)
private List<News> newses;
@ManyToMany(cascade = CascadeType.ALL)
private List<User> users;
}
UsersRepository和NewsSourcesRepository從春數據JPA簡單JpaRepositories。它們的配置情況如下:
@Configuration
@EnableTransactionManagement
@PropertySource("classpath:database_config.properties")
@EnableJpaRepositories(basePackages = {"news.repositories" })
public class RepositoriesConfiguration {
@Bean(destroyMethod = "close")
DataSource dataSource(Environment env) {
HikariConfig dataSourceConfig = new HikariConfig();
dataSourceConfig.setDriverClassName(env.getRequiredProperty("db.driver"));
dataSourceConfig.setJdbcUrl(env.getRequiredProperty("db.url"));
dataSourceConfig.setUsername(env.getRequiredProperty("db.username"));
dataSourceConfig.setPassword(env.getRequiredProperty("db.password"));
return new HikariDataSource(dataSourceConfig);
}
@Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, Environment env) {
LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
entityManagerFactoryBean.setDataSource(dataSource);
entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
entityManagerFactoryBean.setPackagesToScan("pl.mielecmichal.news.entities");
Properties jpaProperties = new Properties();
jpaProperties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect"));
jpaProperties.put("hibernate.hbm2ddl.auto", env.getRequiredProperty("hibernate.hbm2ddl.auto"));
jpaProperties.put("hibernate.ejb.naming_strategy", env.getRequiredProperty("hibernate.ejb.naming_strategy"));
jpaProperties.put("hibernate.show_sql", env.getRequiredProperty("hibernate.show_sql"));
jpaProperties.put("hibernate.format_sql", env.getRequiredProperty("hibernate.format_sql"));
entityManagerFactoryBean.setJpaProperties(jpaProperties);
return entityManagerFactoryBean;
}
@Bean
JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
JpaTransactionManager transactionManager = new JpaTransactionManager();
transactionManager.setEntityManagerFactory(entityManagerFactory);
return transactionManager;
}
}
我的測試拋出一個LazyInitializationException中上線15的消息是:
未能懶洋洋地初始化角色的集合: news.entities.users.User。 newsSources,不能 初始化代理 - 沒有會話
@Test
public void cascadeRelationsShouldBeRetrieved() throws MalformedURLException {
NewsSource source = new NewsSource();
source.setUrl(new URL(SOME_URL));
newsSourcesRepository.save(source);
newsSourcesRepository.flush();
User user = new User();
user.setEmail(EMAIL);
List<NewsSource> sources = new ArrayList<>();
sources.add(source);
user.setNewsSources(sources);
usersRepository.save(user);
usersRepository.flush();
User savedUser = usersRepository.findOne(EMAIL);
NewsSource newsSource = savedUser.getNewsSources().get(0);
assertThat("News source should be saved", newsSource.getUrl(), is(SOME_URL));
NewsSource savedSource = newsSourcesRepository.findOne(newsSource.getUrl());
assertThat("New user should be saved in M2M relation", savedSource.getUsers(), Matchers.contains(user));
}
如果我詮釋我的測試作爲@Transaction除非拋出異常,但我不確定這是否是解決此問題的正確方法。
做您嘗試使用查詢連接抓取? – Pulkit
我不寫任何查詢spring數據做這個工作。 –
那好,但你可以在倉庫界面中使用@Query註釋編寫你自己的查詢。 – Pulkit