Hibernate表关系

Hibernate多对一

package Hibernate.entity;

public class ClassRoom {
    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;
    }

}
package Hibernate.entity;

public class Student {
    private int id;
    private String name;
    private ClassRoom classRoom;

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

    public ClassRoom getClassRoom() {
        return classRoom;
    }

    public void setClassRoom(ClassRoom classRoom) {
        this.classRoom = classRoom;
    }

}
package Test;

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

import Hibernate.entity.ClassRoom;
import Hibernate.entity.Student;

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

            ClassRoom ClassRoom=new ClassRoom();
            ClassRoom.setName("工程班");
            session.save(ClassRoom);


            Student Student=new Student();
            Student.setName("王温帅");
            Student.setClassRoom(ClassRoom);
            session.save(Student);


            session.getTransaction().commit();
        } catch (Exception e) {
            session.getTransaction().rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
}
/*ClassRoom.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 package="Hibernate.entity">
    <class name="ClassRoom" table="T_ClassRoom">
        <id name="id">
            <generator class="native" />
        </id>
        <property name="name"></property>
    </class>
</hibernate-mapping>  
/*Student.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 package="Hibernate.entity">
    <class name="Student" table="T_Student">
        <id name="id">
            <generator class="native" />
        </id>
        <property name="name"></property>
        <many-to-one name="classRoom" column="fk_cid"/>
    </class>
</hibernate-mapping>  
/*
生成语句:
Hibernate: create table T_ClassRoom (id number(10,0) not null, name varchar2(255 char), primary key (id))
Hibernate: create table T_Student (id number(10,0) not null, name varchar2(255 char), fk_cid number(10,0), primary key (id))
Hibernate: create table T_Test (id number(10,0) not null, name varchar2(255 char), possword varchar2(255 char), primary key (id))
Hibernate: alter table T_Student add constraint FKrdsgbacah02r5rudmn8fcxhd0 foreign key (fk_cid) references T_ClassRoom
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into T_ClassRoom (name, id) values (?, ?)
Hibernate: insert into T_Student (name, fk_cid, id) values (?, ?, ?)
*/
/*数据库:*/

这里写图片描述
这里写图片描述

/*
创建两个entity:
一个Student类:第三个字段是ClassRoom
一个ClassRoom类:
xml文件必须在entity包下面
根据操作多对一就完成了
*/

这里写图片描述


Hibernate一对多

package Hibernate.entity;

public class Student {
    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;
    }

}
package Hibernate.entity;

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

public class ClassRoom {
    private int id;
    private String name;
    private Set<Student> setlist;

    public Set<Student> getSetlist() {
        return setlist;
    }

    public void setSetlist(Set<Student> setlist) {
        this.setlist = setlist;
    }

    public ClassRoom() {
        setlist = new HashSet<Student>();
    }

    public void getSetlist(Student student) {
        setlist.add(student);
    }

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

}
package Test;

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

import Hibernate.entity.ClassRoom;
import Hibernate.entity.Student;

