我的关系oneToMany
有问题。我在SQLite
数据库中创建了表,这是我的表:
我创建了两个模型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();
}
我不明白我在哪里犯错。我有这个错误:
无法确定以下类型:
com.dar.darkozmetika.models.CategoryModel,在表格上:product_category,
对于列:[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")