好贷网好贷款

hibernate学习笔记【对象继承采用一个表方式】

发布时间:2016-12-4 5:57:16 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"hibernate学习笔记【对象继承采用一个表方式】",主要涉及到hibernate学习笔记【对象继承采用一个表方式】方面的内容,对于hibernate学习笔记【对象继承采用一个表方式】感兴趣的同学可以参考一下。

例子:商品(dvd,book) Item.java package com.gh.hibernate.test; import java.io.Serializable; public class Item implements Serializable { /** * 测试继承 */ private static final long serialVersionUID = 1586577862062600368L; private Integer id; private String name; private String manufacturer; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getManufacturer() { return manufacturer; } public void setManufacturer(String manufacturer) { this.manufacturer = manufacturer; } } dvd.java package com.gh.hibernate.test; public class dvd extends Item { private String regionCode; public String getRegionCode() { return regionCode; } public void setRegionCode(String regionCode) { this.regionCode = regionCode; } } book.java package com.gh.hibernate.test; public class book extends Item { private String pageCount; public String getPageCount() { return pageCount; } public void setPageCount(String pageCount) { this.pageCount = pageCount; } } Item.hbm.xml <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="com.gh.hibernate.test.Item"> <id name="id" > <generator class="native"></generator> </id> <discriminator column="category" type="string" /> <property name="name"/> <property name="manufacturer"/> <subclass name="com.gh.hibernate.test.book" discriminator-value="1"> <property name="pageCount"></property> </subclass> <subclass name="com.gh.hibernate.test.dvd" discriminator-value="2"> <property name="regionCode"></property> </subclass> </class> </hibernate-mapping> 测试: public void testDemo1(){ Configuration config = new Configuration().configure(); SessionFactory sessionFactory = config.buildSessionFactory(); Session session = sessionFactory.openSession(); List<Item> l1 = session.createQuery("from dvd").list(); for(Item it : l1){ System.out.println(it.getName()); } book bk = new book(); bk.setName("aaa"); dvd dvd = new dvd(); dvd.setName("dvd2"); Transaction ts = session.beginTransaction(); session.save(bk); session.save(dvd); session.flush(); ts.commit(); }

上一篇:VC 程序自杀
下一篇:ZooKeeper安装过程

相关文章

相关评论