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

HQL语句

程序员文章站 2022-04-13 21:33:12
...

HQL语句

标签: JAVA框架


1.创建工程Day-HQL

2.在工程下创建lib文件夹

3.导入jar包

antlr-2.7.6.jar
backport-util-concurrent.jar
c3p0-0.9.1.jar
commons-collections-3.1.jar
commons-logging-1.1.1.jar
dom4j-1.6.1.jar
ehcache-1.5.0.jar
hibernate3.jar
javassist-3.9.0GA.jar
jta-1.1.jar
log4j.jar
mysql-connector-java-5.1.10-bin.jar
slf4j-api-1.5.8.jar
slf4j-log4j12.jar

4.在src下创建包com.cd.domain

创建可持续化类

    student.java代码如下
package com.cd.domain;

import java.io.Serializable;
import java.util.Set;

public class Student implements Serializable{
    private int sid;
    private String sname;
    private String sdescription;
    private Set<Course> courses;
    private Classes classes;
    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 getSdescription() {
        return sdescription;
    }
    public void setSdescription(String sdescription) {
        this.sdescription = sdescription;
    }
    public Set<Course> getCourses() {
        return courses;
    }
    public void setCourses(Set<Course> courses) {
        this.courses = courses;
    }
    public Classes getClasses() {
        return classes;
    }
    public void setClasses(Classes classes) {
        this.classes = classes;
    }
}
Course.java
package com.cd.domain;

import java.util.Set;

public class Course {
    private int cid;
    private String cname;
    private String cdescription;
    private Set<Student> students;
    public int getCid() {
        return cid;
    }
    public void setCid(int cid) {
        this.cid = cid;
    }
    public String getCname() {
        return cname;
    }
    public void setCname(String cname) {
        this.cname = cname;
    }
    public String getCdescription() {
        return cdescription;
    }
    public void setCdescription(String cdescription) {
        this.cdescription = cdescription;
    }
    public Set<Student> getStudents() {
        return students;
    }
    public void setStudents(Set<Student> students) {
        this.students = students;
    }

}
Classes.java
package com.cd.domain;

import java.util.Set;

public class Classes {
    private int cid;
    private String cname;
    private String cdescription;
    private Set<Student> students;
    public int getCid() {
        return cid;
    }
    public void setCid(int cid) {
        this.cid = cid;
    }
    public String getCname() {
        return cname;
    }
    public void setCname(String cname) {
        this.cname = cname;
    }
    public String getCdescription() {
        return cdescription;
    }
    public void setCdescription(String cdescription) {
        this.cdescription = cdescription;
    }
    public Set<Student> getStudents() {
        return students;
    }
    public void setStudents(Set<Student> students) {
        this.students = students;
    }

}
创建对应的映射文件
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>
    <class name="com.cd.domain.Student" table="student">
    <id name="sid" column="sid">
    <generator class="increment"></generator>
    </id>
    <property name="sname" length="10"></property>
    <property name="sdescription" length="50"></property>
    <set name="courses" table="student_course"  cascade="all">
    <key>
    <column name="sid"></column>
    </key>
    <many-to-many class="com.cd.domain.Course" column="cid"></many-to-many>
    </set>
    <many-to-one name="classes" class="com.cd.domain.Classes" column="cid"></many-to-one>
    </class>
    </hibernate-mapping>
Course.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.cd.domain.Course" table="course">
    <id name="cid" column="cid">
    <generator class="increment"></generator>
    </id>
    <property name="cname" length="10"></property>
    <property name="cdescription" length="50"></property>
    <set name="students" table="student_course" cascade="all">
    <key>
    <column name="cid"></column>
    </key>
    <many-to-many class="com.cd.domain.Student" column="sid"></many-to-many>
    </set>
    </class>
    </hibernate-mapping>
Classes.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.cd.domain.Classes" table="classes">
    <id name="cid" column="cid">
    <generator class="increment"></generator>
    </id>
    <property name="cname" length="10"></property>
    <property name="cdescription" length="50"></property>
    <set name="students"  inverse="false" lazy="true">
    <key>
    <column name="cid"></column>
    </key>
    <one-to-many class="com.cd.domain.Student" />
    </set>
    </class>
    </hibernate-mapping>

5.在src目录下创建配置文件hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>

<session-factory>
    <property name="connection.username">root</property>
    <property name="connection.password">123456</property>
    <property name="connection.driver_class">
        com.mysql.jdbc.Driver
    </property>
    <property name="connection.url">
        jdbc:mysql://localhost:3306/jdbc02
    </property>
    <property name="dialect">
        org.hibernate.dialect.MySQLDialect
    </property>

    <property name="hbm2ddl.auto">update</property>

    <mapping resource="com/cd/domain/Classes.hbm.xml" />
    <mapping resource="com/cd/domain/Student.hbm.xml" />
    <mapping resource="com/cd/domain/Course.hbm.xml" />
</session-factory>
</hibernate-configuration>

配置表,创建表之前千万不要复制粘贴把二级缓存配置上

6.创建表

创建CreateTabel.java类
package com.cd.db;

import org.hibernate.cfg.Configuration;

public class CreateTable {
    public static void main(String[ ] args){
        Configuration configuration = new Configuration();
        configuration.configure();
        configuration.buildSessionFactory();
    }
}
相关标签: hibernate hql