Hibernate_11_继承实例_多表
<1>每个类都建立一张表,抽象类也建立一张表,各张表中只包含自 己的属性,子类继承的属性不用在子类中显示。
父类 Article,子类Topic 、Reply 、Session生成类、持久化层 、主文件配置 (与10中相同)
Article.hmb.xml文件的配置: <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="extends_2"> <!--每个类对应一张表,抽象类也对应一张表, 每个表只含有本类所对应的属性 每个类中都对应一个表名 每个子类都有外键约束 --> <class name="Article" table="article"> <id name="id"> <generator class="native" /> </id> <property name="title" /> <property name="content" type="text" length="10000" /> <property name="postTime" type="timestamp" /> <!-- 子类:Topic --> <joined-subclass name="Topic" table="topic"> <key column="t_id"></key> <property name="type"></property> </joined-subclass> <!-- 子类:Reply --> <joined-subclass name="Reply" table="reply"> <key column="r_id"></key> <property name="floor"></property> </joined-subclass> </class> </hibernate-mapping>
<2>每个实体类都对应一张表,抽象类不建立表,各张表中包含自 己所有的属性。
父类 Article,子类Topic 、Reply 、Session生成类、
主文件配置 (与14中相同)
持久化层的代码:
public class ExtendsDao { /** * save 方法 */ @Test public void testSave() { Session session = SessionFactoryTools.getSession(); Transaction tx = null; try { tx = session.beginTransaction(); // ========================================== // 新建对象并设置属性 Topic topic = new Topic(); topic.setTitle("topic"); Reply reply = new Reply(); reply.setTitle("reply"); // 保存对象 session.save(topic); session.save(reply); // ============================================ tx.commit(); } catch (RuntimeException e) { if (tx != null) { tx.rollback(); } throw e; } finally { session.close(); } } /** * getById方法 */ @Test public void testGetById() { Session session = SessionFactoryTools.getSession(); Transaction tx = null; try { tx = session.beginTransaction(); // ======================================================== // 读取数据并输出 Article topic = (Article) session.get(Topic.class, 0); System.out.println(topic); Article reply = (Article) session.get(Reply.class, 1); System.out.println(reply); // ========================================================== tx.commit(); } catch (RuntimeException e) { if (tx != null) { tx.rollback(); } throw e; } finally { session.close(); } } }
Article.hbm.xml 文件的配置:
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="extends_3"> <!--每个实体类对应一张表,抽象类不对应表,每个表含有本类所对应全部的属性 --> <class name="Article" abstract="true"> <id name="id"> <!-- 当每个实例都有一张表时,主键生成策略不能使用indentity, 因为在整个集成结构中,主键值是不能重复的。 --> <generator class="hilo" > <param name="table">hilo_value</param> <param name="column">next_value</param> <param name="max_lo">0</param> </generator> </id> <property name="title" /> <property name="content" type="text" length="10000" /> <property name="postTime" type="timestamp" /> <!-- 子类:Topic --> <union-subclass name="Topic" table="topic"> <property name="type"></property> </union-subclass> <!-- 子类:Reply --> <union-subclass name="Reply" table="reply"> <property name="floor"></property> </union-subclass> </class> </hibernate-mapping>
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。