Java Hibernate自连接异常:不存在具有给定标识符的行
我有一张如下的桌子Java Hibernate自连接异常:不存在具有给定标识符的行,java,hibernate,hibernate-mapping,self-join,hibernate-annotations,Java,Hibernate,Hibernate Mapping,Self Join,Hibernate Annotations,我有一张如下的桌子 CREATE TABLE labour\u no\u pk( id bigint(20)非空自动增量, 名称varchar(255)默认为空, 人工id bigint(20)不为空, 承包商id bigint(20)默认为空, 主键(id), 唯一键人工识别号(人工识别号), 钥匙FK_SELF_idx(承包商id), 约束FK_自外键(承包商id)在更新级联时在删除级联时引用人工编号主键(人工id) ); 类别为 @Entity @Table(name = "LABOUR_
CREATE TABLE labour\u no\u pk(
id bigint(20)非空自动增量,
名称varchar(255)默认为空,
人工id bigint(20)不为空,
承包商id bigint(20)默认为空,
主键(id),
唯一键人工识别号(人工识别号),
钥匙FK_SELF_idx(承包商id),
约束FK_自外键(承包商id)在更新级联时在删除级联时引用人工编号主键(人工id)
);代码>
类别为
@Entity
@Table(name = "LABOUR_NO_PK")
public class LabourNoPK {
@Id
@Column(name = "id")
@GeneratedValue
private Long id;
@Column(name = "firstname")
private String firstName;
@ManyToOne(cascade = { CascadeType.ALL })
@JoinColumn(name = "labour_id")
private LabourNoPK contractor;
@OneToMany(mappedBy = "contractor")
private Set<LabourNoPK> subordinates = new HashSet<LabourNoPK>();
}
@实体
@表(name=“人工编号”)
公共类LabourNoPK{
@身份证
@列(name=“id”)
@生成值
私人长id;
@列(name=“firstname”)
私有字符串名;
@ManyToOne(cascade={CascadeType.ALL})
@JoinColumn(name=“labour\u id”)
私人劳工承包商;
@OneToMany(mappedBy=“承包商”)
私有集从属项=新HashSet();
}
道作为
public static List<LabourNoPK> getLabours(Session session) {
List<LabourNoPK> labours = null;
try {
Query query = session.createQuery("FROM LabourNoPK where contractor_id is null");
labours = query.list();
for (LabourNoPK labour : labours) {
System.out.println("parent=" + labour.toString());
if (null != labour.getSubordinates() && !labour.getSubordinates().isEmpty()) {
for (LabourNoPK subordinate : labour.getSubordinates()) {
System.out.println("Sub=" + subordinate.toString());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return labours;
}
公共静态列表getLabours(会话){
列表人工=空;
试一试{
Query Query=session.createQuery(“来自LabourNoPK,其中承包商id为空”);
人工=query.list();
用于(劳工NOPK劳工:劳工){
System.out.println(“parent=“+labour.toString());
if(null!=labour.getsubstances()&&!labour.getsubstances().isEmpty()){
for(LabourNoPK下属:labour.getsubstances()){
System.out.println(“Sub=“+substance.toString());
}
}
}
}捕获(例外e){
e、 printStackTrace();
}
回归劳动;
}
我的数据如下
当我运行程序时,我得到的是org.hibernate.ObjectNotFoundException:不存在具有给定标识符的行:[LabourNoPK#100]
,但数据库中有一条记录可用。
我理解(从异常消息)我的模型类指向id
,而不是contractor\u id
。我应该如何映射以获得所有有孩子的父母的结果
我到底错过了什么?
提前感谢经过大量阅读和尝试,我能够实现我想要的。所以,如果有人需要同样的方式,就发布
@Entity
@Table(name = "LABOUR_NO_PK")
public class LabourNoPK implements Serializable {
@Id
@Column(name = "id")
@GeneratedValue
private Long id;
@Column(name = "labour_id")
@NaturalId
private Long labourId;
@Column(name = "firstname")
private String firstName;
@OneToMany(mappedBy="labour")
private Set<LabourNoPK> subordinates = new HashSet<>();
@ManyToOne
@JoinColumn(name = "contractor_id", referencedColumnName = "labour_id")
/* child (subordinate) contractor_id will be matched with parent (labour) labour_id*/
private LabourNoPK labour;
}
@实体
@表(name=“人工编号”)
公共类LabourNoPK实现了可序列化{
@身份证
@列(name=“id”)
@生成值
私人长id;
@列(name=“labour\u id”)
@归化
私人长期劳动;
@列(name=“firstname”)
私有字符串名;
@OneToMany(mappedBy=“劳工”)
私有集从属项=新HashSet();
@许多酮
@JoinColumn(name=“承包商id”,referencedColumnName=“劳工id”)
/*儿童(下属)承包商id将与父母(劳工)劳工id匹配*/
私人劳工;
}