gpt4 book ai didi

java - 无法写入内容: failed to lazily initialize a collection of role using OpenEntityManagerInViewFilter

转载 作者:太空宇宙 更新时间:2023-11-04 12:51:40 25 4
gpt4 key购买 nike

所以我有这两门课:

示例

@Entity
@Table(name="sample")
public class Sample implements Serializable {

@Id
@GeneratedValue
@Column(name="sample_id")
private Long sample_id;
@Column(name="id")
private String id;
@Column(name="description")
private String description;
@ManyToOne
@JoinColumn(name="dna_study_id")
private DNA_Study study;
...Getters and setters ...

DNA_Study

@Entity
@Table(name = "dna_study")
public class DNA_Study implements Serializable {

@Id
@GeneratedValue
@Column(name="dna_study_id")
private Long id;
@Column(name="name")
private String name;
@Column(name="description")
private String description;
@Column(name="date")
private Date date;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinColumn(name = "dna_study_id")
private List<Sample> samples;

我想从我的数据库中获取所有 DNA_Study,并使用此 DAO:

@Repository
public interface DNA_StudyDAO extends CrudRepository<DNA_Study, Long>{ }

还有这个RestController:

@RestController
public class AnalysisController {

ClassPathXmlApplicationContext context;

@CrossOrigin
@RequestMapping("/getanalysis")
public ArrayList<DNA_Study> getAnalysis() {

context = new ClassPathXmlApplicationContext("applicationContext.xml");
DNA_StudyDAO dao = context.getBean(DNA_StudyDAO.class);
return (ArrayList<DNA_Study>) dao.findAll();
}

当我调用它时,我得到“无法写入内容:无法延迟初始化角色集合”我尝试修改我的 DAO,因此方法 findAll() 更改为:

@Override
@Query("select d from DNA_Study d join fetch d.samples")
Iterable<DNA_Study> findAll();

使用此方法不会引发异常,但调用方法会创建无限循环,因为创建 DNA_Study 意味着加载它的样本,并且每个样本加载它的 DNA_Study 等,因此它会中断。所以我假设我需要添加一个 OpenEntityManagerInViewFilter 并撤消对 findAll() 的覆盖,尝试添加到我的 SpringBootServletInitializer 类中:

@Override
public void onStartup(ServletContext servletContext) throws ServletException
{
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(Application.class);
rootContext.setServletContext(servletContext);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");

FilterRegistration.Dynamic filter = servletContext.addFilter("openEntityManagerInViewFilter", OpenEntityManagerInViewFilter.class);
filter.setInitParameter("singleSession", "true");
filter.addMappingForServletNames(null, true, "dispatcher");
servletContext.addListener(new ContextLoaderListener(rootContext));
}

但是当我调用电话时,我仍然收到“无法延迟初始化角色集合

我应该如何正确添加OpenEntityManagerInViewFilter

<小时/>

编辑

扩展SpringBootServletInitializer的类:

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(GemDomusServerApplication.class);
}

@Override
public void onStartup(ServletContext servletContext) throws ServletException
{
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(Application.class);
rootContext.setServletContext(servletContext);
ServletRegistration.Dynamic dispatcher = servletContext.addServlet("dispatcher", new DispatcherServlet(rootContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
FilterRegistration.Dynamic filter = servletContext.addFilter("openEntityManagerInViewFilter", OpenEntityManagerInViewFilter.class);
filter.setInitParameter("singleSession", "true");
filter.setInitParameter("entityManagerFactoryBeanName", "entityManagerFactory");
filter.setInitParameter("flushMode", "auto");
filter.addMappingForServletNames(null, true, "dispatcher");
servletContext.addListener(new ContextLoaderListener(rootContext));
servletContext.addListener(new RequestContextListener());
}

public static void main(String[] args) {

SpringApplication.run(GemDomusServerApplication.class, args);
}

涉及的applicationContext.xml beans

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="jpaData" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.postgresql.Driver</value>
</property>
<property name="url">
<value>**</value>
</property>
<property name="username">
<value>**</value>
</property>
<property name="password">
<value>**</value>
</property>
</bean>

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">

<persistence-unit name="jpaData"/>

</persistence>

最佳答案

您的问题看起来与此非常相似Lazy Initialisation with OpenEntityManagerInViewFilter?

我更喜欢的另一个选择是在 DAO 中调用 Hibernate.initialize(Object) 或使用 @Transactional 注释包装 DAO 的管理器

关于java - 无法写入内容: failed to lazily initialize a collection of role using OpenEntityManagerInViewFilter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35771725/

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