gpt4 book ai didi

java - Hibernate 无法正确从数据库读取对象

转载 作者:行者123 更新时间:2023-11-30 08:04:32 25 4
gpt4 key购买 nike

我在 Hibernate 方面遇到了一些问题。

我有一个或多或少复杂的对象结构,我想使用 Hibernate EntityManager(版本 4.3.5.Final)保存/加载。我设法保存它,但如果我尝试读取该对象,则只会读取 PK。即使 PK 正确,EntityManager 的 find 方法也会返回 null,因此我使用其 getReference 方法。

我仍然无法使用正确的关系(ManyToOne 等),所以我很可能在那里犯了一个错误,我猜这就是导致问题的原因。无论如何。

我的问题是:如何使用 Hibernate 持久保存这样的对象结构?

这是我正在使用的 POJO:

编辑:更新了代码

计算列表:

@Entity(name = "calculation")
public class CalculationList implements EntityList {

@Id
private Date created;

@OneToMany(mappedBy = "", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Product> products;

public CalculationList(Date created) {
this.created = created;
this.products = new LinkedList<>();
}

public CalculationList(Date created, List<Product> products) {
this.created = created;
this.products = products;
}

public CalculationList() {
}

public Date getCreated() {
return created;
}

public void setCreated(Date created) {
this.created = created;
}

public List<Product> getProducts() {
return products;
}

public void setProducts(List<Product> entities) {
this.products = entities;
}

@Override
public String toString() {
return "CalculationList{" +
"created=" + created +
", products=" + products +
'}';
}
}

计算器实体:

@Entity(name = "calculator_entity")
public class CalculatorEntity implements Serializable {

@Id
private int id;

private CalculatorEntityType type;

private String name;

private int number;

@ManyToOne(cascade = CascadeType.ALL)
private Product product;

public CalculatorEntity(CalculatorEntityType type) {
this.type = type;
}

protected CalculatorEntity() {
}

public int getNumber() {
return number;
}

public void setNumber(int number) {
this.number = number;
}

public Product getProduct() {
return product;
}

public void setProduct(Product product) {
this.product = product;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public CalculatorEntityType getType() {
return type;
}

public void setType(CalculatorEntityType type) {
this.type = type;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public enum CalculatorEntityType {
GAS_PUMP, DELIVERY_BILL;
}

@Override
public String toString() {
return "CalculatorEntity{" +
"id=" + id +
", type=" + type +
", name='" + name + '\'' +
", product=" + product +
", number=" + number +
'}';
}
}

产品:

@Entity(name = "product")
public class Product implements Serializable {

@Id
private int id;

private String name;

private ProductType type;

@OneToMany(mappedBy = "product", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<CalculatorEntity> entities;

public Product(String name, ProductType type) {
this.name = name;
this.type = type;
this.entities = new LinkedList<>();
}

/**
* JPA - Konstruktor
*/
public Product() {
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public ProductType getType() {
return type;
}

public void setType(ProductType type) {
this.type = type;
}

public List<CalculatorEntity> getEntities() {
return entities;
}

public void setEntities(List<CalculatorEntity> entities) {
this.entities = entities;
}

@Override
public String toString() {
return "Product{" +
"id=" + id +
", name='" + name + '\'' +
", type=" + type +
", entities=" + entities +
'}';
}

public enum ProductType {

FUEL("Treibstoff"), OIL("Öl"), OTHER("Verschiedenes");

private String name;

private ProductType(String name) {
this.name = name;
}

public String getName() {
return this.name;
}
}
}

最佳答案

您正在使用带有错误实体的 @OneToMany 映射,而不是映射 Product 类,而是映射 CalculationList 类,请移动以下内容配置:

@OneToMany(mappedBy = "product", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<CalculatorEntity> entities;

Product 类。

关于java - Hibernate 无法正确从数据库读取对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31335567/

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