JPA初探02-継承関係と簡単な一対多関係の注釈実現


【例背景紹介】2種類のエンティティークラス:1.学生クラスStudenBean学生クラスの下には、スポーツ特技生SportsStudentBean、芸術生PrintStudentBeanの2つのサブクラスがある.クラスクラスクラスClazzBean
関係:1.StudenBeanはSportsStudentBeanとPrintStudentBeanの親2である.ClazzBeanとStudenBeanは一対多の関係です
 
【データベーステーブル】
create table t_student(
 sid int primary key,
 sname varchar(45),
 clazzid int,
 studytype varchar(45),
 remark varchar(45)
);

 
create table t_clazz(
	classid int primary key,
	classname varchar(45)
);

【Javaファイルの設計】
【   】
package bean;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/**
 *    
 * @author   
 * @date 2014 10 16 
 * @file name: StudentBean.java
 */
@Entity
@Table(name="t_student")
@Inheritance(strategy=InheritanceType.SINGLE_TABLE) //                          
@DiscriminatorColumn(name="studytype") //                       ,name             
@DiscriminatorValue("normal")  //       
public class StudentBean {
	@Id
	@Column(name="sid")
	private int stuId;
	
	@Column(name="sname")
	private String name;
	
	@ManyToOne(cascade=CascadeType.ALL)//     ClazzBean    
	@JoinColumn(name="clazzid") //                  
	private ClazzBean clazz;
	
	
	public int getStuId() {
		return stuId;
	}
	public void setStuId(int stuId) {
		this.stuId = stuId;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public ClazzBean getClazz() {
		return clazz;
	}
	public void setClazz(ClazzBean clazz) {
		this.clazz = clazz;
	}
	
	
}
【     】 
package bean;

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

/**
 *      
 * @author   
 * @date 2014 10 16 
 * @file name: SportsStudentBean.java
 */
@Entity
@DiscriminatorValue("sport")//       
public class SportsStudentBean extends StudentBean{
	@Column(name="remark")
	private String sport;

	public String getSport() {
		return sport;
	}

	public void setSport(String sport) {
		this.sport = sport;
	}
	
	
}

【   】
package bean;

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

/**
 *     
 * @author   
 * @date 2014 10 16 
 * @file name: PrintStudentBean.java
 */
@Entity
@DiscriminatorValue("print")//       
public class PrintStudentBean extends StudentBean{
	@Column(name="remark")
	private String print;

	public String getPrint() {
		return print;
	}

	public void setPrint(String print) {
		this.print = print;
	}
	
	
}

【   】
package bean;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 *    
 * @author   
 * @date 2014 10 16 
 * @file name: ClazzBean.java
 */
@Entity
@Table(name="t_clazz")
public class ClazzBean {
	@Id
	//         identity:       。                       ,Oracle   
	@GeneratedValue(strategy=GenerationType.IDENTITY)
	@Column(name="classid")
	private int clazzId;
	
	@Column(name="classname")
	private String clazzName;
	
	//mappedBy="clazz"  clazz       many                
	@OneToMany(cascade={CascadeType.MERGE,CascadeType.PERSIST},fetch=FetchType.LAZY,mappedBy="clazz") 
	private Set<StudentBean> set;
	
	public int getClazzId() {
		return clazzId;
	}
	public void setClazzId(int clazzId) {
		this.clazzId = clazzId;
	}
	public String getClazzName() {
		return clazzName;
	}
	public void setClazzName(String clazzName) {
		this.clazzName = clazzName;
	}
	public Set<StudentBean> getSet() {
		return set;
	}
	public void setSet(Set<StudentBean> set) {
		this.set = set;
	}
	
	
}

【   】

package com.jpa.test;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;

import util.JPAManagerFactory;
import bean.ClazzBean;
import bean.PrintStudentBean;
import bean.SportsStudentBean;
import bean.StudentBean;

public class StudentTest {
	public static void main(String[] args) {
		EntityManager manager = JPAManagerFactory.getMFInstance().getManager();
		
//		addTest(manager);
		
		findTest(manager);
		
		
	}
	static void findTest(EntityManager manager){
		ClazzBean clazz = manager.find(ClazzBean.class,8);
		
		System.out.println(clazz.getClazzName());
		
		Set<StudentBean> set = clazz.getSet();
		for(StudentBean stu : set){
			System.out.println(stu.getName());
		}
	}
	static void addTest(EntityManager manager){
		//      
				ClazzBean c1 = new ClazzBean();
				c1.setClazzName("    ");
				ClazzBean c2 = new ClazzBean();
				c2.setClazzName("    ");
				
				//  3   
				StudentBean stu1 = new StudentBean();
				stu1.setClazz(c1);
				stu1.setName("  ");
				stu1.setStuId(1);
				
				SportsStudentBean stu2 = new SportsStudentBean();
				stu2.setClazz(c2);
				stu2.setName("  ");
				stu2.setStuId(2);
				stu2.setSport("basketball");
				
				PrintStudentBean stu3 = new PrintStudentBean();
				stu3.setClazz(c2);
				stu3.setName("  ");
				stu3.setStuId(3);
				stu3.setPrint("person print");
				//    
				EntityTransaction t = manager.getTransaction();
				t.begin();
				//          
				Set<StudentBean> set1 = new HashSet<StudentBean>();
				set1.add(stu1);
				Set<StudentBean> set2 = new HashSet<StudentBean>();
				set2.add(stu3);
				set2.add(stu2);
				
				c1.setSet(set1);
				c2.setSet(set2);
				//     
				manager.persist(c1);
				manager.persist(c2);
				
				
				t.commit();
				manager.close();
	}
}

 
原理の解説が少なく、私も初学です.JPAの注釈とhibernateのXMLのコンフィギュレーションファイルの差は少ないような気がしますが、hibernateコンフィギュレーションファイルのベースがあればannotationの使用行を学ぶだけです.