2

I'm working to explore the new features of JPA 2.1 with spring ex. EntityGraph feature by making a sample CRUD operations using a sample relations between products, purchase order and order items.

below are the code I made for the main bean, I defined a named query to retrieve all data, and entity graph

@Entity
@Table(name = "purchase_order")
@NamedQueries({
@NamedQuery(name = "Order.findAll", query = "SELECT o FROM Order o")})
@NamedEntityGraph(name = "graph.Order.items", attributeNodes =       @NamedAttributeNode(value = "items", subgraph = "items"), 
subgraphs = @NamedSubgraph(name = "items", attributeNodes = @NamedAttributeNode("product")))

public class Order implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", updatable = false, nullable = false)
private Long id = null;

@Version
@Column(name = "version")
private int version = 0;

@Column
private String orderNumber;

@OneToMany(mappedBy = "order", fetch = FetchType.LAZY)
private Set<OrderItem> items = new HashSet<OrderItem>();
... getter and setter methods

I'm trying to call the named query and the graph from the DAO method I have as the following

public List<Order> getOrderDetails() {
    return (List<Order>) entityManager.createNamedQuery("Order.findAll").setHint("javax.persistence.loadgraph", 
            entityManager.getEntityGraph("graph.Order.items")).getResultList();
}

The result of calling the DAO method returns zero index although the database contains many rows although I tried to change hints between "javax.persistence.fetchgraph" and "javax.persistence.loadgraph" please advice.

gasser
  • 279
  • 1
  • 4
  • 16

1 Answers1

0

I found the issue myself, sorry as I discovered that the block of cod above is fine the graph and subgraph are well defined, but hbm2ddl was dropping my data as its value was create that makes it remove the dummy data from the database on deleting and creating it again.

<property name="hibernate.hbm2ddl.auto" value="create"/>
gasser
  • 279
  • 1
  • 4
  • 16