本人用的是hibernate5.2.10,所以跟视频学习hibernate中ExportDB类的编写出现一些问题:
public class ExportDB { public static void main(String[] args) { // 默认读取hibernate.cfg.xml文件 Configuration cfg = new Configuration().configure(); // 生成并输出sql到文件(当前目录)和数据库 SchemaExport export = new SchemaExport(cfg); // 创建表结构,第一个true 表示在控制台打印sql语句,第二个true 表示导入sql语句到数据库 export.create(true, true); } }
经过查阅,现将该类改为:
public class ExportDB { public static void main(String[] args) { ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build(); Metadata metadata = new MetadataSources(serviceRegistry).buildMetadata(); SchemaExport schemaExport = new SchemaExport(); schemaExport.create(EnumSet.of(TargetType.DATABASE), metadata); } }
两个实体类,Person和IdCard类,单向关联 Person -->IdCard
Person类
public class Person { private int id; private String name; private IdCard idCard; public IdCard getIdCard() { return idCard; } public void setIdCard(IdCard idCard) { this.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; } }
Person.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="com.cnblogs.hibernate_first"> <class name="Person" table="t_person"> <id name="id" column="t_id"> <!-- 采用foreign生成测录,foreign可以取到关联对象的标识 --> <generator class="foreign"> <!-- property 只关联对象 --> <param name="property">idCard</param> </generator> </id> <property name="name" column="t_name" length="30"/> <!-- constrained="true" 表示当前主键(Pserson的主键)还是一个外键(参照IDCard对端的主键) 也就是会生成外键约束语句 one-to-one指示hibernate如何加载其关联对象,默认根据主键加载 也就是拿到关系字段值,根据对端的主键来加载关联对象 --> <one-to-one name="idCard" constrained="true"></one-to-one> </class> </hibernate-mapping>
IdCard类:
public class IdCard { private int id; private String cardNo; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getCardNo() { return cardNo; } public void setCardNo(String cardNo) { this.cardNo = cardNo; } }
IdCard.hbm.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd" > <hibernate-mapping package="com.cnblogs.hibernate_first"> <class name="IdCard" table="t_idcard"> <id name="id" column="t_id"> <generator class="native"></generator> </id> <property name="cardNo" column="t_cardNo" length="30"/> </class> </hibernate-mapping>
测试类:
public class One2OneTest extends TestCase { public void testSave1() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); IdCard idCard = new IdCard(); idCard.setCardNo("123123123"); Person person = new Person(); person.setName("张三"); // 建立关联 person.setIdCard(idCard); session.save(person); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } finally { HibernateUtils.closeSession(session); } } public void testLoad1() { Session session = null; try { session = HibernateUtils.getSession(); session.beginTransaction(); Person person = session.load(Person.class, 1); System.out.println("Person.name = " + person.getName()); System.out.println("Pserson.cardNo = " + person.getIdCard().getCardNo()); session.getTransaction().commit(); } catch (Exception e) { e.printStackTrace(); session.getTransaction().rollback(); } finally { HibernateUtils.closeSession(session); } } }
testload1()方法得到的结果就是:
Hibernate: alter table t_person add constraint FKt1wqtq1hcqkcmd4vk2cmhgd1c forei
gn key (t_id) references t_idcard (t_id)
Hibernate: select person0_.t_id as t_id1_1_0_, person0_.t_name as t_name2_1_0_ f
rom t_person person0_ where person0_.t_id=?
Person.name = 张三
Hibernate: select idcard0_.t_id as t_id1_0_0_, idcard0_.t_cardNo as t_cardNo2_0_
0_ from t_idcard idcard0_ where idcard0_.t_id=?
Pserson.cardNo = 123123123