gpt4 book ai didi

java - Spring-Data 中子对象不与父对象一起获取

转载 作者:行者123 更新时间:2023-12-02 00:11:16 25 4
gpt4 key购买 nike

我对 Hibernate 很陌生,我已经在父对象和子对象之间设置了 @OneToMany 双向关系,并期望当我将所有内容保留在数据库中时,将父对象设置为子对象(因为子对象是关系),当我使用 ORM 获取父对象时,我希望也获取子对象,但作为返回,我看到零个子对象。

据我在互联网上阅读的内容了解,当我将子对象保留为与父对象的关系的所有者时,完整的 ORM 应该根据预定义的关系映射来实现连接两个对象的魔力。此信息正确吗?

@Entity
@Table(name = "stream")
@SequenceGenerator(name = "sequence", sequenceName = "stream_id", allocationSize = 1)
public class StreamEntity extends EntityWithId {

// @MappedSuperclass with ID

@Column(name = "request_id")
@OneToMany(
mappedBy = "streamEntity",
fetch = FetchType.LAZY
)
private List<RequestEntity> requestEntities = new ArrayList<>();

// getters setters
}

@Entity
@Table(name = "request")
@SequenceGenerator(name = "sequence", sequenceName = "request_id", allocationSize = 1, initialValue = 1)
public class RequestEntity extends EntityWithId {

// @MappedSuperclass with ID

@JoinColumn(name = "stream_id")
@ManyToOne(fetch = FetchType.LAZY)
private StreamEntity streamEntity;

// getters setters
}

一些测试:

@Test
public void createStreamWithSeveralStreamTargetsAndDeleteStream() {
List<StreamEntity> streamEntities = createStream(1); // stream object is persisted here

List<RequestEntity> requestEntitySave = createRequest(1, streamEntityCheck);
assertEquals(1, requestEntitySave.size()); // request entity is persisted with the stream object into the database

List<StreamEntity> streamEntities = streamDao.findAll();
assertEquals(1, streamEntities.size());
StreamEntity streamEntity2 = streamEntities.get(0);
streamEntity2.getRequestEntities(); - this collection shows 0
}

我错过了什么?

更新:正如 @Alan Hay 建议的那样,我已经在测试中添加了 @Persistent 上下文,现在它看起来像这样:

@PersistenceContext
private EntityManager em;


@Test
public void createStreamWithSeveralStreamTargetsAndDeleteStream() {
List<StreamEntity> streamEntities = createStream(1); // stream object is persisted here

List<RequestEntity> requestEntitySave = createRequest(1, streamEntityCheck);
assertEquals(1, requestEntitySave.size()); // request entity is persisted with the stream object into the database

em.clear();

List<RequestEntity> requestEntities = requestDao.findAll();
assertEquals(1, requestEntities.size()); // retirns zero objects

List<StreamEntity> streamEntities = streamDao.findAll();
assertEquals(1, streamEntities.size());
StreamEntity streamEntity2 = streamEntities.get(0);
streamEntity2.getRequestEntities();// this collection shows 0
}

完成此操作后,我在 requestDao.findAll(); 上得到零个对象;步。我已在上一步中提交了这些更改,为什么我无法获取此实体?

最佳答案

由于一级缓存的概念,您创建并保存的初始对象与您加载的对象是同一个实例。由于您没有将创建的请求实体添加到内存模型中,而只是将其持久化到数据库中,因此内存模型中不存在关系。

要获得所需的结果,您需要清除保存和重新加载之间的持久性上下文。

@Test
public void createStreamWithSeveralStreamTargetsAndDeleteStream() {
List<StreamEntity> streamEntities = createStream(1);

List<RequestEntity> requestEntitySave = createRequest(1, streamEntityCheck);
assertEquals(1, requestEntitySave.size());

//you need to clear the persistence context here
//now a db query should execute.
//Inject the entitymanager to your test @PersistenceContext EntityManager em;
em.clear();

List<StreamEntity> streamEntities = streamDao.findAll();
assertEquals(1, streamEntities.size());
StreamEntity streamEntity2 = streamEntities.get(0);
streamEntity2.getRequestEntities(); - this collection shows 0

}

关于java - Spring-Data 中子对象不与父对象一起获取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58119601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com