标题翻译
add a lazy child in hibernate
问题
想要在不获取它的情况下添加子对象,但却得到了LazyInitializationException。
我的子对象是一个庞大的列表,因此不想获取所有子对象,只想添加一个子对象。
@Entity
public class JobRunId{
@OneToMany(mappedBy = "jobRun", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<JobStep> jobSteps = new ArrayList<>();
public void addStep(JobStep jobStep) {
if (jobStep != null) {
jobStep.setJobRun(this);
this.jobSteps.add(jobStep);// 这里会出现LazyInitializationException异常
}
}
}
// 服务类
jobRun = repository.findById(id);
// 这里不想使用 hibernate.initialize,因为想要避免获取所有子记录
JobStep jobStep = new JobStep();
// 一些 jobStep 状态
jobRun.addStep(jobStep); // 在这里添加一个步骤到 jobRun,出现错误
有没有一种方法只能添加一个子记录,而不需要获取所有的子记录。
请注意,上述翻译中保留了您提供的代码和问题,并根据您的要求进行了翻译。如果您需要进一步的帮助,请随时提问。
英文翻译
Want to add a child object without fetching it but getting LazyInitializationException.
My child is hug list so dont want to fetch all child just to add one child object.
@Entity
public class JobRunId{
@OneToMany(mappedBy = "jobRun", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private List<JobStep> jobSteps = new ArrayList<>();
public void addStep(JobStep jobStep) {
if (jobStep != null) {
jobStep.setJobRun(this);
this.jobSteps.add(jobStep);// here getting LazyInitializationException
}
}
}
service class
jobRun = repository.findById(id);
//Dont want to do hibernate.initialize here as want to avoid fetch all
child records
JobStep jobStep = new JobStep();
//some jobStep status
jobRun.addStep(jobStep);//adding one step here to jobRun.getting error
is there a way just to add one child record without fetching all the child records.
答案1
得分: 0
> 有没有一种方法只添加一条子记录而不获取所有子记录。
考虑到您想要添加给定的 JobRunId
的 JobStep
实体,您可以使用类似查询的 JOIN FETCH
指令。JOIN FETCH
指令指示 Hibernate 进行 INNER JOIN
,以便将 JobRunId 与其对应的 JobStep 一起获取。createQuery 代码如下:
SELECT js
FROM JobStep js
JOIN fetch js.jobRunId
WHERE js.id = :jobId"
另一种方法是使用 DTO 投影,如果您不想修改实体,可以使用 DTO 投影。基本上,它允许您获取较少的列。
我不确定关系的确切细节以及您为什么以这种方式进行管理。这里有一篇很好的文章可以阅读:处理 LazyInitializationException 的最佳方法。希望这能帮助您解决问题。
英文翻译
> is there a way just to add one child record without fetching all the child records.
Considering you want to add JobStep
entities of given JobRunId
, you can use JOIN FETCH
directive like query. The JOIN FETCH
directive instructs Hibernate to issue an INNER JOIN
so JobRunId fetched along with its JobStep. The createQuery will look something similar:
SELECT js
FROM JobStep js
JOIN fetch js.jobRunId
WHERE js.id = :jobId"
Alternative approach is to use DTO projection, if you do not want to modify an entity then use DTO projection. Basically, it allows you to fetch fewer columns.
I am not sure the exact details of the relation and why you managing this way. Here is good article to read Best way to handle LazyInitializationException. Hope this would give you insight to solve your problem.
答案2
得分: 0
直接持久化您的jobStep(例如使用类似于 em.persist(jobStep)
或者 JobStepRepository.save(jobStep)
的方法)...
注意:jobStep 实例应该已经持久化了对 JobRunId 的引用...
英文翻译
Persist your jobStep directly (i.e. using something like em.persist(jobStep)
or JobStepRepository.save(jobStep)
...
NOTE: the jobStep instance should have a reference to the JobRunId already persisted ...
专注分享java语言的经验与见解,让所有开发者获益!
评论