一、核心配置文件
<?xml version="1.0" encoding="UTF-8"?> <!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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password">root</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.show_sql">true</property> <property name="hibernate.format_sql">true</property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="current_session_context_class">thread</property> <!-- <mapping resource="com/chinasofti/entity/User.hbm.xml"/> <mapping resource="com/chinasofti/entity/Role.hbm.xml"/> --> <mapping class="com.chinasofti.entity.Person"/> <mapping class="com.chinasofti.entity.Card"/> </session-factory> </hibernate-configuration>
二、实体类
person
package com.chinasofti.entity; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; import javax.persistence.Table; @Entity @Table(name="T_Person") public class Person { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer pid; private String pname; @OneToOne(cascade=CascadeType.ALL,mappedBy="person") @JoinColumn(name="pid") private Card card; public Integer getPid() { return pid; } public void setPid(Integer pid) { this.pid = pid; } public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public Card getCard() { return card; } public void setCard(Card card) { this.card = card; } @Override public String toString() { return "Person [pid=" + pid + ", pname=" + pname + ", card=" + card + "]"; } }
Card
package com.chinasofti.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.OneToOne; import javax.persistence.Table; @Entity @Table(name="T_Card") public class Card { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Integer cid; private String cnum; @OneToOne @JoinColumn(name="pid") private Person person; public Integer getCid() { return cid; } public void setCid(Integer cid) { this.cid = cid; } public String getCnum() { return cnum; } public void setCnum(String cnum) { this.cnum = cnum; } public Person getPerson() { return person; } public void setPerson(Person person) { this.person = person; } @Override public String toString() { return "Card [cid=" + cid + ", cnum=" + cnum + ", person=" + person + "]"; } }
三、封装类
package com.chinasofti.entity; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class HibernateUtil { static Configuration CONFIGURATION = null; static SessionFactory SESSION_FACTORY = null; static Session SESSION = null; static{ CONFIGURATION = new Configuration(); CONFIGURATION.configure(); SESSION_FACTORY = CONFIGURATION.buildSessionFactory(); SESSION = SESSION_FACTORY.openSession(); } public static SessionFactory getSessionFactory(){ return SESSION_FACTORY; } public static Session openSession(){ return SESSION; } }
四、测试类
package com.chinasofti.entity; import org.hibernate.Session; import org.hibernate.Transaction; import org.junit.Test; public class TestOneOne { @Test public void test1to1(){ Session session = HibernateUtil.openSession(); Transaction transaction = session.beginTransaction(); Person person = session.get(Person.class, 1); session.delete(person); transaction.commit(); session.close(); } }
补充