一对一主键映射(人and身份证)
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 column="useful_life"/> <one-to-one name="person" constrained="true"></one-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></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>
?
package dao;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.cfg.Configuration;public final class HibernateUtil {private static SessionFactory sessionFactory;private HibernateUtil(){}static{Configuration cfg = new Configuration();cfg.configure();//cfg.setProperty(propertyName, value)sessionFactory = cfg.buildSessionFactory();}public static SessionFactory getSessionFactory() {return sessionFactory;}public static Session getSession(){return sessionFactory.openSession();}}
?
package test;import java.util.Date;import org.hibernate.Session;import org.hibernate.Transaction;import dao.HibernateUtil;import domain.IdCard;import domain.Person;public class One2One {public static void main(String[] args) {Session session = null;Transaction tx = null;IdCard idCard = new IdCard();Person person = new Person();try {session = HibernateUtil.getSession();idCard.setUsefulLise(new Date());person.setName("admin");person.setIdCard(idCard);idCard.setPerson(person);tx = session.beginTransaction();//可以传id 然后用user.setId(id);再将user作为变量delete(user)session.save(person);session.save(idCard);tx.commit();} catch (Exception e) {e.printStackTrace();}finally{if(session != null){session.close();}}query(1);}static void query(int id){Session session = null;Transaction tx = null;try {session = HibernateUtil.getSession();tx = session.beginTransaction();//Person p = (Person) session.get(Person.class, id);//System.out.println(p.getIdCard().getUsefulLise());IdCard idCard = (IdCard) session.get(IdCard.class, 1);System.out.println(idCard.getPerson().getName());tx.commit();} catch (Exception e) {e.printStackTrace();}finally{if(session != null){session.close();}}}}
??
表结构
?
person:
字段名主键类型描述idpkint人的编号name?varchar(45)人的姓名id_card:
?
字段名主键类型描述idpkint身份证编号(从人的编号读取)useful_life?Date有限期限