一 表结构:
CREATE TABLE `address` (
`aid` int(11) NOT NULL auto_increment,
`address` varchar(255) NOT NULL default '',
PRIMARY KEY (`aid`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
CREATE TABLE `user` (
`userid` int(11) NOT NULL auto_increment,
`username` varchar(255) NOT NULL default '',
`age` int(2) default NULL,
`addressID` int(11) default NULL, --外键
PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;
二 POJO与映射文件
public class User {
private Integer userid;
private String username;
private Integer age;
private Address address;
//getters setters method...
}
public class Address {
private Integer aid;
private String address;
private User user;
//getters setters method...
}
User.hbm.xml
<class name="com.pojo.User" table="user" dynamic-insert="true"
dynamic-update="true">
<id name="userid" type="integer">
<column name="userid" sql-type="int" length="11"></column>
<generator class="native"></generator>
</id>
<property name="username" type="string">
<column name="username" sql-type="varchar" length="255"
not-null="true">
</column>
</property>
<property name="age" type="integer">
<column name="age" sql-type="int" length="2"></column>
</property>
<many-to-one name="address" class="com.pojo.Address"
cascade="all" column="addressID" unique="true">
</many-to-one>
</class>
</hibernate-mapping>
Address.hbm.xml
<hibernate-mapping>
<class name="com.pojo.Address" table="address" dynamic-insert="true" dynamic-update="true">
<id name="aid" type="integer">
<column name="aid" sql-type="int" length="11"></column>
<generator class="native">
</generator>
</id>
<property name="address" type="string">
<column name="address" sql-type="varchar" length="255"></column>
</property>
<one-to-one name="user" class="com.pojo.User"
constrained="true" cascade="all" property-ref="address">
</one-to-one>
</class>
</hibernate-mapping>
分享到:
相关推荐
博文链接:https://shaqiang32.iteye.com/blog/201312
博文链接:https://shaqiang32.iteye.com/blog/201311
在本文中,我们将详细介绍Hibernate一对一唯一外键关联映射的概念、配置方法和实践应用。 一对一唯一外键关联映射的概念 在Hibernate中,一对一唯一外键关联映射是指两个实体之间的关联关系,其中一个实体作为外键...
Hibernate 一对一外键单向关联 Hibernate 一对一主键单向关联 Hibernate 一对一连接表单向关联 Hibernate 一对多外键单向关联 Hibernate 一对多连接表单向关联 Hibernate 多对一外键单向关联 Hibernate 多对一连接...
NULL 博文链接:https://cdxs2.iteye.com/blog/1932507
博文链接:https://llying.iteye.com/blog/220804
博文链接:https://llying.iteye.com/blog/220807
NULL 博文链接:https://huihai.iteye.com/blog/1243558
Hibernate 一对多 外键 单向关联,有例子。
NULL 博文链接:https://cdxs2.iteye.com/blog/1930749
NULL 博文链接:https://cdxs2.iteye.com/blog/1930748
hibernate基于主外键的一对多/多对一关联
hibernate外键实现一对一单向关联关系源码
NULL 博文链接:https://12345678.iteye.com/blog/723871
NULL 博文链接:https://ganshisheng.iteye.com/blog/434420
NULL 博文链接:https://huihai.iteye.com/blog/1243530
hibernate外键实现一对一双向关联关系源码