Hibernate 一對一外鍵單向關聯
事實上,單向1-1與N-1的實質是相同的,1-1是N-1的特例,單向1-1與N-1的映射配置也很是類似。只須要將原來的many-to-one元素增長unique="true"屬性,用於表示N的一端也必須是惟一的,在N的一端增長了惟一的約束,即成爲單向1-1。基於外鍵的單向1-1的配置將與無鏈接表N-1關聯的many-to-one增長unique="true"屬性便可。
1、模型介紹
一我的(Person)對應一個地址(Address)。
2、實體(省略getter、setter方法)
public class Person11fk {
private int personid;
private String name;
private int age;
private Address11fk address11fk;
public class Address11fk {
private int addressid;
private String addressdetail;
3、表模型
mysql> desc address_11fk;
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| addressid | int(11) | NO | PRI | NULL | auto_increment |
| addressdetail | varchar(255) | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
mysql> desc person_11fk;
+-----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+--------------+------+-----+---------+----------------+
| personid | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| age | int(11) | YES | | NULL | |
| addressId | int(11) | YES | UNI | NULL | |
+-----------+--------------+------+-----+---------+----------------+
4、生成的SQL腳本
CREATE
TABLE `address_11fk` (
`addressid`
int(11)
NOT
NULL auto_increment,
`addressdetail`
varchar(255)
default
NULL,
PRIMARY
KEY (`addressid`)
) ENGINE=InnoDB AUTO_INCREMENT=2
DEFAULT CHARSET=gbk;
CREATE
TABLE `person_11fk` (
`personid`
int(11)
NOT
NULL auto_increment,
`
name`
varchar(255)
default
NULL,
`age`
int(11)
default
NULL,
`addressId`
int(11)
default
NULL,
PRIMARY
KEY (`personid`),
KEY `FK68A8818F3F45AA77` (`addressId`),
CONSTRAINT `FK68A8818F3F45AA77`
FOREIGN
KEY (`addressId`)
REFERENCES `address_11fk` (`addressid`)
) ENGINE=InnoDB AUTO_INCREMENT=2
DEFAULT CHARSET=gbk;
5、映射方法:
在Person中添加Address屬性,映射配置爲:
<!--
用來映射關聯PO column是Address在該表中的外鍵列名,增長unique變成「1
-1
」
-->
<many-to-one name="address11fk" column="addressId" unique="true"/>
<
hibernate-mapping
>
<
class
name
="com.lavasoft.dx._1_1_fk.Address11fk"
table
="ADDRESS_11fk"
>
<
id
name
="addressid"
>
<
generator
class
="identity"
/>
</
id
>
<
property
name
="addressdetail"
/>
</
class
>
</
hibernate-mapping
>
<
hibernate-mapping
>
<
class
name
="com.lavasoft.dx._1_1_fk.Person11fk"
table
="PERSON_11fk"
>
<
id
name
="personid"
>
<
generator
class
="identity"
/>
</
id
>
<
property
name
="name"
/>
<
property
name
="age"
/>
<!--
用來映射關聯PO column是Address在該表中的外鍵列名,增長unique變成「1-1」-->
<
many-to-one
name
="address11fk"
column
="addressId"
unique
="true"
/>
</
class
>
</
hibernate-mapping
>
6、測試方法
public
class Test_11fk {
public
static
void main(String[] args){
Person11fk p1=
new Person11fk();
p1.setAge(21);
p1.setName(
"p1");
Address11fk add1=
new Address11fk();
add1.setAddressdetail(
"鄭州市經三路");
p1.setAddress11fk(add1);
Session session= HibernateUtil.getCurrentSession();
Transaction tx=session.beginTransaction();
session.save(add1);
session.save(p1);
tx.commit();
HibernateUtil.closeSession();
}
}
7、測試結果
1) :
正常保存. 推薦這麼幹!
session.save(add1);
session.save(p1);
Hibernate: insert into ADDRESS_11fk (addressdetail) values (?)
Hibernate: insert into PERSON_11fk (name, age, addressId) values (?, ?, ?)
2) :
正常保存.
session.save(p1);
session.save(add1);
Hibernate: insert into PERSON_11fk (name, age, addressId) values (?, ?, ?)
Hibernate: insert into ADDRESS_11fk (addressdetail) values (?)
Hibernate: update PERSON_11fk set name=?, age=?, addressId=? where personid=?
3) :
正常保存.
// session.save(p1);
session.save(add1);
Hibernate: insert into ADDRESS_11fk (addressdetail) values (?)
4) :
發生異常,不能保存.
session.save(p1);
// session.save(add1);
Hibernate: insert into PERSON_11fk (name, age, addressId) values (?, ?, ?)
Exception in thread "main" org.hibernate.TransientObjectException: com.lavasoft.dx._1_1_fk.Address11fk