gpt4 book ai didi

java - OneToMany 无法确定 SpringBoot/SQLite3 的类型

转载 作者:太空宇宙 更新时间:2023-11-04 09:50:46 26 4
gpt4 key购买 nike

我的关系 oneToMany 有问题。我在 SQLite DB 中创建了表,这是我的表:enter image description here我创建了两个模型 CategoryModel 和 ProductModel。产品型号为:

@Entity
@Table(name = "Product_Category")
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
public class ProductModel {

@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Long product_id;
private Long category_id;

private String name;
private String description;
private int numberOfProduct;
private String image;
private int price;

@JoinColumn(name = "country_id", nullable = false)
private CategoryModel category;

//geter's and seter's
}

我的类别模型:

@Entity
@Table(name = "Category")
@JsonIgnoreProperties({ "hibernateLazyInitializer", "handler" })
public class CategoryModel {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;

private String category_name;
private String category_description;
private String image_path;

@OneToMany( mappedBy = "category")
private Set<ProductModel> category;
//Geter's and Seter's

我的存储库:

public interface CategoryRepository extends JpaRepository<CategoryModel, Long>  {

@Query("SELECT * "
+ "FROM Product_Category d INNER JOIN d.categoryModel e")
List<ProductModel> fetchEmpDeptDataInnerJoin();

}

我不明白我在哪里犯了错误。我有这个错误:

Could not determine type for: com.dar.darkozmetika.models.CategoryModel, at table: product_category, for columns: [org.hibernate.mapping.Column(category)]

最佳答案

1)添加@ManyToOne注释:

@ManyToOne
@JoinColumn(name = "country_id", nullable = false)
private CategoryModel category;

2) 请记住,您使用的是 JPQL,而不是 SQL(除非您发送 native="true"):

   @Query("SELECT p "
+ "FROM ProductModel p INNER JOIN p.category c")

关于java - OneToMany 无法确定 SpringBoot/SQLite3 的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54787553/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com