Starter III to hibernate association mapping (and many-to-many)

Tip; entry operations herein is a top and two

hibernate type of map

At the beginning of learning, I think many and many-to-be the same, but later found not the case, for example, the class teacher and the student, a class may have more than one student, but only one teacher, student and teacher the relationship is many, the relationship between teacher and students are many, the difference between the two is their directivity

  • One (one-to-one)
  • To-many (ont-to-many)
  • Many-to (many-to-one)
  • To-many (many-to-many)

To-many (ont-to-many) and many to one (many-to-one) doing the most common and popular type of mapping

 

To-many (ont-to-many)

1, create a class table and the table of students, related through foreign key (

alter table Students add constraint fk_students_gid foreign key (gid) references grade(gid);

 

2, the table creating entity class and student class table (attribute / default constructor, parameterized constructors, get, and set methods, toString Method)

int GID Private; 
Private String gname;
Private String gdesc;
// define one of a set of multi-party
private Set <Students> students = new HashSet <Students> ();
//other. . . slightly }

 

public class Students {

    private int sid;
    private String sname;
    private String gender;
    private Date birthday;
    //private String address;
    private Blob pictur;
    private Address address;

//其他。。。略
}

 3、创建Grade.hbm.xml(我的Stuendt.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="com.hibernate.entity.Grade" table="grade">
        <id name="gid" column="gid" type="int">
            <generator class="increment"></generator>
        </id>
        <property name="gname" type="java.lang.String">
            <column name="gname"/>
        </property>
        <property name="gdesc" type="java.lang.String">
            <column name="gdesc"/>
        </property>
        <!--单项的一对多的关系-->
        <set name="students" table="Students">
            <!--指定的关联外键列-->
            <key column="gid"></key>
            <one-to-many class="com.hibernate.entity.Students"/>
        </set>

    </class>


</hibernate-mapping>

 4、在cfg.xml中配置映射文件路径

<!--指定映射文件的路径-->
    <mapping resource="com/hibernate/entity/Grade.hbm.xml"/>
    <mapping resource="com/hibernate/entity/Students.hbm.xml"/>

 5、创建HibernateUtil类

public class HibernadUtil {

    private static SessionFactory sessionFactory;
    private static Session session;

    static {
        StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
        sessionFactory = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
    }

    //获取SessionFacory
    public static SessionFactory getSessionFacory() {
        return sessionFactory;
    }

    //获取session
    public static Session getSession() {

        return sessionFactory.openSession();
    }

    //关闭session
    public static void closeSession(Session session) {
        if (null != session) {
            session.close();
        }
    }

 6、进行测试

 

public class TestStudentsAndGrade {

    public static void main(String[] args) {
        //add();
        //findStudensByGrade();
        //update();
        deleteStudentsBySid();
    }

    //给表中插入数据
    public static void add() {
        Grade grade = new Grade("道班", "道一");
        Students s = new Students("道一", "男", new Date());
        Students s2 = new Students("阴阳", "男", new Date());
        Students s3 = new Students("三生", "女", new Date());
        System.out.println(grade.getStudents().add(s));
        //如果希望在学生表中添加对应道班级编号,需要在班级中添加学生,建立关联关系
        grade.getStudents().add(s);
        grade.getStudents().add(s2);
        grade.getStudents().add(s3);

        Session session = HibernadUtil.getSession();
        //开启事务
        Transaction transaction = session.beginTransaction();
        session.save(grade);
        session.save(s);
        session.save(s2);
        session.save(s3);
        transaction.commit();
        HibernadUtil.closeSession(session);
    }

    //查询班级中包含的学生
    public static void findStudensByGrade() {
        //获取session
        Session session = HibernadUtil.getSession();
        //获取班级信息
        Grade grade = session.get(Grade.class, 1);
        //输出班级信息
        System.out.println(grade.getGname() + "," + grade.getGdesc());

        //找出班级信息,然后找出班级学生信息
        Set<Students> students = grade.getStudents();
        for (Students stu:students
             ) {
            System.out.println(stu);
        }
    }

    //修改学生信息
    public static void update(){
        Grade grade =new Grade("道","道二");
        Session session = HibernadUtil.getSession();
        Transaction transaction = session.beginTransaction();

        //修改学生所在班级信息(grade班级信息为自增长,别一不小心玩过头了)
        Students students = session.get(Students.class, 2);
        grade.getStudents().add(students);
        session.save(grade);
        transaction.commit();
        HibernadUtil.closeSession(session);
    }

    //从班级中删除学生信息
    public static  void deleteStudentsBySid(){

        Session session = HibernadUtil.getSession();
        Transaction transaction = session.beginTransaction();
        Students students = session.get(Students.class,9);
        session.delete(students);
        transaction.commit();
        HibernadUtil.closeSession(session);

    }

}

 7、一对多中的set元素属性

多对一(many-to-one)

 

Guess you like

Origin www.cnblogs.com/lindaiyu/p/10994131.html