内部连接上的休眠选择错误
Posted
技术标签:
【中文标题】内部连接上的休眠选择错误【英文标题】:Hibernate select error on inner join 【发布时间】:2016-09-10 13:04:01 【问题描述】:我在使用 hibernate/HQL 时遇到了一个错误
我的表是 table1(id, data),其中 id -> 主键和 table2(id, anotherdata),其中 id -> 对 table1.id 的外键引用。
头等舱->
@Entity
@Table(name = "table1")
public class Table1
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "data")
private String data;
@OneToOne
private Table2 table2;
//construct, get, set
二等->
@Entity
@Table(name = "table2")
public class Table2
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "anotherdata")
private String data;
//get, set, construct
HQL 查询是 SELECT t1.id, t1.data, t2.data FROM Table1 as t1 JOIN t1.table2 as t2
休眠日志:select t1.id, t1.data, t2.anotherdata from table1 t1 inner join table2 on t1.t2_id = t2.id
这会导致错误ERROR: column t1.t2__id does not exist
在这个语句的末尾“on t1.t2_id = t2.id”我应该有“on t1.id = t2.id”(纯sql)
我该如何解决这个问题?将感谢您的所有回答!
【问题讨论】:
【参考方案1】:您的查询是正确的,但您应该更改映射:
@Entity
@Table(name = "table1")
public class Table1
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "data")
private String data;
@OneToOne(mappedBy = "table1", cascade = CascadeType.ALL)
private Table2 table2;
//construct, get, set
@Entity
@Table(name = "table2")
public class Table2
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "anotherdata")
private String data;
//get, set, construct
@OneToOne
@PrimaryKeyJoinColumn
private Table1 table1;
【讨论】:
谢谢。它可以工作,但现在我在 List以上是关于内部连接上的休眠选择错误的主要内容,如果未能解决你的问题,请参考以下文章
EF sqlite3报错 "System.Data.Entity.Core.EntityException: 在提供程序连接上启动事务时出错。有关详细信息,请参阅内部异常。