欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

创建持久化和映射文件并配置单向一对多关系

程序员文章站 2022-07-12 18:09:32
...
 Grade
package com.imooc.entity;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
public class Grade implements Serializable {
    private int gid;
    private String gname;
    private String gdesc;
    private Set<Student> students =new HashSet<Student>();
    public int getGid() {
        return gid;
    }
    public void setGid(int gid) {
        this.gid = gid;
    }
    public String getGname() {
        return gname;
    }
    public void setGname(String gname) {
        this.gname = gname;
    }
    public String getGdesc() {
        return gdesc;
    }
    public void setGdesc(String gdesc) {
        this.gdesc = gdesc;
    }
    public Grade() {
        super();
    }
    public Set<Student> getStudents() {
        return students;
    }
    public void setStudents(Set<Student> students) {
        this.students = students;
    }
    public Grade(int gid, String gname, String gdesc) {
        super();
        this.gid = gid;
        this.gname = gname;
        this.gdesc = gdesc;
    }
    public Grade(String gname, String gdesc) {
        super();
        this.gname = gname;
        this.gdesc = gdesc;
    }
}
 
 
二 Student
package com.imooc.entity;
 
import java.io.Serializable;
 
public class Student implements Serializable {
        private int sid;
        private String sname;
        private String sex;
 
        public int getSid() {
                return sid;
        }
 
        public void setSid(int sid) {
                this.sid = sid;
        }
 
        public String getSname() {
                return sname;
        }
 
        public void setSname(String sname) {
                this.sname = sname;
        }
 
        public String getSex() {
                return sex;
        }
 
        public void setSex(String sex) {
                this.sex = sex;
        }
 
        public Student() {
                super();
        }
 
        public Student(String sname, String sex) {
                super();
                this.sname = sname;
                this.sex = sex;
        }
 
}
 
三 Grade.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<hibernate-mapping>
        <class name="com.imooc.entity.Grade" table="grade">
                <id name="gid" column="gid" type="java.lang.Integer">
                        <generator class="increment"></generator>
                </id>
                <property name="gname" type="java.lang.String">
                        <column name="gname" length="20" not-null="true"></column>
                </property>
                <property name="gdesc">
                        <column name="gdesc"></column>
                </property>
                <!-- 配置一对多关联关系 -->
                <set name="students" table="student">
                        <key column="gid"></key>
                        <one-to-many class="com.imooc.entity.Student"/>
                </set>
        </class>
</hibernate-mapping>
 
四 Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
<hibernate-mapping>
        <class name="com.imooc.entity.Student" table="student">
                <id name="sid" column="sid" type="java.lang.Integer">
                        <generator class="increment"></generator>
                </id>
                <property name="sname" type="java.lang.String">
                        <column name="sname" length="20" not-null="true"></column>
                </property>
                <property name="sex">
                        <column name="sex"></column>
                </property>
        </class>
</hibernate-mapping>
 
五 hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 
<hibernate-configuration>
    <session-factory>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.url">
                <![CDATA[
                        jdbc:mysql://localhost:3306/imooc?useUnicode=true&characterEncoding=utf8
                ]]>
        </property>
        <property name="show_sql">true</property>
        <property name="hbm2ddl.auto">update</property>
   
        <!-- 指定映射文件的路径 -->
        <mapping resource="com/imooc/entity/Grade.hbm.xml"/>
        <mapping resource="com/imooc/entity/Student.hbm.xml"/>
    </session-factory>
</hibernate-configuration>
 
六 set元素的常用属性

创建持久化和映射文件并配置单向一对多关系
            
    
    博客分类: Hibernate Hibernate 
 
  • 创建持久化和映射文件并配置单向一对多关系
            
    
    博客分类: Hibernate Hibernate 
  • 大小: 21.7 KB
  • 创建持久化和映射文件并配置单向一对多关系
            
    
    博客分类: Hibernate Hibernate 
  • 大小: 18.7 KB
  • 创建持久化和映射文件并配置单向一对多关系
            
    
    博客分类: Hibernate Hibernate 
  • 大小: 17.5 KB
  • 创建持久化和映射文件并配置单向一对多关系
            
    
    博客分类: Hibernate Hibernate 
  • 大小: 152 KB
相关标签: Hibernate