public class Test {
    public static void main(String[] args) {
        SessionFactory factory = new Configuration().configure().buildSessionFactory();
        Session session = factory.openSession();
        try {
            session.beginTransaction();
            Student Student=new Student();
            Student.setName("小红");
            session.save(Student);



            ClassRoom ClassRoom=new ClassRoom();
            ClassRoom.setName("一班");
            ClassRoom.getSetlist(Student);
            session.save(ClassRoom);


            session.getTransaction().commit();
        } catch (Exception e) {
            session.getTransaction().rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
  PUBLIC "-//Hibernate?Hibernate Mapping DTD//EN"
  "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package ="Hibernate.entity">
    <class name="Student" table="T_Student">
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="name"></property>
    </class>
</hibernate-mapping>  
<?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 package="Hibernate.entity">
    <class name="ClassRoom" table="T_ClassRoom">
        <id name="id">
            <generator class="native" />
        </id>
        <property name="name"></property>
        <set name="setlist">
            <key column="fk_cid"></key>
            <one-to-many class="Student"></one-to-many>
        </set>
    </class>
</hibernate-mapping>  
/*
生成语句:
Hibernate: alter table T_Student add constraint FKrdsgbacah02r5rudmn8fcxhd0 foreign key (fk_cid) references T_ClassRoom
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into T_Student (name, id) values (?, ?)
Hibernate: insert into T_ClassRoom (name, id) values (?, ?)
Hibernate: update T_Student set fk_cid=? where id=?
*/
/*数据库*/

这里写图片描述
这里写图片描述

/*
创建两个entity:
一个Student类:
一个ClassRoom类:第三个字段是Student类型的Set<Student> setlist集合
xml文件必须在entity包下面
根据操作一对多就完成了
*/

Hibernate双向

package Hibernate.entity;

public class Student {
    private int id;
    private String name;
    private ClassRoom classRoom;

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

    public ClassRoom getClassRoom() {
        return classRoom;
    }

    public void setClassRoom(ClassRoom classRoom) {
        this.classRoom = classRoom;
    }

}
package Hibernate.entity;

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

public class ClassRoom {
    private int id;
    private String name;
    private Set<Student> setlist;

    public Set<Student> getSetlist() {
        return setlist;
    }

    public void setSetlist(Set<Student> setlist) {
        this.setlist = setlist;
    }

    public ClassRoom() {
        setlist = new HashSet<Student>();
    }

    public void getSetlist(Student student) {
        setlist.add(student);
    }

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

}
package Test;

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

import Hibernate.entity.ClassRoom;
import Hibernate.entity.Student;

public class Test {
    public static void main(String[] args) {
        SessionFactory factory = new Configuration().configure().buildSessionFactory();
        Session session = factory.openSession();
        try {
            session.beginTransaction();
            ClassRoom ClassRoom=new ClassRoom();
            ClassRoom.setName("一班");
            session.save(ClassRoom);

            Student Student=new Student();
            Student.setName("小红");
            Student.setClassRoom(ClassRoom);
            session.save(Student);

            session.getTransaction().commit();
        } catch (Exception e) {
            session.getTransaction().rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
  PUBLIC "-//Hibernate?Hibernate Mapping DTD//EN"
  "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package ="Hibernate.entity">
    <class name="Student" table="T_Student">
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="name"></property>
        <many-to-one name="classRoom" column="fk_cid"></many-to-one>
    </class>
</hibernate-mapping>  
<!DOCTYPE hibernate-mapping
  PUBLIC "-//Hibernate?Hibernate Mapping DTD//EN"
  "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="Hibernate.entity">
    <class name="ClassRoom" table="T_ClassRoom">
        <id name="id">
            <generator class="native" />
        </id>
        <property name="name"></property>
        <set name="setlist" inverse="true">
            <key column="fk_cid"></key>
            <one-to-many class="Student"></one-to-many>
        </set>
    </class>
</hibernate-mapping>  
/*
生成语句:
Hibernate: alter table T_Student add constraint FKrdsgbacah02r5rudmn8fcxhd0 foreign key (fk_cid) references T_ClassRoom
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into T_ClassRoom (name, id) values (?, ?)
Hibernate: insert into T_Student (name, fk_cid, id) values (?, ?, ?)
*/
/*数据库*/

这里写图片描述
这里写图片描述

/*
创建两个entity:
一个Student类:第三个字段是ClassRoom类型
一个ClassRoom类:第三个字段是Student类型的Set<Student> setlist集合
xml文件必须在entity包下面
根据操作双向就完成了
*/

Hibernate多对多

package Hibernate.entity;

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

public class Student {
    private int id;
    private String name;
    private Set<ClassRoom> setlist;


    public Student() {
        setlist = new HashSet<ClassRoom>();
    }

    public void getSetlist(ClassRoom ClassRoom) {
        setlist.add(ClassRoom);
    }

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

    public Set<ClassRoom> getSetlist() {
        return setlist;
    }

    public void setSetlist(Set<ClassRoom> setlist) {
        this.setlist = setlist;
    }


}
package Hibernate.entity;

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

public class ClassRoom {
    private int id;
    private String name;
    private Set<Student> setlist;

    public ClassRoom() {
        setlist = new HashSet<Student>();
    }

    public void getSetlist(Student student) {
        setlist.add(student);
    }

    public Set<Student> getSetlist() {
        return setlist;
    }

    public void setSetlist(Set<Student> setlist) {
        this.setlist = setlist;
    }

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

}
package Test;

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

import Hibernate.entity.ClassRoom;
import Hibernate.entity.Student;

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


            Student Student=new Student();
            Student.setName("王温帅");
            session.save(Student);

            ClassRoom ClassRoom=new ClassRoom();
            ClassRoom.setName("牛逼班级");
            session.save(ClassRoom);

            Student.getSetlist(ClassRoom);



            session.getTransaction().commit();
        } catch (Exception e) {
            session.getTransaction().rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
}
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
  PUBLIC "-//Hibernate?Hibernate Mapping DTD//EN"
  "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package ="Hibernate.entity">
    <class name="Student" table="T_Student">
        <id name="id" column="Student_id">
            <generator class="native"/>
        </id>
        <property name="name"></property>
        <set name="setlist" table="Student_ClassRoom">
            <key column="Student_id"></key>
            <many-to-many class="ClassRoom" column="ClassRoom_id"></many-to-many>
        </set>
    </class>
</hibernate-mapping>  
<!DOCTYPE hibernate-mapping
  PUBLIC "-//Hibernate?Hibernate Mapping DTD//EN"
  "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="Hibernate.entity">
    <class name="ClassRoom" table="T_ClassRoom">
        <id name="id" column="ClassRoom_id">
            <generator class="native" />
        </id>
        <property name="name"></property>
        <set name="setlist" inverse="true">
            <key column="ClassRoom_id"></key>
            <many-to-many class="Student" column="Student_id"></many-to-many>
        </set>
    </class>
</hibernate-mapping>  
/*
生成语句:
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: select hibernate_sequence.nextval from dual
Hibernate: insert into T_Student (name, Student_id) values (?, ?)
Hibernate: insert into T_ClassRoom (name, ClassRoom_id) values (?, ?)
Hibernate: insert into Student_ClassRoom (Student_id, ClassRoom_id) values (?, ?)
*/
/*数据库*/

这里写图片描述
这里写图片描述
这里写图片描述

/*hibernate.cfg.xml*/
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:SHUAI</property>
        <property name="hibernate.connection.username">user_shuai</property>
        <property name="hibernate.connection.password">0</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.default_schema">user_shuai</property>
        <property name="show_sql">true</property>
        <mapping resource="Hibernate/entity/Student.hbm.xml"/>
        <mapping resource="Hibernate/entity/ClassRoom.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

Hibernate双向(Annotation)

package Hibernate.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name="T_Student")
public class Student {
    private int id;
    private String name;
    private ClassRoom classRoom;

    @Id
    @GenericGenerator(name="myGeneric",strategy="increment")
    @GeneratedValue(generator="myGeneric")
    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;
    }
    @ManyToOne
    @JoinColumn(name="cid")
    public ClassRoom getClassRoom() {
        return classRoom;
    }
    public void setClassRoom(ClassRoom classRoom) {
        this.classRoom = classRoom;
    }
}
package Hibernate.entity;

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

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;
@Entity
@Table(name="T_ClassRoom")
public class ClassRoom {
    private int id;
    private String name;
    private Set<Student> setlist;

    public ClassRoom() {
        setlist = new HashSet<Student>();
    }
    public void getSetlist(Student student) {
        setlist.add(student);
    }

    @OneToMany(mappedBy="classRoom")
    public Set<Student> getSetlist() {
        return setlist;
    }

    public void setSetlist(Set<Student> setlist) {
        this.setlist = setlist;
    }
    @Id
    @GenericGenerator(name = "myGeneric", strategy = "increment")
    @GeneratedValue(generator = "myGeneric")
    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;
    }
}
package Test;

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

import Hibernate.entity.ClassRoom;
import Hibernate.entity.Student;

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

            ClassRoom ClassRoom=new ClassRoom();
            ClassRoom.setName("牛逼班级");
            session.save(ClassRoom);

            Student Student=new Student();
            Student.setName("王温帅");
            Student.setClassRoom(ClassRoom);
            session.save(Student);


            session.getTransaction().commit();
        } catch (Exception e) {
            session.getTransaction().rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
    }
}
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:SHUAI</property>
        <property name="hibernate.connection.username">user_shuai</property>
        <property name="hibernate.connection.password">0</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.default_schema">user_shuai</property>
        <property name="show_sql">true</property>
        <!-- <mapping resource="Hibernate/entity/Student.hbm.xml"/>
        <mapping resource="Hibernate/entity/ClassRoom.hbm.xml"/> -->
        <mapping class="Hibernate.entity.ClassRoom"/>
        <mapping class="Hibernate.entity.Student"/>
    </session-factory>
