首页 > 解决方案 > 具有不同列主键和外键的 HQL ManyToOne

问题描述

我查看了其他人的其他类似问题,但每个给定的答案似乎都不适合我。

我有这个“机构”实体:

@ManyToOne
@JoinColumn(name = "regionId", referencedColumnName = "id")
private Region region;

'institution' 有regionId并且实体 'region' 有一个id. 与数据库中的名称完全相同。但这给了我一个org.hibernate.QueryException: could not resolve property: region错误。

我使用的查询:Query q = session.createQuery("from Institution where id = :id");数据库具有正确的列设置的适当关系。

编辑:

区域类代码:

@Entity
@Table( name = "region" )
public class Region {
   @Id
   @Column(name="id")
   private Long id;

   @OneToMany(mappedBy="region")
   private List<Institution> institutionList = new ArrayList();

标签: javahibernatehql

解决方案


对于双向 OneToMany 映射使用:

@OneToMany(mappedBy="region")
private List<Institution> institutionList = new ArrayList();

推荐阅读