hibernate一次插入三个表

时间:2017-09-10 10:28:54

标签: java hibernate jpa

我有一个Customer班级,Orders班级和Payment班级。 客户和订单具有一对多的关系。 订单和付款有一对多的关系。

我认为Orders.java(模型)存在问题 谁能告诉我如何解决这个问题?

这是ERD:

enter image description here

我认为这个类没有映射

这是我的订单java类(模型)

@Entity
public class Orders implements Serializable {
private Integer OrderID;
private Customer customer;
private String OrderDate;

private List<Payment> payments;


/**
 * @return the OrderID
 */
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getOrderID() {
    return OrderID;
}

/**
 * @param OrderID the OrderID to set
 */
public void setOrderID(Integer OrderID) {
    this.OrderID = OrderID;
}

/**
 * @return the customer
 */
@ManyToOne
@JoinColumn(name = "Cust_Id")
public Customer getCustomer() {
    return customer;
}

/**
 * @param customer the customer to set
 */
public void setCustomer(Customer customer) {
    this.customer = customer;
}

/**
 * @return the OrderDate
 */
public String getOrderDate() {
    return OrderDate;
}

/**
 * @param OrderDate the OrderDate to set
 */
public void setOrderDate(String OrderDate) {
    this.OrderDate = OrderDate;
}

/**
 * @return the payments
 */
@OneToMany(targetEntity = Payment.class, mappedBy = "orders", cascade = CascadeType.ALL,fetch = FetchType.EAGER)
public List<Payment> getPayments() {
    return payments;
}

/**
 * @param payments the payments to set
 */
public void setPayments(List<Payment> payments) {
    this.payments = payments;
}


}

ERROR

run:
Hibernate many to many (Annotation)
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Sep 10, 2017 3:25:20 PM org.hibernate.validator.internal.util.Version <clinit>
INFO: HV000001: Hibernate Validator 5.0.0.Final
Initial SessionFactory creation failed.org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags
Exception in thread "main" java.lang.ExceptionInInitializerError
    at util.HibernateUtil.buildSessionFactory(HibernateUtil.java:17)
    at util.HibernateUtil.<clinit>(HibernateUtil.java:8)
    at Main.Demo.main(Demo.java:49)
Caused by: org.hibernate.loader.MultipleBagFetchException: cannot simultaneously fetch multiple bags
    at org.hibernate.loader.BasicLoader.postInstantiate(BasicLoader.java:94)
    at org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:119)
    at org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:71)
    at org.hibernate.loader.entity.EntityLoader.<init>(EntityLoader.java:54)
    at org.hibernate.loader.entity.BatchingEntityLoader.createBatchingEntityLoader(BatchingEntityLoader.java:133)
    at org.hibernate.persister.entity.AbstractEntityPersister.createEntityLoader(AbstractEntityPersister.java:1914)
    at org.hibernate.persister.entity.AbstractEntityPersister.createEntityLoader(AbstractEntityPersister.java:1937)
    at org.hibernate.persister.entity.AbstractEntityPersister.createLoaders(AbstractEntityPersister.java:3205)
    at org.hibernate.persister.entity.AbstractEntityPersister.postInstantiate(AbstractEntityPersister.java:3191)
    at org.hibernate.persister.entity.SingleTableEntityPersister.postInstantiate(SingleTableEntityPersister.java:728)
    at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:348)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
    at util.HibernateUtil.buildSessionFactory(HibernateUtil.java:13)
    ... 2 more
Java Result: 1

1 个答案:

答案 0 :(得分:1)

Here你可以找到答案。

Hibernate不喜欢两个FetchType.EAGER的集合。