英文:
JPA LAZY loading test case
问题
我有一个合同实体,它与生成器状态实体具有一对一的映射。
我保存了一个合同,它可以正常工作并保存合同和生成器状态。
但是当我在Postman中获取合同时,我有生成器状态的详细信息,尽管我尝试将其设置为LAZY。
请问有人能否解释一下如何获取合同实体,包括/不包括生成器状态的详细信息。
主要代码段:
@Override
public void run(String... args) throws Exception {
Contract c1 = new Contract(60, "TSTE");
// GeneratorState gs = new GeneratorState(60,100);
// c.setGeneratorState(gs);
//
// contractRepository.save(c);
// Contract c2 = contractRepository.findById(60).get();
// System.out.println("coucou");
}
合同实体:
@Entity
@Data
@NoArgsConstructor
@Table(name = "contracts")
public class Contract {
@Id
int id;
String symbol;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "id")
private GeneratorState generatorState;
public Contract(int id, String symbol) {
this.id = id;
this.symbol = symbol;
}
@JsonIgnore
@CreationTimestamp
@Column(nullable = false, updatable = false, columnDefinition = "TIMESTAMP WITH TIME ZONE")
private ZonedDateTime createdOn;
@JsonIgnore
@UpdateTimestamp
@Column(nullable = false, columnDefinition = "TIMESTAMP WITH TIME ZONE")
private ZonedDateTime updatedOn;
}
生成器状态:
@Entity
@Table(name = "state_generator")
@NoArgsConstructor
public class GeneratorState {
@Id
int generatorId;
public int getGeneratorId() {
return generatorId;
}
public double getLastPrice() {
return lastPrice;
}
double lastPrice;
@JsonIgnore
@OneToOne(mappedBy = "generatorState")
Contract contract;
public GeneratorState(int generatorId, double lastPrice) {
this.generatorId = generatorId;
this.lastPrice = lastPrice;
}
}
根据你的描述,你想要实现懒加载(LAZY)的效果,但目前似乎无法如预期地工作。需要检查你的持久层配置和关系映射是否正确。如果需要更多帮助,请提供更多代码和配置信息,以便我可以更详细地分析问题。
英文:
I have a contract entity which has oneToOne mapping with generatorState entity
I save one contract, it works and save the contract and the generatorState
However when I get the contract in Postman I have the details of the generatorState while I try to set it up as LAZY
Can someone please shed some lights on how to get the contract entity with/without the generatorState detailled
Main
@Override
public void run(String... args) throws Exception {
Contract c1 = new Contract (60,"TSTE");
// GeneratorState gs = new GeneratorState(60,100);
// c.setGeneratorState(gs);
//
// contractRepository.save(c);
// Contract c2 = contractRepository.findById(60).get();
// System.out.println("coucou");
}
Contract entity
@Entity
@Data
@NoArgsConstructor
@Table(name="contracts")
public class Contract {
@Id
int id;
String symbol;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name ="id")
private GeneratorState generatorState;
public Contract(int id, String symbol) {
this.id = id;
this.symbol = symbol;
}
@JsonIgnore
@CreationTimestamp
@Column(nullable = false, updatable = false, columnDefinition = "TIMESTAMP WITH TIME ZONE")
private ZonedDateTime createdOn;
@JsonIgnore
@UpdateTimestamp
@Column(nullable = false, columnDefinition = "TIMESTAMP WITH TIME ZONE")
private ZonedDateTime updatedOn;
}
GeneratorState
@Entity
@Table(name="state_generator")
@NoArgsConstructor
public class GeneratorState {
@Id
int generatorId;
public int getGeneratorId() {
return generatorId;
}
public double getLastPrice() {
return lastPrice;
}
double lastPrice;
@JsonIgnore
@OneToOne(mappedBy = "generatorState")
Contract contract;
public GeneratorState(int generatorId, double lastPrice) {
this.generatorId = generatorId;
this.lastPrice = lastPrice;
}
}
答案1
得分: 2
从文档中,我们了解到FetchType.LAZY表示数据可以被懒加载。
它并不强制要求一定会懒加载,实体以整体交付的决策由框架来决定,通常这些决策都是合理的。
特别是在你的情况下,对于一对一的关系,框架通常会在大多数情况下交付已展开的实体。
强制它懒加载的最简单方法是将其设为一对多(虽然不建议这样做)。
欲了解更多信息,请参阅这篇博客。
有一些方法可以强制它使用懒加载。
此外,这可能与这个问题重复。
英文:
from the documentation, we find out that FetchType.LAZY means, that data CAN be lazily fetched.
It does not enforce that it will be lazily fetched, the decision when an entity is delivered as a whole is taken by the framework, and these decision usually make good sense.
Especially in your case, with a 1:1 relationship, the framework will deliver the entity expanded in most cases.
Easiest way to force it to be lazy is to make it 1:n. (not recommending that tho)
For more information look at that blog
There are some ways to force it to use Lazy loaded.
Also, this is maybe a duplicate of this question
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论