模型user,address
user中有一个字段为address_id与addrees的addressid进行外键关联
user sql
CREATE TABLE `NewTable` ( `userid` int(11) NOT NULL AUTO_INCREMENT , `account` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , `password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , `address_id` int(11) NULL DEFAULT NULL , PRIMARY KEY (`userid`), FOREIGN KEY (`address_id`) REFERENCES `address` (`addressid`) ON DELETE RESTRICT ON UPDATE RESTRICT, INDEX `address_id` (`address_id`) USING BTREE ) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=12 ROW_FORMAT=COMPACT ;
address sql
CREATE TABLE `NewTable` ( `addressid` int(11) NOT NULL AUTO_INCREMENT , `addressinfo` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL , PRIMARY KEY (`addressid`) ) ENGINE=InnoDB DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci AUTO_INCREMENT=12 ROW_FORMAT=COMPACT ;
user dto
package dto; import java.io.Serializable; public class User{ private int userid; private String account; private String password; private Address address; public Address getAddress() { return address; } public void setAddress(Address address) { this.address = address; } public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
user.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2015-6-18 22:24:51 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="dto.User" table="user" catalog="snlu"> <id name="userid" type="java.lang.Integer"> <column name="userid" /> <generator class="identity" /> </id> <many-to-one name="address" class="dto.Address" fetch="select" update="true"> <column name="address_id" /> </many-to-one> <property name="account" type="string"> <column name="account" /> </property> <property name="password" type="string"> <column name="password" /> </property> </class> </hibernate-mapping>
注:只需要在many-to-one 中增加unique="true" 就变为一对一
address dto
package dto; public class Address { private String addressinfo; private int addressid; public String getAddressinfo() { return addressinfo; } public void setAddressinfo(String addressinfo) { this.addressinfo = addressinfo; } public int getAddressid() { return addressid; } public void setAddressid(int addressid) { this.addressid = addressid; } }
address.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <!-- Generated 2015-6-18 22:24:51 by Hibernate Tools 3.4.0.CR1 --> <hibernate-mapping> <class name="dto.Address" table="address" catalog="snlu"> <id name="addressid" type="java.lang.Integer"> <column name="addressid" /> <generator class="identity" /> </id> <property name="addressinfo" type="string"> <column name="addressinfo" /> </property> </class> </hibernate-mapping>
main
package test; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; import util.HibernateUtil; import dao.DAOFactory; import dao.UserDAO; import dao.UserDAOImpl; import dto.Address; import dto.User; public class UserTest { public static void main(String[] args) { Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); User user = new User(); user.setAccount("jack"); user.setPassword("111111"); Address a = new Address(); a.setAddressinfo("america"); user.setAddress(a); session.save(a); session.save(user); tx.commit(); HibernateUtil.closeSession(); } }
时间: 2024-10-11 13:17:45