Hibernate 继承关系映射
创始人
2024-11-13 07:37:20
0次

hibernate对类的继承关系的三种策略。以动物(Animal、Pig、Bird)为例

一、所有类的属性(字段)放入在一张表中

特点,数据库查询效率高,但粒度比较粗,会有冗余字段,配置特点是在配置文件中添加了discriminator-value标签属性。

java类
 

package test.hibernate.spring.model;  public class Animal { 	private int id; 	private String name; 	private String sex; 	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; 	} 	public String getSex() { 		return sex; 	} 	public void setSex(String sex) { 		this.sex = sex; 	} 	public Animal() { 		super(); 	} 	@Override 	public String toString() { 		return "Animal [id=" + id + ", name=" + name + ", sex=" + sex + "]"; 	} 	  }
package test.hibernate.spring.model;  public class Bird extends Animal { 	private int height;  	public int getHeight() { 		return height; 	}  	public void setHeight(int height) { 		this.height = height; 	}  	public Bird() { 		super(); 	}  	@Override 	public String toString() { 		return "Bird [height=" + height + "]"; 	} 	  }
package test.hibernate.spring.model;  public class Pig extends Animal { 	private int weight;  	public int getWeight() { 		return weight; 	}  	public void setWeight(int weight) { 		this.weight = weight; 	}  	public Pig() { 		super(); 	}  	@Override 	public String toString() { 		return "Pig [weight=" + weight + "]"; 	} 	 	  }

配置文件

                                                                                                                                                                                                       

测试

package test.hibernate.spring;  import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.boot.MetadataSources; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.junit.After; import org.junit.Before; import org.junit.Test;  import test.hibernate.spring.model.Bird; import test.hibernate.spring.model.Pig;  public class TestSession { 	SessionFactory sessionFactory = null; 	Session session=null; 	Transaction ts=null; 	@Before 	public void beforP() { 		System.out.println("begin...."); 		/* hibernate规定,所有的配置或服务,必须配置或注册到一个服务注册类中 */ 		  Configuration configuration = new Configuration().configure(); 		  ServiceRegistry sr=configuration.getStandardServiceRegistryBuilder().build(); 		/* 从注册类中获得工厂类 */ 		  sessionFactory=new MetadataSources(sr).buildMetadata().buildSessionFactory(); 		/* 通过工厂类开启Session */ 		  session=sessionFactory.openSession(); 			/* 开启事务 */ 		  ts=session.beginTransaction(); 					} 	@After 	public void endP() { 		System.out.println("end...."); 		/* 提交事务 */ 		ts.commit(); 		/* 关闭Session */ 		session.close(); 		/* 关闭工厂 */ 		sessionFactory.close(); 		} 	@Test 	public void add() { 		Pig p=new Pig(); 		p.setName("ls"); 		p.setSex("公"); 		p.setWeight(300); 		 		Bird b=new Bird(); 		b.setName("zs"); 		b.setSex("母"); 		b.setHeight(500); 		session.save(b); 		 	} }

二、每个类一张表,用joined-subclass标签来配置,java类没有变化。

配置

   	 		 			 			 		 		 			 		 		 			 		 		 		 			 			 		 		 			 			 		 	  

测试

与第一种相同
 

相关内容

热门资讯

玩家交流!精品乐清麻将辅助脚本... 玩家交流!精品乐清麻将辅助脚本软件工具,精品乐清麻将从前有辅助解密APP(有挂讲解);1、很好的精品...
黑科技教程!嵊州越乡游麻将开挂... 黑科技教程!嵊州越乡游麻将开挂脚本软件工具,嵊州越乡游麻将真是真的有开挂揭幕APP(真的有挂);嵊州...
总算清楚!!逸趣吉安透视挂脚本... 总算清楚!!逸趣吉安透视挂脚本软件工具,逸趣吉安切实有透视挂详情APP(有挂头条)是一款可以让一直输...
玩家必看教程!!苍南麻将透视脚... 玩家必看教程!!苍南麻将透视脚本软件工具,苍南麻将原本真的有透视详情APP(有挂技术);详细苍南麻将...
必知教程!乐友开挂脚本软件工具... 必知教程!乐友开挂脚本软件工具,乐友一向真的有开挂必备APP(有挂技巧)是一款可以让一直输的玩家,快...
一分钟教你!众胜麻将开挂脚本软... 一分钟教你!众胜麻将开挂脚本软件工具,众胜麻将素来是有开挂了解APP(有挂详情);众胜麻将软件透视开...
一分钟带你了解!土豆互娱辅助挂... 一分钟带你了解!土豆互娱辅助挂脚本软件工具,土豆互娱起初有辅助挂曝光APP(有挂技巧);1、这是跨平...
必备科技!来一盘河南麻将开挂脚... 必备科技!来一盘河南麻将开挂脚本软件工具,来一盘河南麻将果然是真的有开挂解迷APP(确实有挂);来一...
玩家亲测!川渝互娱辅助挂脚本软... 玩家亲测!川渝互娱辅助挂脚本软件工具,川渝互娱最初有辅助挂了解APP(有挂辅助),川渝互娱是用手机号...
一分钟揭秘!决胜奕福辅助脚本软... 一分钟揭秘!决胜奕福辅助脚本软件工具,决胜奕福原本真的有辅助教你APP(有挂神器);1、决胜奕福ai...