无法写内容:无法使用OpenEntityManagerInViewFilter懒惰地初始化角色集合

时间:2016-03-03 12:03:35

标签: java spring hibernate jpa spring-boot

所以我有这两个课程:

示例

@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();
    }

当我打电话给它时,我得到&#34; 无法写内容:懒得初始化角色集合&#34;我试图修改我的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));
}

但是当我拨打电话时,我仍然没有得到&#34; 懒得初始化角色集合&#34;

我应该如何正确添加 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 bean

<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>

2 个答案:

答案 0 :(得分:0)

您的问题看起来非常类似于Lazy Initialisation with OpenEntityManagerInViewFilter?

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

答案 1 :(得分:0)

嗯,最后在尝试这个工作后: 制作Eager初始化booth集合(Sample和DNA_Study并将AnalysisController更改为:

    @CrossOrigin
    @RequestMapping("/getanalysis")
    public JsonArray getAnalysis() {

        context = new ClassPathXmlApplicationContext("applicationContext.xml");
        DNA_StudyDAO dao = context.getBean(DNA_StudyDAO.class);
        ArrayList<DNA_Study> result = (ArrayList<DNA_Study>) dao.findAll();
        JsonArrayBuilder datasourcesBuilder = Json.createArrayBuilder();
        for(DNA_Study study : result) {
            datasourcesBuilder
            .add(Json.createObjectBuilder()
                .add("name", study.getName())
                .add("description", study.getDescription())
                .add("size", String.valueOf(study.getSamples().size())));
        }
        return datasourcesBuilder.build();
}

无法通过延迟初始化找到一种方法