</hibernate-configuration>
/*
生成语句:
Hibernate: select max(id) from user_shuai.T_ClassRoom
Hibernate: select max(id) from user_shuai.T_Student
Hibernate: insert into user_shuai.T_ClassRoom (name, id) values (?, ?)
Hibernate: insert into user_shuai.T_Student (cid, name, id) values (?, ?, ?)
*/
/*数据库*/

这里写图片描述
这里写图片描述

Hibernate多对多(Annotation)

package Hibernate.entity;

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "T_Role")
public class Role {
    private int id;
    private String name;
    private Set<Admin> admins;

    @Id
    @GenericGenerator(name = "myGeneric", strategy = "increment")
    @GeneratedValue(generator = "myGeneric")
    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;
    }

    @ManyToMany(mappedBy = "roles")

    public Set<Admin> getAdmins() {
        return admins;
    }

    public void setAdmins(Set<Admin> admins) {
        this.admins = admins;
    }

}
package Hibernate.entity;

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

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "T_Admin")
public class Admin {
    private int id;
    private String name;
    private Set<Role> roles;

    public Admin() {
        roles = new HashSet<Role>();
    }

    public void add(Role Role) {
        roles.add(Role);
    }

    @Id
    @GenericGenerator(name = "myGeneric", strategy = "increment")
    @GeneratedValue(generator = "myGeneric")
    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;
    }

    @ManyToMany
    @JoinTable(name = "role_admin", joinColumns = { @JoinColumn(columnDefinition = "aid") }, inverseJoinColumns = {
            @JoinColumn(columnDefinition = "rid") })
    public Set<Role> getRoles() {
        return roles;
    }

    public void setRoles(Set<Role> roles) {
        this.roles = roles;
    }

}
package Test;

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

