gpt4 book ai didi

java - 方法抛出 'org.hibernate.LazyInitializationException' 异常

转载 作者:行者123 更新时间:2023-11-29 08:45:46 27 4
gpt4 key购买 nike

我在使用 createQuery()load()get() 方法时收到 LazyInitializationException。配置基于注释。

这是我得到的异常:

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;

持久化配置.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);
}
}

我真的找不到导致这些异常的任何错误。我做错了什么?

最佳答案

通过id获取对象的正确方法是

   public Setting get(final String key) {
return (Setting) sessionFactory.getCurrentSession().get(Setting.class, key);
}

关于java - 方法抛出 'org.hibernate.LazyInitializationException' 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25514740/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com