Hibernate 多对一外键单向关联

Hibernate多对一外键单向关联

一、模型介绍

多个人(Person)对应一个地址(Address)。

二、实体(省略getter、setter方法)

publicclassPersonn1fk{

privateintpersonid;

privateStringname;

privateintage;

privateAddressn1fkaddressn1fk;

publicclassAddressn1fk{

privateintaddressid;

privateStringaddressdetail;

三、表模型

mysql>descaddress_n1kf;

+---------------+--------------+------+-----+---------+----------------+

|Field|Type|Null|Key|Default|Extra|

+---------------+--------------+------+-----+---------+----------------+

|addressid|int(11)|NO|PRI|NULL|auto_increment|

|addressdetail|varchar(255)|YES||NULL||

+---------------+--------------+------+-----+---------+----------------+

mysql>descperson_n1kf;

+-----------+--------------+------+-----+---------+----------------+

|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|MUL|NULL||

+-----------+--------------+------+-----+---------+----------------+

四、生成的SQL脚本

CREATETABLE`address_n1kf`(

`addressid`int(11)NOTNULLauto_increment,

`addressdetail`varchar(255)defaultNULL,

PRIMARYKEY(`addressid`)

)ENGINE=InnoDBDEFAULTCHARSET=gbk;

DROPTABLEIFEXISTS`person_n1kf`;

CREATETABLE`person_n1kf`(

`personid`int(11)NOTNULLauto_increment,

`name`varchar(255)defaultNULL,

`age`int(11)defaultNULL,

`addressId`int(11)defaultNULL,

PRIMARYKEY(`personid`),

KEY`FK4571AF54A2A3EE48`(`addressId`),

CONSTRAINT`FK4571AF54A2A3EE48`FOREIGNKEY(`addressId`)REFERENCES`address_n1kf`(`addressid`)

)ENGINE=InnoDBDEFAULTCHARSET=gbk;

五、映射方法

<hibernate-mapping>

<classname="com.lavasoft.dx._n_1_fk.Personn1fk"table="PERSON_n1fk">

<idname="personid">

<generatorclass="identity"/>

</id>

<propertyname="name"/>

<propertyname="age"/>

<!--用来映射关联POcolumn是Address在该表中的外键列名-->

<many-to-onename="addressn1fk"column="addressId"/>

</class>

</hibernate-mapping>

<hibernate-mapping>

<classname="com.lavasoft.dx._n_1_fk.Addressn1fk"table="ADDRESS_n1fk">

<idname="addressid">

<generatorclass="identity"/>

</id>

<propertyname="addressdetail"/>

</class>

</hibernate-mapping>

六、测试方法

publicclassTest_n1fk{

publicstaticvoidmain(String[]args){

Personn1fkp1=newPersonn1fk();

Personn1fkp2=newPersonn1fk();

p1.setAge(21);

p1.setName("p1");

p2.setAge(23);

p2.setName("p2");

Addressn1fkadd=newAddressn1fk();

add.setAddressdetail("郑州市经三路");

p1.setAddressn1fk(add);

p2.setAddressn1fk(add);

Sessionsession=HibernateUtil.getCurrentSession();

Transactiontx=session.beginTransaction();

session.save(add);

session.save(p1);

session.save(p2);

tx.commit();

HibernateUtil.closeSession();

}

}

七、测试结果

1):正常保存.推荐这么干!

session.save(p1);

session.save(p2);

Hibernate:insertintoADDRESS_n1kf(addressdetail)values(?)

Hibernate:insertintoPERSON_n1kf(name,age,addressId)values(?,?,?)

Hibernate:insertintoPERSON_n1kf(name,age,addressId)values(?,?,?)

2):正常保存.

session.save(p1);

session.save(p2);

session.save(add);

Hibernate:insertintoPERSON_n1kf(name,age,addressId)values(?,?,?)

Hibernate:insertintoPERSON_n1kf(name,age,addressId)values(?,?,?)

Hibernate:insertintoADDRESS_n1kf(addressdetail)values(?)

Hibernate:updatePERSON_n1kfsetname=?,age=?,addressId=?wherepersonid=?

Hibernate:updatePERSON_n1kfsetname=?,age=?,addressId=?wherepersonid=?

3):正常保存.

session.save(add);

//session.save(p1);

//session.save(p2);

Hibernate:insertintoADDRESS_n1kf(addressdetail)values(?)

4):发生异常,不能保存.

//session.save(add);

session.save(p1);

session.save(p2);

Hibernate:insertintoPERSON_n1kf(name,age,addressId)values(?,?,?)

Hibernate:insertintoPERSON_n1kf(name,age,addressId)values(?,?,?)

Exceptioninthread"main"org.hibernate.TransientObjectException:com.lavasoft.dx._n_1_fk.Addressn1kf

本文出自“熔岩”博客,转载请与作者联系!

相关推荐