ER:
字段名 | 主键 | 类型 | 描述 |
id | pk | int | 编号 |
name | varchar | 姓名 | |
age | int | 年龄 |
字段名 | 主键 | 类型 | 描述 |
id | pk | int | 编号 |
useful_life | date | 有限期限 | |
person_id | fk | int | 关联人的id |
package domain;
import java.util.Date;
public class IdCard {
private int id;
private Date usefulLise;
private Person person;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getUsefulLise() {
return usefulLise;
}
public void setUsefulLise(Date usefulLise) {
this.usefulLise = usefulLise;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
}
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="domain"> <class name="IdCard" table="`id_card`"> <id name="id" column="`id`"> <!-- <generator class="foreign"> <param name="property">person</param> </generator> --> <generator class="native"></generator> </id> <property name="usefulLise" column="useful_life"/> <!--<one-to-one name="person" constrained="true"></one-to-one>--> <many-to-one name="person" column="person_id" unique="true"></many-to-one> </class> </hibernate-mapping>
package domain;
public class Person {
private int id;
private String name;
private IdCard idCard;
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 IdCard getIdCard() {
return idCard;
}
public void setIdCard(IdCard idCard) {
this.idCard = idCard;
}
}
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="domain"> <class name="Person" table="`person`"> <id name="id" column="`id`"> <generator class="native"> </generator> </id> <property name="name" unique="true"/> <!--<one-to-one name="idCard"></one-to-one>--> <one-to-one name="idCard" property-ref="person"></one-to-one> </class> </hibernate-mapping>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql:///user</property> <property name="connection.username">root</property> <property name="connection.password">mysqladmin</property> <property name="show_sql">true</property> <property name="hbm2ddl.auto">create</property> <mapping resource="domain/Person.hbm.xml"/> <mapping resource="domain/IdCard.hbm.xml"/> </session-factory> </hibernate-configuration>