HIBERNATE配置注解 一对多

hibernate.cfg.xml同上 加入Classes 、Student

Classes.java

package com.yulin.hibernate.pojo;

import java.util.LinkedHashSet;
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;


@Entity
@Table(name="class")
public class Classes extends Base {
    
   
    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    @Column
    private Integer id;
   
   
    @Column
    private String className;
   
    @Column
    private Integer classNum;
   
   
    // cascade表示级联操作  
    // CascadeType.MERGE级联更新:若items属性修改了那么order对象保存时同时修改items里的对象。对应EntityManager的merge方法  
    // CascadeType.PERSIST级联刷新:获取order对象里也同时也重新获取最新的items时的对象。对应EntityManager的refresh(object)方法有效。即会重新查询数据库里的最新数据  
    // CascadeType.REFRESH级联保存:对order对象保存时也对items里的对象也会保存。对应EntityManager的presist方法  
    // CascadeType.REMOVE级联删除:对order对象删除也对items里的对象也会删除。对应EntityManager的remove方法  
    // FetchType.LAZY表示懒加载。对于xxxtoMany时即获得多的一方fetch的默认值是FetchType.LAZY懒加载。对于xxxtoOne时即获得一的一方fetch的默认值是FetchType.EAGER立即加载  
    // mappedBy="classes"就相当于inverse=true,(mappedBy指定的是不需要维护关系的一端)     
    //请检查orderItems这个属性是否使用了泛型,如:Set.

    //如果未使用泛型,请在OneToMany注释中使用targetEntity指定元素的类型

    @OneToMany(cascade = { CascadeType.ALL}, fetch = FetchType.LAZY ,targetEntity = Student.class,mappedBy="classes") 
    private Set students= new LinkedHashSet();

    public Set getStudents() {
        return students;
    }

    public void setStudents(Set students) {
        this.students = students;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public Integer getClassNum() {
        return classNum;
    }

    public void setClassNum(Integer classNum) {
        this.classNum = classNum;
    }
   
   

}

 



Student.java

package com.yulin.hibernate.pojo;

import java.util.LinkedHashSet;
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.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;





@Entity
@Table
public class Student extends Base {
   
    public Student(){}
   
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Integer id;
   
   
    @Column(name="studentName")
    private String name;
   
    @Column(name="studentAge")
    private Integer age;
   
    // cascade表示级联。CascadeType.REFRESH级联刷新  
    // optional表示该对象可有可无,它的值为true表示该外键可以为null,它的值为false表示该外键为not null  
    @ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER, optional = false)  
    // JoinColumn表示外键的列  
    @JoinColumn(name="classId")
    private Classes classes;
   
   

    public Classes getClasses() {
        return classes;
    }

    public void setClasses(Classes classes) {
        this.classes = classes;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

   
   
}


 
Test.java

package com.yulin.hibernate.test;

import java.util.Date;

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

import com.yulin.hibernate.pojo.Classes;
import com.yulin.hibernate.pojo.Person;
import com.yulin.hibernate.pojo.Role;
import com.yulin.hibernate.pojo.Student;
import com.yulin.hibernate.pojo.Users;

public class Test {

    public static final SessionFactory sessionFactory;

    static {
sessionFactory =  new AnnotationConfiguration().configure(  
        "/hibernate.cfg.xml").buildSessionFactory();
    }

    public static final ThreadLocal session = new ThreadLocal();

    public static Session currentSession() throws HibernateException {
Session s = session.get();
if (s == null) {
     s = sessionFactory.openSession();
     session.set(s);
}
return s;
    }

    public static void closeSession() throws HibernateException {
Session s = session.get();
if (s != null) {
     s.close();
}
session.set(null);
    }

    
    

    
public static void delOneToMany(){
Session session=currentSession();
org.hibernate.Transaction tran=session.beginTransaction();
Student student=(Student)session.get(Student.class, 5);
session.delete(student);
tran.commit();
closeSession();

    }
   
   
    public static void addOneToMany(){
Session session=currentSession();
org.hibernate.Transaction tran=session.beginTransaction();
Classes classs= new Classes();
classs.setClassName("1班");
classs.setClassNum(1);

Student student = new Student();
student.setName("小李");
student.setAge(12);
student.setClasses(classs);


Student student1 = new Student();
student1.setName("小娇");
student1.setAge(10);
student1.setClasses(classs);

classs.getStudents().add(student);
classs.getStudents().add(student1);

session.save(classs);
tran.commit();
closeSession();
    }
    
  
    
    
  public static void main(String[] args) {
    addOneToMany();
    //delOneToMany();
    }

}

 

 

你可能感兴趣的:(Hibernate)