码迷,mamicode.com
首页 > Web开发 > 详细

Hibernate入门(五)

时间:2017-06-18 22:48:10      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:encoding   bsp   XML   gets   provider   update   subclass   prim   nbsp   

一 Hibernate继承映射

  我们可以将继承层次结构类与数据库的表映射。

1. 每个层次类一张表

 技术分享

这个层次结构有三个类,EmployeeRegular_EmployeeContract_Employee类的超类(父类)。

此层次结构的表结构如下所示:

技术分享

XML实现

1)创建持久化类

Employee.java

package cn;

public class Employee {
	
	private int id;
	
	private String name;

	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;
	}
	
	

}

 Regular_Employee.java

package cn;

public class Regular_Employee extends Employee{

private float salary;

private int bonus;

public float getSalary() {
return salary;
}

public void setSalary(float salary) {
this.salary = salary;
}

public int getBonus() {
return bonus;
}

public void setBonus(int bonus) {
this.bonus = bonus;
}

}

  Contract_Employee.java

package cn;

public class Contract_Employee extends Employee{

private float pay_per_hour;
private String contract_duration;
public float getPay_per_hour() {
return pay_per_hour;
}
public void setPay_per_hour(float pay_per_hour) {
this.pay_per_hour = pay_per_hour;
}
public String getContract_duration() {
return contract_duration;
}
public void setContract_duration(String contract_duration) {
this.contract_duration = contract_duration;
}

}

  2)创建映射文件

在每个类层次结构一个表的情况下,hibernate框架添加了一个标识符列,该框架指定了记录的类型。 它主要用于区分记录。要指定它,必须指定类的 discriminator 子元素。
类的子类subelement指定子类。 在这种情况下,Regular_EmployeeContract_EmployeeEmployee类的子类。

Employee.hbm.xml

<?xml version=‘1.0‘ encoding=‘UTF-8‘?>  
<!DOCTYPE hibernate-mapping PUBLIC  
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  

 <hibernate-mapping>  
  <class name="cn.Employee" table="employee">  
    <id name="id">  
     <generator class="increment"></generator>  
    </id>  
    <discriminator column="type" type="string"></discriminator>
    <property name="name"></property>  
    
    <subclass name = "cn.Regular_Employee" discriminator-value="reg_emp">
    	<property name="salary"></property>
    	<property name="bonus"></property>
    </subclass>
    
    <subclass name = "cn.Contract_Employee" discriminator-value="con_emp">
    	<property name="pay_per_hour"></property>
    	<property name="contract_duration"></property>
    </subclass>
    
    
    
  </class>  

 </hibernate-mapping>

  3)在配置文件中增加Employee.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hbm2ddl.auto">update</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost/test
</property>
<property name="hibernate.connection.username">
root
</property>
<property name="hibernate.connection.password">
root
</property>
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="show_sql">true</property>
<property name="hibernate.cache.use_second_level_cache">true</property>
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

<!-- List of XML mapping files -->
<mapping resource="cn/Employee.hbm.xml"/>
</session-factory>
</hibernate-configuration>

  4)测试

package cn;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.User;

public class Test {

		public static void main(String[] args) {
			Configuration cfg = new Configuration();
			cfg.configure("hibernate.cfg.xml");
			SessionFactory factory = cfg.buildSessionFactory();
			Session session = factory.openSession();
			session.beginTransaction();
			
			Employee emp = new Employee();
			emp.setName("emp");
			
			Regular_Employee reg = new Regular_Employee();
			reg.setName("reg_emp");
			reg.setSalary(10000);
			reg.setBonus(1);
			
			Contract_Employee con = new Contract_Employee();
			con.setName("con_emp");
			con.setPay_per_hour(200);
			con.setContract_duration("20h");
			
			session.persist(emp);
			session.persist(reg);
			session.persist(con);
			
			
			session.getTransaction().commit();
			session.close();
		}
}

  技术分享

技术分享

 注解实现

1)创建持久化类

Employee.java

package cn;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

@Entity
@Table(name="employee")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="type",discriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue(value="employeee")
public class Employee {
	
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="id")
	private int id;
	
	@Column(name="name")
	private String name;

	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;
	}
	
	

}

  Regular_Employee.java

package cn;

import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
@DiscriminatorValue(value="reg")
public class Regular_Employee extends Employee{
	
	@Column(name="salary")
	private float salary;
	
	@Column(name="bonus")
	private int bonus;

	public float getSalary() {
		return salary;
	}

	public void setSalary(float salary) {
		this.salary = salary;
	}

