一对一外键映射(人and身份证)
ER:
person字段名主键类型描述idpkint编号name?varchar姓名age?int年龄id_card字段名主键类型描述idpkint编号useful_life?date有限期限person_idfkint关联人的idpackage 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 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 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>
?