0

I have a Customer class, Orders class and Payment class. Customer and Orders have a One-to-Many relationship. Orders and Payment have a One-to-Many relationship.

I think there is a problem in Orders.java (model) can any one tell me how to solve this?

This is the ERD:

enter image description here

I think this class is not mapped

This is my Orders java class(model)

@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
Andrew Nepogoda
  • 1,825
  • 17
  • 24
Suhad Mendis
  • 51
  • 1
  • 11
  • 1
    Try with `Set` instead `List`. You can also try with `@Fetch(value = FetchMode.SUBSELECT)`. – parlad Sep 10 '17 at 10:44

1 Answers1

1

Here you can find the answer.

Hibernate doesn't like two collections with FetchType.EAGER.

Andrew Nepogoda
  • 1,825
  • 17
  • 24