如何在一对多关系中选择实体?

时间:2015-10-25 10:50:15

标签: java spring jpa jpql

我有两个具有一对多关系的实体(所有者一,书本多)。

@Entity
@Table(name = "owners")
@NamedEntityGraph(name = "Owner.books",
attributeNodes = @NamedAttributeNode("books"))
public class Owner implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "owner_id", nullable = false, unique = true)
    private Long id;

    @Column(name = "owner_name", nullable = false)
    private String name;

    @OneToMany(fetch = FetchType.LAZY,mappedBy = "owner")
    private Set<Book> books= new HashSet<>(0);

}
@Entity
@Table(name = "books")
@NamedEntityGraph(name = "Book.owner",
attributeNodes = @NamedAttributeNode("owner"))
public class Book implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "book_id", unique = true, nullable = false)
    private Long id;

    @Column(name = "book_name", nullable = false, unique = true)
    private String name;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "owner_id")
    private Owner owner;

}

我创建了Book找到Owner列表的方法。这很简单。

public interface BookRepository extends JpaRepository<Book, Long> {

    @Query("select b from Book b where b.owner=:b")
    @EntityGraph(value = "Book.owner", type = EntityGraph.EntityGraphType.LOAD)
    List<Book> findListByOwner(@Param("w") Owner w);
}

但我必须在我的JpaRepository中创建方法,通过'Book'找到'Owner',但我不知道如何进行JPQL查询。我不知道在我的JPQL语句中使用Set。

public interface OwnerRepository extends JpaRepository<Owner, Long> {

  @Query("select w from Owner w join fetch w.books where ???? dont know")
  @EntityGraph(value = "Owner.books", type = EntityGraph.EntityGraphType.LOAD)
  Owner findOneByBook(@Param("b") Book);
}

更新

我不能使用getBook()和getOwner,因为当我阅读书籍列表时,我只有所有者的ID。我可以将“w.books”的别名添加为类似“b”的内容,但我不知道该别名旁边的操作。无论如何我使用新的@query(“选择o来自Book b join b.owner o where b =:b”),但是hibernate抛出异常。

Caused by: org.hibernate.QueryException: could not resolve property: books of: com.company.entities.Task [select o from Book b join b.owner o where b = :b]
    at org.hibernate.QueryException.generateQueryException(QueryException.java:137)
    at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:234)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:158)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:131)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:98)
    at org.hibernate.jpa.internal.QueryImpl.list(QueryImpl.java:568)
    at org.hibernate.jpa.internal.QueryImpl.getSingleResult(QueryImpl.java:495)
    ... 65 more
Caused by: org.hibernate.QueryException: could not resolve property: books of: com.company.entities.Book
    at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:83)
    at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:77)
    at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1978)
    at org.hibernate.hql.internal.ast.tree.FromElementType.getPropertyType(FromElementType.java:367)
    at org.hibernate.hql.internal.ast.tree.FromElement.getPropertyType(FromElement.java:500)
    at org.hibernate.engine.query.spi.EntityGraphQueryHint.getFromElements(EntityGraphQueryHint.java:95)
    at org.hibernate.engine.query.spi.EntityGraphQueryHint.toFromElements(EntityGraphQueryHint.java:68)
    at org.hibernate.hql.internal.ast.HqlSqlWalker.processQuery(HqlSqlWalker.java:685)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:665)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:301)
    at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:249)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:278)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:206)
    ... 70 more

有什么想法吗?我需要帮助

0 个答案:

没有答案