我对关系oneToMany
有疑问。我在SQLite
数据库中创建了表,这是我的表:
我创建了两个模型CategoryModel和ProductModel。
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
}
我的CategoryModel:
@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)]
答案 0 :(得分:0)
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")