import Hibernate.entity.Admin;
import Hibernate.entity.Role;

public class Test1 {
    public static void main(String[] args) {
        Session session = new Configuration().configure().buildSessionFactory().openSession();
        try {
            session.beginTransaction();


            Role Role=new Role();
            Role.setName("role");
            session.save(Role);

            Admin Admin=new Admin();
            Admin.setName("admin");
            Admin.add(Role);
            session.save(Admin);




            session.getTransaction().commit();
        } catch (Exception e) {
            session.getTransaction().rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }

    }
}
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:SHUAI</property>
        <property name="hibernate.connection.username">user_shuai</property>
        <property name="hibernate.connection.password">0</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.default_schema">user_shuai</property>
        <property name="show_sql">true</property>
        <!-- <mapping resource="Hibernate/entity/Student.hbm.xml"/> <mapping resource="Hibernate/entity/ClassRoom.hbm.xml"/> -->
        <!--<mapping class="Hibernate.entity.ClassRoom" />
        <mapping class="Hibernate.entity.Student" />
        <mapping class="Hibernate.entity.Special" />-->
        <mapping class="Hibernate.entity.Role" />
        <mapping class="Hibernate.entity.Admin" />
    </session-factory>
</hibernate-configuration>
/*生成语句*/
Hibernate: create table user_shuai.role_admin (admins_id number(10,0) not null, roles_id number(10,0) not null, primary key (admins_id, roles_id))
Hibernate: create table user_shuai.T_Admin (id number(10,0) not null, name varchar2(255 char), primary key (id))
Hibernate: create table user_shuai.T_Role (id number(10,0) not null, name varchar2(255 char), primary key (id))
Hibernate: alter table user_shuai.role_admin add constraint FKqx1ysbqwx9p4f83pcs4y37nxa foreign key (roles_id) references user_shuai.T_Role
Hibernate: alter table user_shuai.role_admin add constraint FKk78w0nqu85lixy92vtjyv64xm foreign key (admins_id) references user_shuai.T_Admin
Hibernate: select max(id) from user_shuai.T_Role
Hibernate: select max(id) from user_shuai.T_Admin
Hibernate: insert into user_shuai.T_Role (name, id) values (?, ?)
Hibernate: insert into user_shuai.T_Admin (name, id) values (?, ?)
Hibernate: insert into user_shuai.role_admin (admins_id, roles_id) values (?, ?)
/*数据库*/

这里写图片描述

Hibernate一对多,多对一(Annotation)(练习)

package Hibernate.entity;

import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "T_special")
public class Special {
    private int id;
    private String name;
    private String type;
    private Set<ClassRoom> classRoom;

    @Id
    @GenericGenerator(name = "myGeneric", strategy = "increment")
    @GeneratedValue(generator = "myGeneric")
    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;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    @OneToMany(mappedBy = "special")
    public Set<ClassRoom> getClassRoom() {
        return classRoom;
    }

    public void setClassRoom(Set<ClassRoom> classRoom) {
        this.classRoom = classRoom;
    }

