简体   繁体   中英

Jpa entity graph with hints and named query

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.

我自己发现了这个问题,很抱歉,我发现上面的cod块很好地定义了图形和子图,但是hbm2ddl删除了我的数据,因为它的值已创建,从而使其在删除和删除数据库时删除了虚拟数据。再次创建它。

<property name="hibernate.hbm2ddl.auto" value="create"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM