[英]JPQL Join on one to many database
我知道之前在這里已經提到過幾次,但我真的無法讓它為我工作,
我有兩個實體:配方,成分:
@Entity
@Table(name = "Recipe")
public class Recipe {
@Id
@GeneratedValue
@Column(name = "Recipe_id")
private Long id;
private String name;
private String description;
@Lob
@Basic(fetch = FetchType.LAZY)
@Column(length = 100000)
private byte[] image;
@OneToMany(mappedBy = "recipe", fetch = FetchType.EAGER)
@Cascade({CascadeType.ALL})
private List<Ingredient> ingredientsList;
....
}
和成分:
@Entity
@Table
public class Ingredient {
@Id
@GeneratedValue
private Long id;
private String name;
private int cpt;
private String cptyType;
@ManyToOne
@JoinColumn(name = "Recipe_id")
private Recipe recipe;
....
}
我還設置了 JPA Reposiotries,我想創建自定義查詢,它相當於:
SELECT *
FROM `Recipe`
INNER JOIN `Ingredient` ON Recipe.Recipe_id = Ingredient.Recipe_id
WHERE Ingredient.name = "fancyName"
LIMIT 0 , 30
到目前為止,我已經嘗試過這個:
@Query("Select r from Recipe r join r.id i where i.name = :ingredient")
List<Recipe> findRecipeByIngredient(@Param("ingredient") String ingredient);
以 expcetion 結束:
Caused by: java.lang.NullPointerException
at org.hibernate.hql.internal.ast.HqlSqlWalker.createFromJoinElement(HqlSqlWalker.java:395)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.joinElement(HqlSqlBaseWalker.java:3477)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3263)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:3141)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:694)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:550)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:287)
at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:235)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:101)
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:119)
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:214)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:192)
我試過這樣的事情:
@Query("Select r from Recipe r join fetch r.ingredientsList where r.name = :ingredient")
List<Recipe> findRecipeByIngredient(@Param("ingredient") String ingredient);
這不會導致任何錯誤,而是返回空結果。
這是一個微不足道的問題,但我之前沒有任何使用 jpql 的經驗 = /
編輯:
仍然得到空結果:
DEBUG (SqlStatementLogger.java:104) - select recipe0_.Recipe_id as Recipe1_1_0_, ingredient1_.id as id0_1_, recipe0_.description as descript2_1_0_, recipe0_.image as image1_0_, recipe0_.name as name1_0_, ingredient1_.cpt as cpt0_1_, ingredient1_.cptyType as cptyType0_1_, ingredient1_.name as name0_1_, ingredient1_.Recipe_id as Recipe5_0_1_, ingredient1_.Recipe_id as Recipe5_1_0__, ingredient1_.id as id0__ from Recipe recipe0_ inner join Ingredient ingredient1_ on recipe0_.Recipe_id=ingredient1_.Recipe_id where ingredient1_.name=?
DEBUG (CollectionLoadContext.java:224) - No collections were found in result set for role: com.bla.model.Recipe.ingredientsList
編輯2:
從語句中刪除 fetch 后:
DEBUG (SqlStatementLogger.java:104) - select recipe0_.Recipe_id as Recipe1_1_, recipe0_.description as descript2_1_, recipe0_.image as image1_, recipe0_.name as name1_ from Recipe recipe0_ inner join Ingredient ingredient1_ on recipe0_.Recipe_id=ingredient1_.Recipe_id where ingredient1_.name=?
DEBUG (StatefulPersistenceContext.java:899) - Initializing non-lazy collections
您的最后一個查詢搜索名稱為作為參數傳遞的成分名稱的所有配方。 那不是你想要的。 您想要的是所有具有成分名稱是作為參數傳遞的成分名稱的配方:
select r from Recipe r
join r.ingredientList i
where i.name = :ingredient
旁注:為什么我不能使用相同的配料制作兩個食譜? 相當有限。 該關聯應該是ManyToMany。
您的 JPQl 查詢應如下所示:-(由於 JPA 已在連接元數據中烘焙,但對於@OnetoMany
我們可以將其用於 JPQl)
@Query("Select r from Recipe r join Ingredient i on r.id = i.recipe.id where i.name=:ingredient)
List<Recipe> findRecipeByIngredient(@Param("ingredient") String ingredient);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.