在Hibernate中添加一个延迟加载的子项。

huangapple 未分类评论51阅读模式
标题翻译

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 = &quot;jobRun&quot;, cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	private List&lt;JobStep&gt; jobSteps = new ArrayList&lt;&gt;();

	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

> 有没有一种方法只添加一条子记录而不获取所有子记录。

考虑到您想要添加给定的 JobRunIdJobStep 实体,您可以使用类似查询的 JOIN FETCH 指令。JOIN FETCH 指令指示 Hibernate 进行 INNER JOIN,以便将 JobRunId 与其对应的 JobStep 一起获取。createQuery 代码如下:

    SELECT js
    FROM JobStep js
    JOIN fetch js.jobRunId 
    WHERE js.id = :jobId&quot;

另一种方法是使用 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&quot;

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

huangapple
  • 本文由 发表于 2020年3月17日 02:58:23
  • 转载请务必保留本文链接:https://java.coder-hub.com/60711751.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定