@Queryアノテーションを用いて3つのテーブルを結合した結果をDBから取得したいのですが、うまくいきません。
実行すると以下のようなエラーがでます。
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Invalid path: 'i.category' [SELECT i.id,i.name,i.image,i.price, c.name, COUNT(i.id)FROM jp.co.sss.shop.entity.OrderItem oi INNER JOIN oi.item iINNER JOIN i.category c GROUP BY i.id,i.name,i.image, i.price, c.name ORDER BY count(i.id) DESC ]
ItemRepository
1package jp.co.sss.shop.repository; 2 3import org.springframework.data.domain.Page; 4import org.springframework.data.domain.Pageable; 5import org.springframework.data.jpa.repository.JpaRepository; 6import org.springframework.data.jpa.repository.Query; 7import org.springframework.data.repository.query.Param; 8import org.springframework.stereotype.Repository; 9 10import jp.co.sss.shop.entity.Category; 11import jp.co.sss.shop.entity.Item; 12import java.util.List; 13/** 14 * itemsテーブル用リポジトリ 15 * 16 * @author System Shared 17 */ 18@Repository 19public interface ItemRepository extends JpaRepository<Item, Integer> { 20 21 //一覧表示(売れ筋順) 22 @Query("SELECT i.id,i.name,i.image,i.price, c.name, COUNT(i.id)" + 23 "FROM OrderItem oi INNER JOIN oi.item i" + 24 "INNER JOIN i.category c " + 25 "GROUP BY i.id,i.name,i.image, i.price, c.name " + 26 "ORDER BY count(i.id) DESC ") 27 public List<Item> sortByOrderCount(); 28 29}
Item
1package jp.co.sss.shop.entity; 2 3import java.sql.Date; 4import java.util.List; 5 6import javax.persistence.Column; 7import javax.persistence.Entity; 8import javax.persistence.GeneratedValue; 9import javax.persistence.GenerationType; 10import javax.persistence.Id; 11import javax.persistence.JoinColumn; 12import javax.persistence.ManyToOne; 13import javax.persistence.NamedQuery; 14import javax.persistence.OneToMany; 15import javax.persistence.SequenceGenerator; 16import javax.persistence.Table; 17 18/** 19 * 商品情報のエンティティクラス 20 * 21 * @author SystemShared 22 */ 23@Entity 24@Table(name = "items") 25@NamedQuery(name="OrderById", query="SELECT i FROM Item i INNER JOIN i.category c ORDER BY i.id DESC") 26public class Item { 27 /** 28 * 商品ID 29 */ 30 @Id 31 @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_items_gen") 32 @SequenceGenerator(name = "seq_items_gen", sequenceName = "seq_items", allocationSize = 1) 33 private Integer id; 34 35 /** 36 * 商品名 37 */ 38 @Column 39 private String name; 40 41 /** 42 * 価格 43 */ 44 @Column 45 private Integer price; 46 47 /** 48 * 商品説明 49 */ 50 @Column 51 private String description; 52 53 /** 54 * 在庫数 55 */ 56 @Column 57 private Integer stock; 58 59 /** 60 * 商品画像ファイル名 61 */ 62 @Column 63 private String image; 64 65 /** 66 * 削除フラグ 67 */ 68 @Column(insertable = false) 69 private Integer deleteFlag; 70 71 /** 72 * 登録日付 73 */ 74 @Column(insertable = false, updatable = false) 75 private Date insertDate; 76 77 /** 78 * カテゴリ情報 79 */ 80 @ManyToOne 81 @JoinColumn(name = "category_id", referencedColumnName = "id") 82 private Category category; 83 84 /** 85 * 注文商品情報 86 */ 87 @OneToMany(mappedBy = "item") 88 private List<OrderItem> orderItemList; 89 90 /** 91 * コンストラクタ 92 */ 93 public Item() { 94 } 95 96 /** 97 * コンストラクタ 98 * @param id 商品ID 99 * @param name 商品名 100 * @param description 商品説明 101 * @param image 画像ファイル名 102 * @param category_name カテゴリ名 103 */ 104 public Item(Integer id, String name, String description, String image, String category_name) { 105 this.id = id; 106 this.name = name; 107 this.description = description; 108 this.image = image; 109 this.category = new Category(); 110 this.category.setName(category_name); 111 } 112 113 /** 114 * コンストラクタ 115 * @param id 商品ID 116 * @param name 商品名 117 * @param price 価格 118 * @param description 商品説明 119 * @param image 画像ファイル名 120 * @param category_name カテゴリ名 121 */ 122 public Item(Integer id, String name, Integer price, String description, String image, String category_name) { 123 this.id = id; 124 this.name = name; 125 this.price = price; 126 this.description = description; 127 this.image = image; 128 this.category = new Category(); 129 this.category.setName(category_name); 130 } 131 132 public Integer getId() { 133 return id; 134 } 135 136 public void setId(Integer id) { 137 this.id = id; 138 } 139 140 public String getName() { 141 return name; 142 } 143 144 public void setName(String name) { 145 this.name = name; 146 } 147 148 public Integer getPrice() { 149 return price; 150 } 151 152 public void setPrice(Integer price) { 153 this.price = price; 154 } 155 156 public String getDescription() { 157 return description; 158 } 159 160 public void setDescription(String description) { 161 this.description = description; 162 } 163 164 public Integer getStock() { 165 return stock; 166 } 167 168 public void setStock(Integer stock) { 169 this.stock = stock; 170 } 171 172 public String getImage() { 173 return image; 174 } 175 176 public void setImage(String image) { 177 this.image = image; 178 } 179 180 public Integer getDeleteFlag() { 181 return deleteFlag; 182 } 183 184 public void setDeleteFlag(Integer deleteFlag) { 185 this.deleteFlag = deleteFlag; 186 } 187 188 public Date getInsertDate() { 189 return insertDate; 190 } 191 192 public void setInsertDate(Date insertDate) { 193 this.insertDate = insertDate; 194 } 195 196 public Category getCategory() { 197 return category; 198 } 199 200 public void setCategory(Category category) { 201 this.category = category; 202 } 203 204 public List<OrderItem> getOrderItemsList() { 205 return orderItemList; 206 } 207 208 public void setOrderItemsList(List<OrderItem> orderItemList) { 209 this.orderItemList = orderItemList; 210 } 211 212} 213
OrderItem
1package jp.co.sss.shop.entity; 2 3import javax.persistence.Column; 4import javax.persistence.Entity; 5import javax.persistence.GeneratedValue; 6import javax.persistence.GenerationType; 7import javax.persistence.Id; 8import javax.persistence.JoinColumn; 9import javax.persistence.ManyToOne; 10import javax.persistence.NamedQuery; 11import javax.persistence.SequenceGenerator; 12import javax.persistence.Table; 13 14/** 15 * 注文商品情報のエンティティクラス 16 * 17 * @author SystemShared 18 */ 19@Entity 20@Table(name = "order_items") 21//@NamedQuery(name="findByOrderQuantity", query="SELECT o FROM order_items WHERE o.quantity >= 1 ORDER BY o.quantity DESC") 22public class OrderItem { 23 24 /** 25 * 注文商品ID 26 */ 27 @Id 28 @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_order_items_gen") 29 @SequenceGenerator(name = "seq_order_items_gen", sequenceName = "seq_order_items", allocationSize = 1) 30 private Integer id; 31 32 /** 33 * 注文個数 34 */ 35 @Column 36 private Integer quantity; 37 38 /** 39 * 注文情報 40 */ 41 @ManyToOne 42 @JoinColumn(name = "order_id", referencedColumnName = "id") 43 private Order order; 44 45 /** 46 * 商品情報 47 */ 48 @ManyToOne 49 @JoinColumn(name = "item_id", referencedColumnName = "id") 50 private Item item; 51 52 53 /** 54 * 注文時点商品単価 55 */ 56 @Column 57 private int price; 58 59 60 public Integer getId() { 61 return id; 62 } 63 64 public void setId(Integer id) { 65 this.id = id; 66 } 67 68 public Integer getQuantity() { 69 return quantity; 70 } 71 72 public void setQuantity(Integer quantity) { 73 this.quantity = quantity; 74 } 75 76 public Order getOrder() { 77 return order; 78 } 79 80 public void setOrder(Order order) { 81 this.order = order; 82 } 83 84 public Item getItem() { 85 return item; 86 } 87 88 public void setItem(Item item) { 89 this.item = item; 90 } 91 92 public int getPrice() { 93 return price; 94 } 95 96 public void setPrice(int price) { 97 this.price = price; 98 } 99 100 101} 102
Category
1package jp.co.sss.shop.entity; 2 3import java.sql.Date; 4import java.util.List; 5 6import javax.persistence.Column; 7import javax.persistence.Entity; 8import javax.persistence.GeneratedValue; 9import javax.persistence.GenerationType; 10import javax.persistence.Id; 11import javax.persistence.OneToMany; 12import javax.persistence.SequenceGenerator; 13import javax.persistence.Table; 14 15/** 16 * カテゴリ情報のエンティティクラス 17 * 18 * @author SystemShared 19 */ 20@Entity 21@Table(name = "categories") 22public class Category { 23 @Id 24 @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_categories_gen") 25 @SequenceGenerator(name = "seq_categories_gen", sequenceName = "seq_categories", allocationSize = 1) 26 /** 27 * カテゴリID 28 */ 29 private Integer id; 30 31 /** 32 * カテゴリ名 33 */ 34 @Column 35 private String name; 36 37 /** 38 * カテゴリ説明 39 */ 40 @Column 41 private String description; 42 43 /** 44 * 削除フラグ 0:未削除、1:削除済み 45 */ 46 @Column(insertable = false) 47 private Integer deleteFlag; 48 49 /** 50 * 登録日付 51 */ 52 @Column(insertable = false) 53 private Date insertDate; 54 55 /** 56 * 商品リスト 57 */ 58 @OneToMany(mappedBy = "category") 59 private List<Item> itemList; 60 61 /** 62 * コンストラクタ 63 */ 64 public Category() { 65 } 66 67 /** 68 * コンストラクタ 69 * @param name カテゴリ名 70 */ 71 public Category(String name) { 72 this.name = name; 73 } 74 75 public Integer getId() { 76 return id; 77 } 78 79 public void setId(Integer id) { 80 this.id = id; 81 } 82 83 public String getName() { 84 return name; 85 } 86 87 public void setName(String name) { 88 this.name = name; 89 } 90 91 public String getDescription() { 92 return description; 93 } 94 95 public void setDescription(String description) { 96 this.description = description; 97 } 98 99 public Integer getDeleteFlag() { 100 return deleteFlag; 101 } 102 103 public void setDeleteFlag(Integer deleteFlag) { 104 this.deleteFlag = deleteFlag; 105 } 106 107 public Date getInsertDate() { 108 return insertDate; 109 } 110 111 public void setInsertDate(Date insertDate) { 112 this.insertDate = insertDate; 113 } 114 115 public List<Item> getItemList() { 116 return itemList; 117 } 118 119 public void setItemList(List<Item> itemList) { 120 this.itemList = itemList; 121 } 122} 123