数据表字段:
表Account:id,userName, password
表User:id(根据account的生成),name,age
关系:Account <-------> User
Account.java
public class Account implements java.io.Serializable {
private Integer id;
private String userName;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
User.java
public class User implements java.io.Serializable {
private Integer id;
private Account account;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Account getAccount() {
return account;
}
public void setAccount(Account account) {
this.account = account;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
Account.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="Account" table="account" catalog="testdb">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="userName" type="java.lang.String">
<column name="userName" length="20" />
</property>
<property name="password" type="java.lang.String">
<column name="password" length="20" />
</property>
</class>
</hibernate-mapping>
User.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="User" table="user" catalog="testdb">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="foreign">
<param name="property">account</param>
</generator>
</id>
<!-- 这里配置id是根据Account的id来生成 -->
<property name="name" type="java.lang.String">
<column name="name" length="20" />
</property>
<property name="age" type="java.lang.Integer">
<column name="age" />
</property>
<one-to-one name="account" class="Account"></one-to-one>
<!-- 这里配置one-to-one的关系 -->
</class>
</hibernate-mapping>
TestMapping.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class TestMapping {
private SessionFactory factory;
public TestMapping() {
Configuration config = new Configuration().configure();
factory = config.buildSessionFactory();
}
public void testOne2OneAdd() {
// add test data
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
/*
* Account account = new Account(); account.setUserName("userName");
* account.setPassword("password");
*/
// **account对象既可以这样new,相当于要添加的,如果想留改,比如修改id是2的Account则如下:
Account account = (Account) session.get(Account.class, 5);
account.setUserName("admin");
account.setPassword("pass");
// 这样修改之后如果只调用session.save(account),则只相当于修改account,
// 如果调用session.save(user),则相当于同时又新增了一个user id : 5
User user = new User();
user.setAccount(account);
user.setName("liming");
user.setAge(23);
// session.save(account);
// 执执行session.save(account)只能把account添加进去
session.save(user);
// 执行session.save(user)可以把account和user都添加进去
tx.commit();
session.close();
}
public void testOne2OneFind() {
Session session = factory.openSession();
User user = (User) session.get(User.class, 5);
// 这样取,会连同将和它id一样的Account取出,这便是映射的好处
System.out.println("User Id: " + user.getId() + " User Name: "
+ user.getName());
System.out.println("Account Id: " + user.getAccount().getId()
+ " Account Password: " + user.getAccount().getPassword());
session.close();
}
public static void main(String[] args) {
TestMapping tm = new TestMapping();
// tm.testOne2OneAdd();
tm.testOne2OneFind();
}
}