Hibernate_映射_继承结构映射2_另外两种映射方式
来源:互联网 发布:mysql如何开启事务 编辑:程序博客网 时间:2024/06/06 02:18
package cn.itcast.j_hbm_extends;import java.util.Date;/** * 文章 * * @author 风清杨 * @version V1.0 */public class Article {private Integer id;private String title;private String content;private Date postTime;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getContent() {return content;}public void setContent(String content) {this.content = content;}public Date getPostTime() {return postTime;}public void setPostTime(Date postTime) {this.postTime = postTime;}}package cn.itcast.j_hbm_extends;/** * 主题 * * @author 风清杨 * @version V1.0 */public class Topic extends Article {private Integer type;// 精华、置顶...public Integer getType() {return type;}public void setType(Integer type) {this.type = type;}}package cn.itcast.j_hbm_extends;/** * 回贴 * * @author 风清杨 * @version V1.0 */public class Reply extends Article {private Integer floor;// 楼层public Integer getFloor() {return floor;}public void setFloor(Integer floor) {this.floor = floor;}}package cn.itcast.j_hbm_extends;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.Test;/** * 应用程序操作类 * * @author 风清杨 * @version V1.0 * */public class App {private static SessionFactory sessionFactory = new Configuration()//.configure()//.addClass(Article.class)//.buildSessionFactory();// 保存,有关联关系@Testpublic void testSave() throws Exception {Session session = sessionFactory.openSession();Transaction tx = null;try {tx = session.beginTransaction();// ------------------------------------// 新键对象Article article = new Article();article.setTitle("这是一个Article");Topic topic = new Topic();topic.setTitle("这是一个Topic");Reply reply = new Reply();reply.setTitle("这是一个Reply");// 保存session.save(article);session.save(topic);session.save(reply);// ------------------------------------tx.commit();} catch (RuntimeException e) {tx.rollback();throw e;} finally {session.close();}}// 获取,可以获取到关联的对方@Testpublic void testGet() throws Exception {Session session = sessionFactory.openSession();Transaction tx = null;try {tx = session.beginTransaction();// ------------------------------------// 获取Article article = (Article) session.get(Article.class, 1);Topic topic = (Topic) session.get(Topic.class, 2);Reply reply = (Reply) session.get(Reply.class, 3);System.out.println(article);System.out.println(topic);System.out.println(reply);System.out.println();Article article1 = (Article) session.get(Article.class, 2);Article article2 = (Article) session.get(Article.class, 3);System.out.println(article1);System.out.println(article2);// ------------------------------------tx.commit();} catch (RuntimeException e) {tx.rollback();throw e;} finally {session.close();}}}
方式1
<?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="cn.itcast.j_hbm_extends2"><!-- 采用每个类一张表的方式,抽象类也对应表 --><class name="Article" table="t_article"><id name="id" type="integer" column="id_"><generator class="native" /></id><property name="title" type="string" column="title_" /><property name="content" type="text" length="10000" column="content_"></property><property name="postTime" type="timestamp" column="postTime_"></property><!-- 子类:Topic --><joined-subclass name="Topic" table="t_topic"><key column="articleId"></key><property name="type" type="integer" column="type_"/></joined-subclass><!-- 子类:Reply --><joined-subclass name="Reply" table="t_reply"><key column="articleId"></key><property name="floor" type="integer" column="floor_"/></joined-subclass></class></hibernate-mapping>
方式2
<?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="cn.itcast.j_hbm_extends3"><!-- 采用每个具体类一张表的方式,抽象类不对应表 abstract默认为false,设为true表示本类不对应表(类可以不是abstract的),这时就会忽略table属性。--><class name="Article" abstract="false"><id name="id" type="integer" column="id_"><!-- 当使用每个具体类一张表的方式时,主键生成策略不能是identity。 因为在整个继承结构中,主键值是不能重复的。--><generator class="hilo"><param name="table">hi_value</param> <param name="column">next_value</param> <param name="max_lo">100</param></generator></id><property name="title" type="string" column="title_" /><property name="content" type="text" length="10000" column="content_"></property><property name="postTime" type="timestamp" column="postTime_"></property><!-- 子类:Topic --><union-subclass name="Topic" table="t_topic"><property name="type" type="integer" column="type_"/></union-subclass><!-- 子类:Reply --><union-subclass name="Reply" table="t_reply"><property name="floor" type="integer" column="floor_"/></union-subclass></class></hibernate-mapping>
0 0
- Hibernate_映射_继承结构映射2_另外两种映射方式
- Hibernate_映射_继承结构映射1_使用一张表的方式
- Hibernate_映射_关联关系_一对一映射2_基于外键的方式
- Hibernate_映射_关联关系_一对一映射3_基于主键的方式
- @注解_继承映射
- Hibernate_继承映射
- Hibernate_映射继承关系
- Hibernate_映射_关联关系_一对多多对一映射2、inverse属性
- Hibernate_映射_关联关系_多对多映射2
- Hibernate_映射_关联关系_一对一映射1__映射基础
- Hibernate继承映射之三_用多张表映射2
- 17. Hibernate_映射继承关系
- Hibernate_映射_关联关系_一对多多对一映射1
- Hibernate_映射_关联关系_多对多映射1
- Hibernate_映射_关联关系_cascade属性、单向关联
- Hibernate入门07_继承映射01_discriminator方式
- Hibernate入门07_继承映射02_joined方式
- Hibernate入门07_继承映射03_union方式
- 大流量、高并发的网站的底层系统架构
- LinkedList<E> 及模拟栈结构
- DNS域名解析
- 大数据运算系统(2)--- 图计算系统
- 如何用消息系统避免分布式事务?
- Hibernate_映射_继承结构映射2_另外两种映射方式
- android 5.1预置apk
- Kafka集群搭建
- 高并发的核心技术-幂等的实现方案
- 扩大VMware虚拟机中CentOS 7的硬盘空间
- 浅谈反射机制(一)
- C#委托的介绍(delegate、Action、Func、predicate)
- mysql数据库使用Navicat时向Navicat导入sql文件时某字段过大时的处理
- 大数据运算系统(3)--- MapReduce+SQL