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类没有变化。

配置

   	 		 			 			 		 		 			 		 		 			 		 		 		 			 			 		 		 			 			 		 	  

测试

与第一种相同
 

相关内容

热门资讯

揭幕透视!hhpoker开挂教... 揭幕透视!hhpoker开挂教程!玩家必看教程辅助器(真的有挂)-哔哩哔哩1、完成hhpoker开挂...
今日头条!wepoker好友助... 今日头条!wepoker好友助力码,江西闲娱脚本,一贯真的有辅助下载(新版有挂)1、在江西闲娱脚本插...
事发当天!hhpoker是内部... 事发当天!hhpoker是内部控制吗(透视)都是是真的辅助下载(有挂详细)-哔哩哔哩1、这是跨平台的...
据通报!玉海楼茶苑价格表,多乐... 据通报!玉海楼茶苑价格表,多乐跑得快游戏辅助脚本,课程教程(有挂教程)-哔哩哔哩1、这是跨平台的多乐...
避坑细节!wepoker私人辅... 避坑细节!wepoker私人辅助器,中至余干有挂吗,本来存在有辅助脚本(有挂分享)1、全新机制【中至...
分享透视!德州局透视脚本下载安... 分享透视!德州局透视脚本下载安装最新版本!今日焦点辅助脚本(有挂技巧)-哔哩哔哩1、用户打开应用后不...
近期!wepoker正确养号方... 近期!wepoker正确养号方法(透视)切实真的是有辅助下载(有挂教学)-哔哩哔哩1、起透看视 we...
现就发布提示!浙江游戏大厅修改... 现就发布提示!浙江游戏大厅修改器,途游破解版无限钻石,窍门教程(的确有挂)-哔哩哔哩浙江游戏大厅修改...
有挂透视!wepoker代打辅... 有挂透视!wepoker代打辅助机器人!科技通报辅助软件(确实有挂)-哔哩哔哩1、打开软件启动之后找...
今日百科!hhpoker哪个俱... 今日百科!hhpoker哪个俱乐部靠谱,新老夫子挂,果然真的是有辅助插件(有挂透视)今日百科!hhp...