java - Hibernate没有正确地从数据库中读取对象

时间:2015-07-10 08:05:01

标签: java hibernate jpa

根据Hibernate,我遇到了一些问题。

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

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

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

以下是我正在使用的POJO:

编辑:更新了代码

CalculationList:

@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 +
            '}';
}
}

CalulatorEntity:

@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;
    }
}
}

1 个答案:

答案 0 :(得分:1)

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

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

Product班。