常见错误:
1、比如Channel.java与Storage.java(在这里只是为了说明一对一,至于这两个类是什么意思,不用管。)是一对一,单向,Channel里面有个属性Storage sto;绝对不能加入这样一个属性:int storageId;如果这样的话,在插入的时候会出错,会出“违反唯一约束条件”的错误。
附:从网上转贴过来的一对一讲解:
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"属性即可。
一、模型介绍
一个人(Person)对应一个地址(Address)。
二、实体(省略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;
三、表模型
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 | |
+-----------+--------------+------+-----+---------+----------------+
四、生成的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;
五、映射方法:
在Person中添加Address属性,映射配置为:
<
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"
/>
<
many-to-one
name
="address11fk"
column
="addressId"
unique
="true"
/>
class
>
hibernate-mapping
>
六、测试方法
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();
}
}
七、测试结果
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