前提・実現したいこと
Spring Bootで、サブクエリや結合を使ったクエリを書いて実行してみたところ、SQLでは正しい異なる値が得られますが、Query DSLでは、同じ値のレコード(件数はOK)しか得られません。Hibernate loaderが変な気がします。どうすれば良いでしょうか?
発生している問題・ログメッセージ
2022-01-05 09:45:15.244 DEBUG 23996 --- [nio-8081-exec-1] org.hibernate.SQL : select distinct axis0_.id as id1_0_, axis0_.label as label2_0_, axis0_.row as row3_0_ from axis_item axis0_ right outer join stat statcell1_ on ( statcell1_.col=axis0_.row ) where axis0_.id=( select graph2_.col_axis_id from graph graph2_ where graph2_.id=? ) order by axis0_.row asc Hibernate: select distinct axis0_.id as id1_0_, axis0_.label as label2_0_, axis0_.row as row3_0_ from axis_item axis0_ right outer join stat statcell1_ on ( statcell1_.col=axis0_.row ) where axis0_.id=( select graph2_.col_axis_id from graph graph2_ where graph2_.id=? ) order by axis0_.row asc 09:45:15.244 TRACE - o.h.type.descriptor.sql.BasicBinder : binding parameter [1] as [INTEGER] - [2] 09:45:15.248 DEBUG - org.hibernate.loader.Loader : Result set row: 0 09:45:15.248 DEBUG - org.hibernate.loader.Loader : Result row: EntityKey[com.sct.entity.Axis#2] 09:45:15.248 DEBUG - org.hibernate.loader.Loader : Result set row: 1 09:45:15.248 DEBUG - org.hibernate.loader.Loader : Result row: EntityKey[com.sct.entity.Axis#2] 09:45:15.248 DEBUG - org.hibernate.loader.Loader : Result set row: 2 09:45:15.248 DEBUG - org.hibernate.loader.Loader : Result row: EntityKey[com.sct.entity.Axis#2] 09:45:15.248 DEBUG - org.hibernate.loader.Loader : Result set row: 3 09:45:15.248 DEBUG - org.hibernate.loader.Loader : Result row: EntityKey[com.sct.entity.Axis#2] 09:45:15.248 DEBUG - org.hibernate.loader.Loader : Result set row: 4 09:45:15.248 DEBUG - org.hibernate.loader.Loader : Result row: EntityKey[com.sct.entity.Axis#2] 09:45:15.248 DEBUG - org.hibernate.loader.Loader : Result set row: 5 09:45:15.248 DEBUG - org.hibernate.loader.Loader : Result row: EntityKey[com.sct.entity.Axis#2] 09:45:15.248 DEBUG - org.hibernate.loader.Loader : Result set row: 6 09:45:15.249 DEBUG - org.hibernate.loader.Loader : Result row: EntityKey[com.sct.entity.Axis#2] 09:45:15.249 DEBUG - org.hibernate.loader.Loader : Result set row: 7 09:45:15.249 DEBUG - org.hibernate.loader.Loader : Result row: EntityKey[com.sct.entity.Axis#2] 09:45:15.249 DEBUG - org.hibernate.loader.Loader : Result set row: 8 09:45:15.249 DEBUG - org.hibernate.loader.Loader : Result row: EntityKey[com.sct.entity.Axis#2] 09:45:15.249 DEBUG - org.hibernate.loader.Loader : Result set row: 9 09:45:15.249 DEBUG - org.hibernate.loader.Loader : Result row: EntityKey[com.sct.entity.Axis#2] 09:45:15.249 DEBUG - o.h.engine.internal.TwoPhaseLoad : Resolving attributes for [com.sct.entity.Axis#2] 09:45:15.249 DEBUG - o.h.engine.internal.TwoPhaseLoad : Processing attribute `label` : value = Hokkaido 09:45:15.249 DEBUG - o.h.engine.internal.TwoPhaseLoad : Attribute (`label`) - enhanced for lazy-loading? - false 09:45:15.249 DEBUG - o.h.engine.internal.TwoPhaseLoad : Processing attribute `row` : value = 1 09:45:15.249 DEBUG - o.h.engine.internal.TwoPhaseLoad : Attribute (`row`) - enhanced for lazy-loading? - fa lse 09:45:15.249 DEBUG - o.h.engine.internal.TwoPhaseLoad : Done materializing entity [com.sct.entity.Axis#2] 09:45:15.250 INFO - com.sct.service.StatService : axis_items=10 09:45:15.250 INFO - com.sct.service.StatService : row=1, label=Hokkaido, hashCode=-1774477528 09:45:15.250 INFO - com.sct.service.StatService : row=1, label=Hokkaido, hashCode=-1774477528 09:45:15.250 INFO - com.sct.service.StatService : row=1, label=Hokkaido, hashCode=-1774477528 09:45:15.250 INFO - com.sct.service.StatService : row=1, label=Hokkaido, hashCode=-1774477528 09:45:15.250 INFO - com.sct.service.StatService : row=1, label=Hokkaido, hashCode=-1774477528 09:45:15.250 INFO - com.sct.service.StatService : row=1, label=Hokkaido, hashCode=-1774477528 09:45:15.250 INFO - com.sct.service.StatService : row=1, label=Hokkaido, hashCode=-1774477528 09:45:15.250 INFO - com.sct.service.StatService : row=1, label=Hokkaido, hashCode=-1774477528 09:45:15.250 INFO - com.sct.service.StatService : row=1, label=Hokkaido, hashCode=-1774477528 09:45:15.250 INFO - com.sct.service.StatService : row=1, label=Hokkaido, hashCode=-1774477528 09:45:15.261 DEBUG - o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, application/x html+xml, image/avif, image/webp, application/xml;q=0.9, */*;q=0.8]
該当のソースコード
sql
1create table stat( 2 id integer 3 , row integer 4 , col integer 5 , data double precision not null 6 , constraint stat_pk1 primary key (id, row, col) 7); 8 9create table axis_item( 10 id integer 11 , row integer 12 , label varchar(30) 13 , constraint axis_item_pk1 primary key (id, row) 14); 15 16create table graph( 17 id integer 18 , stat_id integer not null 19 , col_axis_id integer 20 , row_axis_id integer 21 , o_label varchar(30) 22 , caption varchar(60) not null 23 , shape integer default 0 not null 24 , constraint graph_pk1 primary key (id) 25);
bash
1otnj=# SELECT DISTINCT a.id, a.row, a.label 2 FROM axis_item a 3 RIGHT JOIN stat s ON s.col = a.row 4 WHERE a.id = (SELECT g.col_axis_id FROM graph g WHERE g.id = 2) 5 ORDER BY a.row; 6 id | row | label 7----+-----+------------------- 8 2 | 1 | Hokkaido 9 2 | 2 | Tohoku 10 2 | 3 | Kanto 11 2 | 4 | Hokuriku Shinetsu 12 2 | 5 | Kinki 13 2 | 6 | Chubu 14 2 | 7 | Chugoku 15 2 | 8 | Shikoku 16 2 | 9 | Kyushu 17 2 | 10 | Okinawa 18(10 rows) 19 20otnj=#
Java
1@Repository 2public class QAxisRepository { 3 4 @PersistenceContext 5 private EntityManager em; 6 7 public List<Axis> findColAndLabelById(Integer graphId) { 8 9 JPQLQuery<Axis> query = new JPAQuery<Axis>(em, EclipseLinkTemplates.DEFAULT); 10 QAxis axis = QAxis.axis; 11 QStatCell statCell = QStatCell.statCell; 12 QGraph graph = QGraph.graph; 13 14 return query. 15 from(axis). 16 rightJoin(statCell).on(statCell.id().col.eq(axis.row)). 17 where(axis.id.eq( 18 JPAExpressions.select(graph.colAxisId).from(graph).where(graph.id.eq(graphId)) 19 )). 20 orderBy(axis.row.asc()). 21 distinct(). 22 fetch(); 23 } 24}
Java
1@Entity 2@Table(name="axis_item") 3public class Axis implements Serializable { 4 5 @Id 6 @Column(name="id") 7 private Integer id; 8 9 @Column(name="row") 10 private Integer row; 11 12 @Column(name="label") 13 private String label; 14 15// setters and getters
Java
1@Entity 2@Table(name="stat") 3public class StatCell implements Serializable { 4 5 @EmbeddedId 6 private StatCellId id; 7 8 @Column(name = "data") 9 private Double data; 10 11// setters and getters
Java
1@Embeddable 2public class StatCellId implements Serializable { 3 4 @Column(name = "id") 5 private Integer id; 6 7 @Column(name = "row") 8 private Integer row; 9 10 @Column(name = "col") 11 private Integer col; 12 13// setters and getters
Java
1@Entity 2@Table(name="graph") 3public class Graph implements Serializable { 4 5 @Id 6 @Column(name="id") 7 private Integer id; 8 9 @Column(name="caption") 10 private String caption; 11 12 @Column(name="stat_id") 13 private Integer statId; 14 15 @Transient 16 private List<List<Double>> matrix; 17 18 @Column(name="o_label") 19 private String oLabel; 20 21 @Column(name="col_axis_id") 22 private Integer colAxisId; 23 24 @Transient 25 private List<Axis> colAxis; 26 27// setters and getters
Java
1 @Autowired(required=true) 2 private QAxisRepository axisRepo; 3 4 List<Axis> findColAndLabelById(Integer graphId) { 5 List<Axis> axis = axisRepo.findColAndLabelById(graphId); 6 log.info("axis_items={}", axis.size()); 7 for (Axis item : axis) { 8 log.info("row={}, label={}, hashCode={}", item.getRow(), item.getLabel(), item.hashCode()); 9 } 10 return axis; 11 }
試したこと
JPQLでも何度も試してみましたが、エラーが出て結果が得られなかったため、最終的にQuery DSLを使うことにしました。
Java
1@Repository 2public interface AxisRepository extends JpaRepository<Axis, Integer> { 3/* @Query(value="SELECT DISTINCT a.id, a.row, a.label" 4 + " FROM axis_item a" 5 + " RIGHT JOIN stat s ON s.col = a.row" 6 + " WHERE a.id = (SELECT g.col_axis_id FROM graph g WHERE g.id = ?1)" 7 + " ORDER BY a.row" 8 , nativeQuery=true)*/ 9 @Query(value="SELECT DISTINCT a.id, a.row, a.label" 10 + " FROM Axis a" 11 + " RIGHT JOIN StatCell s ON s.id.col = a.row" 12 + " WHERE a.id = (SELECT g.colAxisId FROM Graph g WHERE g.id = ?1)" 13 + " ORDER BY a.row") 14 List<Axis> findColAndLabelById(Integer graphId); 15}
補足情報(FW/ツールのバージョンなど)
Spring Tool Suite 4 Version: 4.13.0.RELEASE
<querydsl.version>5.0.0</querydsl.version>
PostgreSQL 12.8
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。