	public int getBonus() {
		return bonus;
	}

	public void setBonus(int bonus) {
		this.bonus = bonus;
	}
	

}

  Contract_Employee.java

package cn;

import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;

@Entity
@DiscriminatorValue(value="con")
public class Contract_Employee extends Employee{
	
	@Column(name="pay_per_hour")
	private float pay_per_hour;
	@Column(name="contract_duration")
    private String contract_duration;
	public float getPay_per_hour() {
		return pay_per_hour;
	}
	public void setPay_per_hour(float pay_per_hour) {
		this.pay_per_hour = pay_per_hour;
	}
	public String getContract_duration() {
		return contract_duration;
	}
	public void setContract_duration(String contract_duration) {
		this.contract_duration = contract_duration;
	}
    

}

  2)配置文件中增加持久化类

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration SYSTEM 
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>
		<property name="hbm2ddl.auto">update</property>
		<property name="hibernate.dialect">
		org.hibernate.dialect.MySQLDialect
		</property>
		<property name="hibernate.connection.driver_class">
		com.mysql.jdbc.Driver
		</property>
		<property name="hibernate.connection.url">
		jdbc:mysql://localhost/test
		</property>
		<property name="hibernate.connection.username">
		root
		</property>
		<property name="hibernate.connection.password">
		root
		</property>
		<property name="show_sql">true</property>
		<mapping class="cn.Employee"/>
	 	<mapping class="cn.Regular_Employee"/>
	 	<mapping class="cn.Contract_Employee"/>
	</session-factory>
</hibernate-configuration>

  3)测试

package cn;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

public class Test {
	
	public static void main(String[] args) {
		
		SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
		Session session = factory.openSession();
		Transaction t = session.beginTransaction();
		
		t.begin();

		Employee emp = new Employee();
		emp.setName("emp");
		
		Regular_Employee reg = new Regular_Employee();
		reg.setName("reg_emp");
		reg.setSalary(10000);
		reg.setBonus(1);
		
		Contract_Employee con = new Contract_Employee();
		con.setName("con_emp");
		con.setPay_per_hour(200);
		con.setContract_duration("20h");
		
		session.persist(emp);
		session.persist(reg);
		session.persist(con);
		
		
		
		
		
		t.commit();
		
		session.close();
		
		
		
		
	}

}

  技术分享

技术分享

2. 每个具体类一张表

 数据库中将有三个表,彼此之间没有关系。

XML实现

1) 创建持久化类

Employee.java

package cnn;

public class Employee {
	
	private int id;
	
	private String name;

	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;
	}
	
	

}

Regular_Employee.java

package cnn;

public class Regular_Employee extends Employee{
	
	private float salary;
	
	private int bonus;

	public float getSalary() {
		return salary;
	}

	public void setSalary(float salary) {
		this.salary = salary;
	}

	public int getBonus() {
		return bonus;
	}

	public void setBonus(int bonus) {
		this.bonus = bonus;
	}
	
	

}

Contract_Employee.java

package cnn;

public class Contract_Employee extends Employee {
	private float pay_per_hour;
    private String contract_duration;
	public float getPay_per_hour() {
		return pay_per_hour;
	}
	public void setPay_per_hour(float pay_per_hour) {
		this.pay_per_hour = pay_per_hour;
	}
	public String getContract_duration() {
		return contract_duration;
	}
	public void setContract_duration(String contract_duration) {
		this.contract_duration = contract_duration;
	}
    

}

  

2)employee.hbm.xml

 

<?xml version=‘1.0‘ encoding=‘UTF-8‘?>  
<!DOCTYPE hibernate-mapping PUBLIC  
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  

 <hibernate-mapping>  
  <class name="cnn.Employee" table="employee">  
    <id name="id">  
     <generator class="increment"></generator>  
    </id>  
    <property name="name"></property>  
    
    <union-subclass name = "cnn.Regular_Employee" table="regular" >
    	<property name="salary"></property>
    	<property name="bonus"></property>
    </union-subclass>
    
    <union-subclass name = "cnn.Contract_Employee" table="contract">
    	<property name="pay_per_hour"></property>
    	<property name="contract_duration"></property>
    </union-subclass>
    
    
    
  </class>  

 </hibernate-mapping>

3) 在配置文件中增加hbm文件的映射

 

 	<!-- List of XML mapping files -->
   		<mapping resource="cnn/Employee.hbm.xml"/>

4)测试

package cnn;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.User;

public class Test {

		public static void main(String[] args) {
			Configuration cfg = new Configuration();
			cfg.configure("hibernate.cfg.xml");
			SessionFactory factory = cfg.buildSessionFactory();
			Session session = factory.openSession();
			session.beginTransaction();
			
			Employee emp = new Employee();
			emp.setName("emp");
			
			Regular_Employee reg = new Regular_Employee();
			reg.setName("reg_emp");
			reg.setSalary(10000);
			reg.setBonus(1);
			
			Contract_Employee con = new Contract_Employee();
			con.setName("con_emp");
			con.setPay_per_hour(200);
			con.setContract_duration("20h");
			
			session.persist(emp);
			session.persist(reg);
			session.persist(con);
			
			
			
			session.getTransaction().commit();
			session.close();
		}
}

 

注解实现

1)创建持久化类

Employee.java

package cnn;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

@Entity
@Table(name="employee")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Employee {
	
	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
	private int id;
	
	@Column(name = "name")
	private String name;

	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;
	}
	
	

}

Regular_Employee.java

package cnn;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name = "regular")
@AttributeOverrides({
        @AttributeOverride(name = "id", column = @Column(name = "id")),
        @AttributeOverride(name = "name", column = @Column(name = "name")) })
public class Regular_Employee extends Employee{
	
	@Column(name = "salary")
	private float salary;
	
	@Column(name = "bonus")
	private int bonus;

	public float getSalary() {
		return salary;
	}

	public void setSalary(float salary) {
		this.salary = salary;
	}

	public int getBonus() {
		return bonus;
	}

	public void setBonus(int bonus) {
		this.bonus = bonus;
	}
	
	

}

Contract_Employee.java

package cnn;

import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name = "contract")
@AttributeOverrides({
        @AttributeOverride(name = "id", column = @Column(name = "id")),
        @AttributeOverride(name = "name", column = @Column(name = "name")) })
public class Contract_Employee extends Employee {
	@Column(name = "pay_per_hour")
	private float pay_per_hour;
	@Column(name = "contract_duration")
    private String contract_duration;
	public float getPay_per_hour() {
		return pay_per_hour;
	}
	public void setPay_per_hour(float pay_per_hour) {
		this.pay_per_hour = pay_per_hour;
	}
	public String getContract_duration() {
		return contract_duration;
	}
	public void setContract_duration(String contract_duration) {
		this.contract_duration = contract_duration;
	}
    

}

2)在配置文件中添加hbm文件映射

	<mapping class="cnn.Employee"/>
	 	<mapping class="cnn.Regular_Employee"/>
	 	<mapping class="cnn.Contract_Employee"/>

3)测试类

package cnn;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

public class Test {
	
	public static void main(String[] args) {
		
		SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
		Session session = factory.openSession();
		Transaction t = session.beginTransaction();
		
		t.begin();

		Employee emp = new Employee();
		emp.setName("emp");
		
		Regular_Employee reg = new Regular_Employee();
		reg.setName("reg_emp");
		reg.setSalary(20000);
		reg.setBonus(12);
		
		Contract_Employee con = new Contract_Employee();
		con.setName("con_emp");
		con.setPay_per_hour(100);
		con.setContract_duration("30h");
		
		session.persist(emp);
		session.persist(reg);
		session.persist(con);
		
		
		
		
		
		t.commit();
		
		session.close();
		
		
		
		
	}

}

  

3. 每个子类一张表

 在每个子类一张表的情况下,子类映射表与主键和外键的关系与父类映射表相关。

XML实现

1)创建持久化类

Employee.java

package cnnn;

public class Employee {
	
	private int id;
	
	private String name;

	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;
	}
	
	

}

  Regular_Employee.java

 

package cnnn;

public class Regular_Employee extends Employee{
	
	private float salary;
	
	private int bonus;

	public float getSalary() {
		return salary;
	}

	public void setSalary(float salary) {
		this.salary = salary;
	}

	public int getBonus() {
		return bonus;
	}

	public void setBonus(int bonus) {
		this.bonus = bonus;
	}
	

}

  Contract_Employee.java

package cnnn;

public class Contract_Employee extends Employee{
	
	private float pay_per_hour;
    private String contract_duration;
	public float getPay_per_hour() {
		return pay_per_hour;
	}
	public void setPay_per_hour(float pay_per_hour) {
		this.pay_per_hour = pay_per_hour;
	}
	public String getContract_duration() {
		return contract_duration;
	}
	public void setContract_duration(String contract_duration) {
		this.contract_duration = contract_duration;
	}
    

}

