注册 登录  
 加关注
查看详情
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

清风幻影的博客

Where there is love, I will be there.

 
 
 

日志

 
 
关于我

同是天涯沦落人,相逢何必曾相识. 天生我材必有用,千金散尽还复来. 天若有情天亦老,人间正道是沧桑. 月影西斜人已去, 堤上梅花情依旧, 此情故已成追忆, 美人如玉夜留香

hibernate注解-一对多  

2012-10-10 10:53:36|  分类: hiber_1 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

http://blog.163.com/tsing_hua/blog/static/139622224201092164528112/

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<Student>.

    //如果未使用泛型,请在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> session = new ThreadLocal<Session>();

    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();
 
    }
   
    /**
     * one-to-many
     */
    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();
    }
  评论这张
 
阅读(744)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2018