您的位置:首页 > 其它

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

2017-11-22 12:45 246 查看
一 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"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd";>
<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"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd";>
<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"
        "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd";>
 
<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元素的常用属性



 





大小: 21.7 KB





大小: 18.7 KB





大小: 17.5 KB





大小: 152 KB

查看图片附件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: