您的位置:首页 > 其它

Hibernate关系映射一对一

2017-10-20 15:59 316 查看

一对一:

单向注解:

@OneToOne(cascade = CascadeType.ALL)
//	@PrimaryKeyJoinColumn
@JoinColumn(name="hid")
public Heart getHeart() {
return heart;
}
双向注解:另一边添加

@OneToOne(mappedBy="heart")
public Person getPerson() {
return person;
}

单向XML:

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
unique="true"
not-null="true"/>
</class>

<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
</class>


create table Person ( personId bigint not null primary key, addressId bigint not null unique )
create table Address ( addressId bigint not null primary key )

双向XML:

<class name="Person">
<id name="id" column="personId">
<generator class="native"/>
</id>
<many-to-one name="address"
column="addressId"
unique="true"
not-null="true"/>
</class>

<class name="Address">
<id name="id" column="addressId">
<generator class="native"/>
</id>
<one-to-one name="person"
property-ref="address"/>
</class>


create table Person ( personId bigint not null primary key, addressId bigint not null unique )
create table Address ( addressId bigint not null primary key )
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: