我有一个LazyInitializationException问题,我不知道如何解决它。
for (Long id : employeeIds)
{
List<ProjectEmployee> projectEmployeeList = projectEmployeeService.findProjectEmployeesWithinDates(id,
startDate, endDate);
// if no data, then continue with next employee
if (projectEmployeeList.isEmpty())
{
continue;
}
gridCreated = true;
Employee employee = projectEmployeeList.get(0).getEmployee();
Label titleLabel = new Label(employee.getPerson().getSurname() + " " + employee.getPerson().getName() + " ["
+ employee.getRole().getHumanizedRole() + "]");
titleLabel.setStyleName("header-bold");
ProjectEmployeePanel projectEmployeePanel = new ProjectEmployeePanel(id, startDate, endDate);
gridPanelsLayout.addComponents(titleLabel, projectEmployeePanel);
}
之前的问题是我打电话的时候。getperson=null,但我修复了findProjectEmployeesWithinDates请求获取此人的调用。但当我调用“findProjectEmployeesWithinDates”时,我遇到了一个例外。查找项目员工的代码包括:
public List<ProjectEmployee> findProjectEmployeesWithinDates(Long employeeId, LocalDate startDate, LocalDate endDate) {
List<Long> list = new ArrayList<>();
list.add(employeeId);
List<ProjectEmployee> listProjectEmployees = projectEmployeeRepository.findProjectEmployeesWithinDates(list,
LocaleUtils.getDateFromLocalDate(startDate, LocaleUtils.APPLICATION_DEFAULT_ZONE_ID),
LocaleUtils.getDateFromLocalDate(endDate, LocaleUtils.APPLICATION_DEFAULT_ZONE_ID));
for (ProjectEmployee pe : listProjectEmployees)
{
Hibernate.initialize(pe.getEmployee());
Hibernate.initialize(pe.getEmployee().getPerson());
}
return listProjectEmployees;
}
所以用debbug我看到:
Hibernate.initialize(pe.getEmployee()); ----line 105
Hibernate.initialize(pe.getEmployee().getPerson()); ---line 106
它位于findProjectEmployeesWithinDates中for循环的第一行,但不在第二行,这就是异常发生的地方。
我得到的错误
Caused by: org.hibernate.LazyInitializationException: could not initialize proxy - no Session
org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:165)~[hibernate-core-4.3.6。Final.jar: 4.3.6。最终]在org.hibernate.Hibernate.initialize(Hibernate.java:75)~[hibernate-core-4.3.6。Final.jar: 4.3.6。最终]在com.xitee.ccpt.service.项目员工ervice.find项目员工有日期(项目员工ervice.java:105)~[类/: na]在com.xitee.ccpt.service.项目员工服务$$FastClassBySpringCGLIB$$63bfc6f9.invoke()~[sping-core-4.1.1。RELEASE. jar: na]
项目员工类别:
@Entity
@table(name="雇员", Schema="ccpt_data")@NamedQuery(name="Employee.findAll", query="SELECT e From雇员")公共类雇员实现可序列化{私有静态最终长序列化VersionUID=1L;
@Id
@Column(name = "employee_id")
@SequenceGenerator(name = "EMPLOYEE_ID_GENERATOR", sequenceName = "employee_id_seq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.AUTO, generator = "EMPLOYEE_ID_GENERATOR")
private Long employeeId;
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "person_id")
private Person person;
@Column(name = "monthly_cost")
private String monthlyCost;
@Enumerated(EnumType.STRING)
@Column(name = "role")
private EmployeeRole role;
@Column(name = "employee_manager")
private String employeeManager;
@Column(name = "obsolete")
private Boolean obsolete;
@Column(name = "bank_account_number")
private String bankAccountNumber;
@Column(name = "last_employer")
private String lastEmployer;
@Column(name = "starting_day")
private String startingDay;
@Column(name = "hours")
private Short hours;
@OneToMany(mappedBy = "employee", cascade =
{
CascadeType.ALL
}, orphanRemoval = true)
private Set<EmployeeWorkload> employeeWorkloads;
@OneToMany(mappedBy = "employee", fetch = FetchType.LAZY, orphanRemoval = true)
private Set<ProjectEmployee> projectEmployee;
@OneToMany(mappedBy = "employee", cascade =
{
CascadeType.PERSIST
}, orphanRemoval = true)
private Set<Qualification> qualifications;
@OneToMany(mappedBy = "employee", cascade =
{
CascadeType.PERSIST
}, orphanRemoval = true)
private List<CareerExperience> careerExperiences;
@Transient
private Map<Integer, String> exportOptions;
@OneToMany(mappedBy = "employee", fetch = FetchType.LAZY, orphanRemoval = true)
private Set<ProjectEmployeeRejection> projectEmployeeRejections;
@Transient
private boolean decrypted = true; // allows editing and viewing for users with no encryption rights
/**
* Initialization vector used for encryption of this employee or NO_KEY if no encryption was used
*
* @since 0.4.0
*/
@Column(name = "iv")
private String iv;
@Column(name = "cis_employee_id")
private Long cISEmployeeId;
@Column(name = "experience_summary")
private String experienceSummary;
@Enumerated(EnumType.STRING)
@Column(name = "employee_job_type")
private EmployeeJobType employeeJobType;
@Column(name = "ending_day")
@Type(type = "date")
private Date endingDay;
@Column(name = "main_skill")
private String mainSkill;
public Employee()
{
}
public Long getEmployeeId()
{
return employeeId;
}
public void setEmployeeId(Long employeeId)
{
this.employeeId = employeeId;
}
public Person getPerson()
{
return person;
}
public void setPerson(Person person)
{
this.person = person;
}
public String getExperienceSummary()
{
return experienceSummary;
}
public void setExperienceSummary(String experienceSummary)
{
this.experienceSummary = experienceSummary;
}
有人能帮我解决这个问题吗?
我建议采用以下方法之一:
1) 在您的存储库方法中,您可以
for (ProjectEmployee pe : listProjectEmployees)
{
pe.getEmployee().getPerson();
}
因此,它将在会话打开时初始化对象
2)您可以使用查询获取数据
SELECT * FROM ProjectEmployee pe JOIN FETCH pe.employee e JOIN FETCH e.person
通过这种方式,Hibernates将自动使用员工和人员对象填充执行结果
我使用< code>spring-data-jpa与< code > spring-boot(v 2 . 0 . 0 . release),我刚刚在MySQL上写了一个CRUD演示,但是在运行时出现异常,源代码如下: 源码 User.java UserRepository.java 用户服务测试.java 应用程序.yml 例外详细信息 我尝试另一种方法,它可以成功运行。
我有错误在我的Hibernate代码和我需要知道如何修复它 错误: “errorMessage:“无法初始化代理[tech.basarsoft.hayez.io.entity.University#gx8qcpipfnysrlripzmtgrt1qifuyb8]-无会话”, CollageEntryPOint类 拼贴: 和DAOSQL实现 这是hibernate。cfg文件 拼贴服务: 拼贴映射:
我知道我的问题很奇怪,很难理解 我为我的项目创建了一些结构。但每次我都会遇到不同的问题,如<code>无法初始化代理-再次没有会话</code>或<code>不可序列化异常</code>等 我想得到一些建议或帮助。我尝试使用注释< code>transactional,但不知道在哪种情况下应该使用< code > implements Serializable 。当然,我知道如果我想在视图范围内使
问题内容: 我通过服务将dao称为 在岛上,我得到的产品 运行正常,但如果我将dao类更改为 我得到org.hibernate.LazyInitializationException:无法初始化代理- 没有会话。例外发生在我只是在打印产品的视图层中。我不明白为什么在dao方法中在同一行中返回会导致视图层出现异常,但是如果将其保存在引用中然后返回它,效果很好。 问题答案: 这是一个很好的参考,可让您
我不明白为什么会发生这种情况。根据代码路径,当引发此异常时,我应该在同一线程中并且会话应存在。 有人能告诉我我错过了什么吗? 我有设置 在hibernate.cfg.xml档案里 我在Servlet过滤器中创建了以下代码 在index.xhtml文件中,我有以下调用: 做一些事情 index.xhtml正在使用使用ui include加载menu.xhtml文件的模板。然后,菜单文件会插入menu
问题内容: 我有下一个错误: 我的实体: 我有一个服务班: 我从另一种服务方法调用服务: 但是,当我试图调用这个方法我收到线异常,当我打电话时,也会发生同样的异常上。我已经检查了事务管理器,并且配置正确,并且此步骤中存在事务。另外,我已经检查了关于与此异常相关的stackoverflow的大量答案,但没什么用。 可能是什么原因造成的? 问题答案: 看来模型是一个独立的实体。 尝试合并并在合并实例上