我正在尝试将@Query注释与本机查询一起使用,以从数据库中获取信息,该信息将在我的Web shop应用程序的页面之一上使用。我尝试搜索类似的问题,但是没有运气,所以这里... 这是有问题的数据模型的一部分:
产品类别:
@Entity
@Table(name = "products")
public class Products {
public enum EProductType {
FW_FISH, SW_FISH, P_FISH, CRAB_FISH, FW_PLANT, P_PLANT, TANK, FILTER, CO2, FOOD
}
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "title")
private String title;
@Column(name = "description")
private String description;
@Column(name = "the_type")
@Enumerated(EnumType.STRING)
private EProductType productType;
@OneToMany(cascade = { CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST,
CascadeType.REFRESH }, mappedBy = "product")
private List<Stock> stock;
@ManyToMany(fetch = FetchType.LAZY, cascade = { CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST,
CascadeType.REFRESH })
@JoinTable(name = "products_order", joinColumns = @JoinColumn(name = "id"), inverseJoinColumns = @JoinColumn(name = "product_id"))
private List<Orders> orders;
股票类别:
@Entity
@Table(name = "stock")
public class Stock {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "price")
private double price;
@Column(name = "quantity")
private int quantity;
@Column(name = "price_date")
private Date priceDate;
@ManyToOne(cascade = { CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH })
@JoinColumn(name = "product_id")
private Products product;
Order类:
@Entity
@Table(name = "orders")
public class Orders {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column(name = "date_time")
private LocalDateTime date;
@Column(name = "total_price")
private double totalPrice;
@ManyToOne(cascade = { CascadeType.DETACH, CascadeType.MERGE, CascadeType.PERSIST, CascadeType.REFRESH })
@JoinColumn(name = "user_id")
private Users user;
@ManyToMany
@JoinTable(name = "products_order", joinColumns = @JoinColumn(name = "id"), inverseJoinColumns = @JoinColumn(name = "order_id"))
private List<Products> products;
这是存储库(如评论中所要求):
库存库:
public interface IStockRepository extends CrudRepository<Stock, Integer> {}
产品存储库:
public interface IProductsRepository extends CrudRepository<Products, Integer> {
@Query(value = "SELECT title, the_type, price, quantity FROM Products RIGHT OUTER JOIN Stock ON products.id=stock.product_id", nativeQuery = true)
List<Products> getProductsInfoForTheHomePage();
}
我在html页上需要的信息是:products表中的标题以及stock表中的价格和数量。 我正在我的存储库界面中尝试此查询:
@Query(value = "SELECT title, the_type, price, quantity FROM Products RIGHT OUTER JOIN Stock ON products.id=stock.product_id", nativeQuery = true)
List<Products> getProductsInfoForTheHomePage();
...但是当我尝试在控制器类中调用该方法时,会出现以下异常: java.sql.SQLException:找不到列“ id”。 这就是Hibernate记录器显示的内容:
Hibernate: SELECT title, the_type, price, quantity FROM Products RIGHT OUTER JOIN Stock ON products.id=stock.product_id
由于该列存在于数据模型中,所以我猜问题出在查询中... 打扰一下我的英语笨拙,希望有人能帮助我,因为我被困了一段时间...