Hibernate:如何将B映射为A的属性? [英] Hibernate: how do I map one to one where B is a property of A?
问题描述
我有一个类A,拥有B类的属性。
A的SQL表对B没有任何了解。
B的SQL表包含A的外键。
如何映射(在hbm.xml中)以便B是属性A?我知道如何用Sets来做到这一点:
< set name =btable =Bcascadeall-删除-孤儿>
< key column =a_id>
< composite-element class =B>
< property name =bPropcolumn =b_proptype =string/>
< / composite-element>
< / set>
事情是,B不是一组A.而只是一个元素。编辑:为了澄清一下,我的用例类似于 HTTP://docs.jboss。 org / hibernate / orm / 3.6 / reference / en-US / html / associations.html#assoc-unidirectional-m21 除外:
create table Person(personId bigint not null primary key,addressId bigint not null)
create table Address(addressId bigint不为null主键)
我有:
create table Person(personId bigint不是null主键)
create table地址(personId bigint,addressId bigint不为null主键)
<然而,我期望实际的地址类不包含对Person的任何引用:
class Person {
地址地址;
}
class地址{
int id;
}
此解决方案,但您可以通过使用几个包装方法解决此问题。为未映射的属性创建一个假的getter / setter对,为您提供所需的接口。因此:
public class Person {
private List< Address>地址;
//属性,实际获取者和设置者
public Address getAddress(){
if(this.addresses == null || this.addresses.isEmpty()){
返回null;
}
返回this.addresses.get(0);
}
$ b $ public void setAddress(Address address){
if(this.addresses == null){
this.addresses = new ArrayList< Address>() ;
}
this.addresses.clear();
this.addresses.add(address);
}
}
I have a class A, with property of class B.
The SQL table for A does not know anything about B.
The SQL table for B contains a foreign key to A.
How can I map (in hbm.xml) so that B is a property of A? I know how to do this with Sets:
<set name="b" table="B" cascade"all-delete-orphan">
<key column="a_id">
<composite-element class="B">
<property name="bProp" column="b_prop" type="string"/>
</composite-element>
</set>
Thing is, B is NOT a set of A. Rather just a single element. How might I go about mapping this?
Edit: To clarify a bit, my use case is similar to http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/associations.html#assoc-unidirectional-m21 except instead of:
create table Person ( personId bigint not null primary key, addressId bigint not null )
create table Address ( addressId bigint not null primary key )
I have:
create table Person ( personId bigint not null primary key )
create table Address ( personId bigint, addressId bigint not null primary key )
However, I would expect then that the actual Address Class does not contain any references to Person:
class Person {
Address address;
}
class Address {
int id;
}
I don't especially like this solution, but you can work around the issue by using a couple of wrapper methods. Create a fake getter/setter pair for an unmapped property that gives you the interface you need. Thus:
public class Person {
private List<Address> addresses;
// properties, real getters and setters
public Address getAddress() {
if (this.addresses == null || this.addresses.isEmpty()) {
return null;
}
return this.addresses.get(0);
}
public void setAddress(Address address) {
if (this.addresses == null) {
this.addresses = new ArrayList<Address>();
}
this.addresses.clear();
this.addresses.add(address);
}
}
这篇关于Hibernate:如何将B映射为A的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!