1
我有一些問題根據Hibernate
。java -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;
}
}
}
好吧,謝謝我改變了它。我稍微改了一下代碼,所以現在CalculationList有一個產品列表,產品有一個CalculatorEntities列表。當我讀對象時,我得到:'CalculationList {created = 2015-05-08 10:44:05.0,products = [Product {id = 0,name ='Benzin',type = FUEL,entities = []}] }'。實體仍然有問題。任何想法爲什麼? – EXSolo
你想在'CalculationList'中有產品列表嗎? –
好吧,現在明白了:我忘記初始化CalculatorEntity中的產品 – EXSolo