Hibernate_映射_继承结构映射2_另外两种映射方式

48 篇文章 0 订阅


   
   


   
   

   
   
	
    
    
	
    
    
		
     
     
			
      
      
		
     
     
		
     
     
		
     
     

		
     
     
		
     
     
		
     
     

		
     
     
		
     
     
			
      
      
		
     
     

		
     
     
		
     
     
			
      
      
		
     
     
	
    
    

   
   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();

	// 保存,有关联关系
	@Test
	public 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();
		}
	}

	// 获取,可以获取到关联的对方
	@Test
	public 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值