简体   繁体   English

带有提示和命名查询的JPA实体图

[英]Jpa entity graph with hints and named query

I'm working to explore the new features of JPA 2.1 with spring ex. 我正在尝试通过spring ex探索JPA 2.1的新功能。 EntityGraph feature by making a sample CRUD operations using a sample relations between products, purchase order and order items. EntityGraph功能通过使用产品,采购订单和订单项目之间的示例关系进行CRUD操作示例。

below are the code I made for the main bean, I defined a named query to retrieve all data, and entity graph 以下是我为主bean编写的代码,定义了一个命名查询以检索所有数据以及实体图

@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 我正在尝试从DAO方法中调用命名查询和图形,如下所示

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. 尽管我尝试在“ javax.persistence.fetchgraph”和“ javax.persistence.loadgraph”之间更改提示,但数据库中包含许多行,但调用DAO方法的结果返回零索引,请注意。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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