所以我有这两节课:
@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 ...
@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;
@Repository
public interface DNA_StudyDAO extends CrudRepository<DNA_Study, Long>{ }
@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));
}
但是当我打电话时,我仍然得到“未能懒洋洋地初始化一个角色集合”
@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>
您的问题看起来非常类似于OpenEntityManagerInViewFilter的惰性初始化?
我更喜欢的另一个选项是在DAO内调用hibernate.initialize(Object)或用@transactional注释包装DAO的管理器
我的版本是: SpringFramework 4.2.4.发布 Hibernate4.3.11.final 杰克逊2.7.4 杰克逊类型2.7.1
问题内容: 我有一对多关系,这是我的代码 我得到了例外: org.springframework.http.converter.HttpMessageNotWritableException:无法编写内容:无法延迟初始化角色集合:com.example.helios.model.Catalog.items,无法初始化代理- 没有会话;嵌套异常是com.fasterxml.jackson.datab
我有一个应用程序(Spring 4 MVC Hibernate 4 MySQL Maven集成示例使用注释),使用基于注释的配置将Spring与Hibernate集成。 我有这个实体: 和这个: 服务中的这种方法: 在控制器中: 但我得到了这个错误:
我有下一个错误: 但是当我试图调用这个方法时,我在在上调用时也会出现同样的异常。我已经检查了事务管理器,它配置正确,事务在这一步中存在。此外,我已经检查了大量与此异常相关的stackoverflow的答案,但没有任何有用的。 它的原因是什么?
在我的Spring应用程序中,我实现了一个自定义的身份验证提供商,它使用服务来处理我的登录请求。身份验证提供程序如下: https://github.com/klebermo/webapp_horario_livre/blob/master/src/com/horariolivre/security/CustomAuthenticationProvider.java 我的服务级别是: https:
我正在制作一个网站,我试图在其中订购配料。我选择配料并输入其数量,然后单击添加。我重复了几次。然后我进入下一页,在其中我选择了一个供应商。然后我按“订单”。 预计将Nabavka插入数据库并打开一个页面,说明订单成功,但我收到一个异常,告诉我不能懒惰地初始化它。 来自NabavkaController。java: Nabavka.java: 纳米尔尼卡。java: 纳巴夫卡科帕。java: 纳巴夫