我在使用LazyInitializationException
,createQuery()
或load()
方法时获得了get()
。配置基于注释。
这是我得到的例外情况:
Method threw 'org.hibernate.LazyInitializationException' exception. Cannot evaluate lt.package.to.Setting_$$_jvstfff_0.toString()
架构结构:
CREATE TABLE IF NOT EXISTS `Settings` (
`key` varchar(255) COLLATE utf8_lithuanian_ci NOT NULL,
`value` varchar(255) COLLATE utf8_lithuanian_ci DEFAULT NULL,
PRIMARY KEY (`key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_lithuanian_ci;
PersistenceConfig.java
@Configuration
@ComponentScan(basePackages= {"lt.setkus.sandbox.persistence"})
@EnableTransactionManagement
@PropertySource({"/WEB-INF/properties/configuration.properties"})
public class PersistenceConfig {
@Autowired
private Environment environment;
private Properties getHibernateProperties() {
Properties properties = new Properties();
properties.setProperty("hibernate.hbm2ddl.auto", environment.getProperty("hibernate.hbm2ddl.auto"));
properties.setProperty("hibernate.dialect", environment.getProperty("hibernate.dialect"));
properties.setProperty("hibernate.globally_quoted_identifiers", "true");
return properties;
}
@Bean
public LocalSessionFactoryBean provideSessionFactoryBean() {
LocalSessionFactoryBean localSessionFactoryBean = new LocalSessionFactoryBean();
localSessionFactoryBean.setDataSource(getDataSource());
localSessionFactoryBean.setPackagesToScan("lt.setkus.sandbox.persistence.domain");
localSessionFactoryBean.setHibernateProperties(getHibernateProperties());
return localSessionFactoryBean;
}
@Bean
public DataSource getDataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getProperty("jdbc.driver"));
dataSource.setUrl(environment.getProperty("jdbc.url"));
dataSource.setUsername(environment.getProperty("jdbc.user"));
dataSource.setPassword(environment.getProperty("jdbc.password"));
return dataSource;
}
@Bean
@Autowired
public HibernateTransactionManager provideTransactionManager(SessionFactory sessionFactory) {
HibernateTransactionManager hibernateTransactioManager = new HibernateTransactionManager();
hibernateTransactioManager.setSessionFactory(sessionFactory);
return hibernateTransactioManager;
}
@Bean
public PersistenceExceptionTranslationPostProcessor provideExceptionTranslationPostProcessor() {
return new PersistenceExceptionTranslationPostProcessor();
}
@Bean
public PostItemRepository postItemRepository() {
return new FacebookPostRepository();
}
@Bean
public PostPersistenceService postPersistenceService(PostItemRepository postItemRepository) {
return new PostPersistenceEventHandler(postItemRepository);
}
@Bean
public SettingRepository provideSettingRepository() {
return new SettingDatabaseRepository();
}
@Bean
public SettingPersistenceService provideSettingPersistenceService(SettingRepository settingRepository) {
return new SettingPersistenceEventHandler(settingRepository);
}
}
域模型类
@Entity
@Table(name = "settings")
public class Setting implements Serializable {
@Id
@Column(name = "key", unique = true)
private String key;
@Column(name = "value")
private String value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public SettingDetails toSettingDetails() {
SettingDetails settingDetails = new SettingDetails();
BeanUtils.copyProperties(this, settingDetails);
return settingDetails;
}
public static Setting fromSettingDetails(SettingDetails settingDetails) {
Setting setting = new Setting();
BeanUtils.copyProperties(settingDetails, setting);
return setting;
}
}
服务层
public class SettingDatabaseRepository implements SettingRepository {
@Autowired
private SessionFactory sessionFactory;
@Override
@Transactional
public Setting get(final String key) {
return (Setting)sessionFactory.getCurrentSession().get(Setting.class, key);
}
}
我真的找不到导致这些异常的任何错误。我做错了什么?
答案 0 :(得分:0)
通过id获取对象的正确方法是
public Setting get(final String key) {
return (Setting) sessionFactory.getCurrentSession().get(Setting.class, key);
}