[英]Hibernate annotation mapping to joined table with composite key
I have an entity, that i'd like to join OneToOne with a table with a composite key (Omitting getters/setters): 我有一个实体,我想将OneToOne与带有复合键的表(省略getter / setter)结合在一起:
@Entity
@Table(name = "parent")
public class Parent {
@Id
private String parentId;
@Column(name = "data")
private String data;
@OneToOne
private Child child;
}
And: 和:
@Entity
@IdClass(ChildKey.class)
@Table(name = "child")
public class Child{
@Id
private String parentId;
@Id
private String username;
@Column(name = "data")
private String childData;
}
public class ChildKey implements Serializable {
private String parentId;
private String username;
}
Parent does not have a notion of the 'username' field in the Child entity. 父级在子级实体中没有“用户名”字段的概念。 I need to pass this in as criteria. 我需要将此作为标准。 In the DB, the primary key of child is on parentId and username. 在数据库中,child的主键位于parentId和username上。
If I don't specify a JoinColumn, hibernate attempts to map using fields child_username and child_parentId. 如果我未指定JoinColumn,则休眠状态将尝试使用child_username和child_parentId字段进行映射。 If I specify only one Joincolumn, I get a broken mapping. 如果仅指定一个Joincolumn,则会得到一个损坏的映射。 If I specify both JoinColumns, I have no column on parent to specify. 如果我同时指定了两个JoinColumns,则在父级上没有要指定的列。
How can I map this class and pass in the username as criteria? 如何映射该类并将用户名作为条件传递? (it is coming from authentication data) Or how can I do this in a different way if I'm off track. (它来自身份验证数据),或者如果我偏离轨道,该如何以其他方式执行此操作。
You might be able to use a Derived Identity. 您也许可以使用派生身份。
The Parent
class would remain the same; Parent
类将保持不变; but you would specify a @OneToOne
mapping back to the child's parent and the Child
and ChildKey
classes would look like this: 但您需要指定一个@OneToOne
映射回孩子的父对象, Child
和ChildKey
类如下所示:
@Entity
@IdClass(ChildKey.class)
@Table(name = "child")
public class Child{
@Id
@OneToOne(mappedBy="child")
private Parent parent;
@Id
private String username;
@Column(name = "data")
private String childData;
}
public class ChildKey implements Serializable {
private String parent; // name matches name of the @Id field and type matches type of Parent @Id field
private String username; // name and type match those of the @Id field
}
Derived identity is discussed in JPA 2.1 spec, section 2.4.1. 派生身份将在JPA 2.1规范的第2.4.1节中讨论。
What I ended up doing was defining a @Filter on the Child class, like so: 我最终要做的是在Child类上定义一个@Filter,如下所示:
@Entity
@IdClass(ChildKey.class)
@Table(name = "child")
@FilterDef(name = "usernameFilter", parameters = {
@ParamDef( name = "username", type="string")
})
public class Child { ... }
On the Parent class, I annotated the collection with a reference to the filter: 在Parent类上,我通过引用过滤器对集合进行了注释:
@OneToMany(fetch = FetchType.EAGER)
@JoinColumn(name = "parentId")
@Filter(name="usernameFilter", condition = "username = :username")
private List<Child> children;
Finally, in my DAO, I parameterized the filter by name like so: 最后,在我的DAO中,我按名称对过滤器进行了参数设置,如下所示:
Filter filter = currentSession().enableFilter("usernameFilter");
filter.setParameter("username", user.getUsername());
Doing this resulted in the exact SQL I had in mind, which is an additional clause in the JOIN criteria with a variable: 这样做产生了我想到的确切的SQL,这是JOIN标准中的一个带有变量的附加子句:
SELECT
...
FROM
parent this_
LEFT OUTER JOIN
child child_ ON this_.parentId = child_.parentId
AND child_.username = ?
I might not have been clear about what end result I was looking for in my original question. 对于最初的问题,我可能一直不清楚最终的结果。 Posting this answer in case it helps someone else. 发布此答案,以防其他人受益。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.