hibernate oneToOne xml 外键关联

单向:

单向:

解释:用Users和Customer定义oneToOne单向关联,用一个Users对应一个Customer

[Users]

package pojo;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

public class Users {
private Customer customer; //生成getter和setter方法
private int id;
private String level;

private String title;

public Customer getCustomer() {
return customer;
}
public int getId() {
return id;
}
public String getLevel() {
return level;
}
public String getTitle() {
return title;
}
public void setCustomer(Customer customer) {
this.customer = customer;
}
public void setId(int id) {
this.id = id;
}
public void setLevel(String level) {
this.level = level;
}
public void setTitle(String title) {
this.title = title;
}

}

[Users.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">
<hibernate-mapping>
<class name="pojo.Users" table="USERS">
<id name="id" type="int" >
<generator class="native"></generator>
</id>
<property name="level" type="string">
</property>
<property name="title" type="string">
</property>
<many-to-one name="Customer" column="id" unique="true" insert="false" update="false"></many-to-one> //映射类名
</class>
</hibernate-mapping>

双向:

解释:用Users和Customer定义oneToOne双向关联,一个Users对应一个Customer

[Customer]

package pojo;

public class Customer {
private int id;
private String name;
private String password;
private Users users;

public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}

[Customer.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">
<hibernate-mapping>
<class name="pojo.Customer" table="CUSTOMER">
<id name="id" type="int">
<generator class="native" />
</id>
<property name="name" type="string">
</property>
<property name="password" type="string">
</property>


<one-to-one name="Users" property-ref="Customer"></one-to-one> //这里name值为oneToOne对应方的类名,property-ref="Customer"为关联类的对象的名字


</class>
</hibernate-mapping>

[test]
public class testUsers {

@Test
public void testSchemaExport() {
// SessionFactory sessionFactory = new AnnotationConfiguration()
// .configure().buildSessionFactory();
SchemaExport export = new SchemaExport(
new AnnotationConfiguration().configure());
export.create(true, false);
// Session session = sessionFactory.getCurrentSession();
// session.beginTransaction();
// session.getTransaction().commit();

}

}

posted on 2015-01-21 14:05  ziyi_ang  阅读(284)  评论(0)    收藏  举报

导航