hibernate可选连接 - 需要以任何方式返回连接列值 [英] hibernate optional join - need to return join column value either way
问题描述
随后从 hibernate可选连接
系统中的许多现有查询都依赖于EntityOne和EntityTwo之间的映射关系,并且更改这不是一个附加选项,但是现在我需要开始存储和检索 tbl_one.two_id中的值
不存在于连接表中。在这种情况下,这可以正常工作,如下所示:
@Entity
@Table(name =tbl_one )
public class EntityOne
{
....
@ManyToOne
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name = two_id)
私人实体两个;
....
}
..但是当查询时,如果结果的值< two
的值为null,我无法告诉 tbl_one.two_id
的实际值是多少。我希望hibernate能够创建一个虚拟的 EntityTwo
对象,并且只有当关系不存在时才填充它的id字段。
我还试图将 two_id
映射到另一个没有连接的字段:
@Entity
@Table(name =tbl_one)
public class EntityOne
{
....
@ManyToOne
@NotFound( action = NotFoundAction.IGNORE)
@JoinColumn(name =two_id,insertable = false,updatable = false)
private EntityTwo two;
@Column(name =two_id)
private String twoId;
....
}
...但是<$即使存在相应的记录,也不会填充c $ c> two 字段。
基本上,我需要一些插入/更新值的方法for tbl_one.two_id
,如果关系存在,或者只是<$ c的值,则检索 EntityTwo
的联合实体$ c> tbl_one.two_id 如果关系不存在。
我希望能够解决这个问题,而不需要创建两个映射同一张表。
谢谢。
编辑:更多资讯
public class EntityTwo
{
...
@Id
@Column(name =site_id)
私人字符串ID;
...
}
这应该工作:
@ManyToOne
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn (name =two_id,insertable = false,updatable = false)
private EntityTwo two;
@Formula(two_id)
private String twoId;
Hibernate会选择two_id列两次:一次填充两个关系,一次填充twoId 。尽管如此,twoId属性是只读的。
你也可以做相反的事情,但是它的两个属性将变为只读:
@ManyToOne
@NotFound(action = NotFoundAction.IGNORE)
@JoinFormula(two_id)
private实体两个;
@Column(two_id)
private String twoId;
as a follow on from hibernate optional join
Many existing queries in the system rely on there being a mapped relationship between EntityOne and EntityTwo and changing this is not an attactive option, however I now need to start storing and retrieving values in tbl_one.two_id
which do not exist in the joined table. On the way in, this works fine with the following mapping:
@Entity
@Table(name="tbl_one")
public class EntityOne
{
....
@ManyToOne
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name = "two_id")
private EntityTwo two;
....
}
.. However when querying, if a result has a value for two_id
which does not exist in the joined table, the value for the field two
is null and I have no way of telling what the value for tbl_one.two_id
actually is. I was hoping hibernate would create a dummy EntityTwo
object and populate it's id field only if the relationship did not exist.
I've also tried mapping two_id
to another field without a join:
@Entity
@Table(name="tbl_one")
public class EntityOne
{
....
@ManyToOne
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name = "two_id", insertable = false, updatable = false)
private EntityTwo two;
@Column(name = "two_id")
private String twoId;
....
}
... but then the two
field is never populated, even when the corresponding record exists.
Essentially I need some way of inserting/updating a value for tbl_one.two_id
and retrieving a joined entity for EntityTwo
if the relationship exists, or just the value for tbl_one.two_id
if the relationship does not exist.
I'm hoping to be able to solve this without creating two mappings for the same table.
thanks.
EDIT: more info
public class EntityTwo
{
...
@Id
@Column(name = "site_id")
private String id;
...
}
This should work :
@ManyToOne
@NotFound(action = NotFoundAction.IGNORE)
@JoinColumn(name = "two_id", insertable = false, updatable = false)
private EntityTwo two;
@Formula("two_id")
private String twoId;
Hibernate will select the two_id column twice : once to populate the two relationship, and once to populate the twoId. The twoId property is read-only, though.
You could also do the reverse, but then it's the two property which will become read-only:
@ManyToOne
@NotFound(action = NotFoundAction.IGNORE)
@JoinFormula("two_id")
private EntityTwo two;
@Column("two_id")
private String twoId;
这篇关于hibernate可选连接 - 需要以任何方式返回连接列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!