    public Special(String name, String type) {
        this.name = name;
        this.type = type;
    }

    public Special(int id) {
        this.id = id;
    }

}
package Hibernate.entity;

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

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "T_ClassRoom")
public class ClassRoom {
    private int id;
    private String name;
    private int grade;
    private Special special;
    private Set<Student> setlist;

    @Id
    @GenericGenerator(name = "myGeneric", strategy = "increment")
    @GeneratedValue(generator = "myGeneric")
    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;
    }

    public int getGrade() {
        return grade;
    }

    public void setGrade(int grade) {
        this.grade = grade;
    }

    @ManyToOne
    @JoinColumn(name = "special")
    public Special getSpecial() {
        return special;
    }

    public void setSpecial(Special special) {
        this.special = special;
    }

    @OneToMany(mappedBy = "classRoom")
    public Set<Student> getSetlist() {
        return setlist;
    }

    public void setSetlist(Set<Student> setlist) {
        this.setlist = setlist;
    }

    public ClassRoom(String name, int grade, Special special) {
        this.name = name;
        this.grade = grade;
        this.special = special;
    }

    public ClassRoom(int id) {
        this.id = id;
    }

}
package Hibernate.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table(name = "T_Student")
public class Student {
    private int id;
    private String name;
    private String sex;
    private int version;
    private ClassRoom classRoom;

    public int getVersion() {
        return version;
    }

    public void setVersion(int version) {
        this.version = version;
    }

    @Id
    @GenericGenerator(name = "myGeneric", strategy = "increment")
    @GeneratedValue(generator = "myGeneric")
    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;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @ManyToOne
    @JoinColumn(name = "classroom")
    public ClassRoom getClassRoom() {
        return classRoom;
    }

    public void setClassRoom(ClassRoom classRoom) {
        this.classRoom = classRoom;
    }

    public Student(String name, String sex, ClassRoom classRoom) {
        this.name = name;
        this.sex = sex;
        this.classRoom = classRoom;
    }

}
package Test;

import java.util.Random;

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

import Hibernate.entity.ClassRoom;
import Hibernate.entity.Special;
import Hibernate.entity.Student;

public class Test {
    static Random ran = new Random();
    public static void main(String[] args) {
    Session session = new Configuration().configure().buildSessionFactory().openSession();
            try {
                session.beginTransaction();

                session.save(new Special("计算机教育", "教育类"));
                session.save(new Special("计算机应用技术", "高职类"));
                session.save(new Special("计算机网络技术", "高职类"));
                session.save(new Special("计算机信息管理", "高职类"));
                session.save(new Special("数学教育", "教育类"));
                session.save(new Special("物理教育", "教育类"));
                session.save(new Special("化学教育", "教育类"));
                session.save(new Special("会计", "高职类"));
                session.save(new Special("英语教育", "教育类"));

                session.save(new ClassRoom("计算机教育1班", 2009, new Special(1)));
                session.save(new ClassRoom("计算机教育2班", 2009, new Special(1)));
                session.save(new ClassRoom("计算机教育班", 2010, new Special(1)));
                session.save(new ClassRoom("计算机教育班", 2011, new Special(1)));
                session.save(new ClassRoom("计算机应用技术", 2009, new Special(2)));
                session.save(new ClassRoom("计算机应用技术", 2010, new Special(2)));
                session.save(new ClassRoom("计算机应用技术", 2011, new Special(2)));
                session.save(new ClassRoom("计算机网络技术", 2009, new Special(3)));
                session.save(new ClassRoom("计算机网络技术", 2010, new Special(3)));
                session.save(new ClassRoom("计算机网络技术", 2011, new Special(3)));
                session.save(new ClassRoom("计算机信息管理", 2009, new Special(4)));
                session.save(new ClassRoom("计算机信息管理", 2010, new Special(4)));
                session.save(new ClassRoom("计算机信息管理", 2011, new Special(4)));
                session.save(new ClassRoom("数学教育1班", 2009, new Special(5)));
                session.save(new ClassRoom("数学教育2班", 2009, new Special(5)));
                session.save(new ClassRoom("数学教育3班", 2009, new Special(5)));
                session.save(new ClassRoom("数学教育1班", 2010, new Special(5)));
                session.save(new ClassRoom("数学教育2班", 2010, new Special(5)));
                session.save(new ClassRoom("数学教育1班", 2011, new Special(5)));
                session.save(new ClassRoom("数学教育2班", 2011, new Special(5)));
                session.save(new ClassRoom("物理教育", 2009, new Special(6)));
                session.save(new ClassRoom("物理教育", 2010, new Special(6)));
                session.save(new ClassRoom("物理教育", 2011, new Special(6)));
                session.save(new Special("化学教育", "教育类"));
                session.save(new ClassRoom("化学教育", 2009, new Special(7)));
                session.save(new ClassRoom("化学教育", 2010, new Special(7)));
                session.save(new ClassRoom("化学教育", 2011, new Special(7)));
                session.save(new ClassRoom("会计", 2009, new Special(8)));
                session.save(new ClassRoom("会计", 2010, new Special(8)));
                session.save(new ClassRoom("会计", 2011, new Special(8)));
                session.save(new ClassRoom("英语教育A班", 2009, new Special(9)));
                session.save(new ClassRoom("英语教育B班", 2009, new Special(9)));
                session.save(new ClassRoom("英语教育A班", 2010, new Special(9)));
                session.save(new ClassRoom("英语教育B班", 2010, new Special(9)));
                session.save(new ClassRoom("英语教育A班", 2011, new Special(9)));
                session.save(new ClassRoom("英语教育B班", 2011, new Special(9)));
                session.save(new ClassRoom("选修课班A", 2011, null));
                session.save(new ClassRoom("选修课班B", 2011, null));

                String[] sexs = new String[] { "男", "女" };
                // 仅仅添加32个班 的学生,方便做外连接的实验
                for (int i = 1; i <= 32; i++) {
                    // 每个班40个学生
                    for (int j = 1; j <= 40; j++) {
                        session.save(new Student(getName(), sexs[ran.nextInt(2)], new ClassRoom(i)));
                    }
                }

                session.getTransaction().commit();
            } catch (Exception e) {
                session.getTransaction().rollback();
                e.printStackTrace();
            } finally {
                if (session != null)
                    session.close();

            }
    }
    public void testRan() {
            for (int i = 0; i < 20; i++) {
                System.out.println(ran.nextInt(2));
            }
        }