2)Employee.hbm.xml

<?xml version=‘1.0‘ encoding=‘UTF-8‘?>  
<!DOCTYPE hibernate-mapping PUBLIC  
 "-//Hibernate/Hibernate Mapping DTD 3.0//EN"  
 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  

 <hibernate-mapping>  
  <class name="cnnn.Employee" table="employee">  
    <id name="id">  
     <generator class="increment"></generator>  
    </id>  
    <property name="name"></property>  
    
    <joined-subclass name = "cnnn.Regular_Employee" >
    	<key column="reg_id"></key>
    	<property name="salary"></property>
    	<property name="bonus"></property>
    </joined-subclass>
    
    <joined-subclass name = "cnnn.Contract_Employee" >
    	<key column="con_id"></key>
    	<property name="pay_per_hour"></property>
    	<property name="contract_duration"></property>
    </joined-subclass>
    
    
    
  </class>  

 </hibernate-mapping>

  

 

3)在配置文件中添加hbm文件的映射

<mapping resource="cnnn/Employee.hbm.xml"/>

4)测试

package cnnn;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import com.User;

public class Test {

		public static void main(String[] args) {
			Configuration cfg = new Configuration();
			cfg.configure("hibernate.cfg.xml");
			SessionFactory factory = cfg.buildSessionFactory();
			Session session = factory.openSession();
			session.beginTransaction();
			
			Employee emp = new Employee();
			emp.setName("emp");
			
			Regular_Employee reg = new Regular_Employee();
			reg.setName("reg_emp");
			reg.setSalary(10000);
			reg.setBonus(1);
			
			Contract_Employee con = new Contract_Employee();
			con.setName("con_emp");
			con.setPay_per_hour(200);
			con.setContract_duration("20h");
			
			session.persist(emp);
			session.persist(reg);
			session.persist(con);
			
			
			
			session.getTransaction().commit();
			session.close();
		}
}

注解实现

1)创建持久化类

Employee.java

package cnnn;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

@Entity
@Table(name="employee")
@Inheritance(strategy = InheritanceType.JOINED)
public class Employee {
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
	private int id;
	
	@Column(name = "name")
	private String name;

	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;
	}
	
	

}

  Regular_Employee.java

package cnnn;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table(name = "regular")
@PrimaryKeyJoinColumn(name = "ID")
public class Regular_Employee extends Employee{
	
	@Column(name = "salary")
	private float salary;
	
	@Column(name = "bonus")
	private int bonus;

	public float getSalary() {
		return salary;
	}

	public void setSalary(float salary) {
		this.salary = salary;
	}

	public int getBonus() {
		return bonus;
	}

	public void setBonus(int bonus) {
		this.bonus = bonus;
	}
	
	

}

  

package cnnn;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table(name = "contract")
@PrimaryKeyJoinColumn(name = "ID")
public class Contract_Employee extends Employee {
	@Column(name = "pay_per_hour")
	private float pay_per_hour;
	@Column(name = "contract_duration")
    private String contract_duration;
	public float getPay_per_hour() {
		return pay_per_hour;
	}
	public void setPay_per_hour(float pay_per_hour) {
		this.pay_per_hour = pay_per_hour;
	}
	public String getContract_duration() {
		return contract_duration;
	}
	public void setContract_duration(String contract_duration) {
		this.contract_duration = contract_duration;
	}
    

}

2)在配置文件中增加配置

	<mapping class="cnnn.Employee"/>
	 	<mapping class="cnnn.Regular_Employee"/>
	 	<mapping class="cnnn.Contract_Employee"/>

3)测试

package cnnn;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

public class Test {
	
	public static void main(String[] args) {
		
		SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
		Session session = factory.openSession();
		Transaction t = session.beginTransaction();
		
		t.begin();

		Employee emp = new Employee();
		emp.setName("emp");
		
		Regular_Employee reg = new Regular_Employee();
		reg.setName("reg_emp");
		reg.setSalary(20000);
		reg.setBonus(12);
		
		Contract_Employee con = new Contract_Employee();
		con.setName("con_emp");
		con.setPay_per_hour(100);
		con.setContract_duration("30h");
		
		session.persist(emp);
		session.persist(reg);
		session.persist(con);
		
		
		
		
		
		t.commit();
		
		session.close();
		
		
		
		
	}

}

  

Hibernate入门(五)

标签:encoding   bsp   XML   gets   provider   update   subclass   prim   nbsp   

原文地址:http://www.cnblogs.com/pebbles/p/6869778.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!