        private static String getName() {
            String[] name1 = new String[] { "孔", "张", "叶", "李", "叶入", "孔令", "张立", "陈", "刘", "牛", "夏侯", "令", "令狐", "赵", "母",
                    "穆", "倪", "张毅", "称", "程", "王", "王志", "刘金", "冬", "吴", "马", "沈" };

            String[] name2 = new String[] { "凡", "课", "颖", "页", "源", "都", "浩", "皓", "西", "东", "北", "南", "冲", "昊", "力", "量",
                    "妮", "敏", "捷", "杰", "坚", "名", "生", "华", "鸣", "蓝", "春", "虎", "刚", "诚" };

            String[] name3 = new String[] { "吞", "明", "敦", "刀", "备", "伟", "唯", "楚", "勇", "诠", "佺", "河", "正", "震", "点", "贝",
                    "侠", "伟", "大", "凡", "琴", "青", "林", "星", "集", "财" };

            boolean two = ran.nextInt(50) >= 45 ? false : true;
            if (two) {
                String n1 = name1[ran.nextInt(name1.length)];
                String n2;
                int n = ran.nextInt(10);
                if (n > 5) {
                    n2 = name2[ran.nextInt(name2.length)];
                } else {
                    n2 = name3[ran.nextInt(name3.length)];
                }
                return n1 + n2;
            } else {
                String n1 = name1[ran.nextInt(name1.length)];
                String n2 = name2[ran.nextInt(name2.length)];
                String n3 = name3[ran.nextInt(name3.length)];
                return n1 + n2 + n3;
            }
        }
}
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@localhost:1521:SHUAI</property>
        <property name="hibernate.connection.username">user_shuai</property>
        <property name="hibernate.connection.password">0</property>
        <property name="hibernate.hbm2ddl.auto">update</property>
        <property name="hibernate.default_schema">user_shuai</property>
        <property name="show_sql">true</property>
        <!-- <mapping resource="Hibernate/entity/Student.hbm.xml"/> <mapping resource="Hibernate/entity/ClassRoom.hbm.xml"/> -->
        <mapping class="Hibernate.entity.ClassRoom" />
        <mapping class="Hibernate.entity.Student" />
        <mapping class="Hibernate.entity.Special" />
        <mapping class="Hibernate.entity.Role" />
        <mapping class="Hibernate.entity.Admin" />
    </session-factory>
</hibernate-configuration>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值