Hibernate的数据查找,添加!

1.首先看一下测试数据库的物理模型

2.测试所需要的Hibernate的jar包

3.数据库的sql

/*==============================================================*/
/* DBMS name:      MySQL 5.0                                    */
/* Created on:     2015/7/3 23:17:57                            */
/*==============================================================*/

drop table if exists books;

drop table if exists publishers;

/*==============================================================*/
/* Table: books                                                 */
/*==============================================================*/
create table books
(
   bookId               int not null auto_increment,
   publisherId          int,
   title                varchar(20),
   author               varchar(20),
   primary key (bookId)
);

/*==============================================================*/
/* Table: publishers                                            */
/*==============================================================*/
create table publishers
(
   publisherId          int not null auto_increment,
   publishName          varchar(20),
   primary key (publisherId)
);

alter table books add constraint FK_publisherToBook foreign key (publisherId)
      references publishers (publisherId) on delete restrict on update restrict;

4.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节点就够了,除非我们中间使用了多个数据库-->
     <session-factory>
         <!--用户名 -->
         <property name="connection.username">root</property>
         <!--url信息 -->
         <property name="connection.url">jdbc:mysql://localhost:3306/bookstore</property>
         <!--数据库方言信息-->
         <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
         <!--密码 -->
         <property name="connection.password">hjzgg5211314</property>
         <!--数据库驱动信息 -->
         <property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
         <!--指定Hibernate映射文件路径 --> 

         <!-- 字符集乱码问题   -->
         <property name="connection.useUnicode">true</property>
         <property name="connection.characterEncoding">UTF-8</property> 

          <!-- 控制台输出sql语句  -->
          <property name="show_sql">true</property>
          <property name="format_sql">true</property> 

         <mapping resource="com/entry/books.hbm.xml" />
         <mapping resource="com/entry/publishers.hbm.xml" />
     </session-factory>
</hibernate-configuration> 

5.测试的servlet

package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dao.MyHibernateDao;
import com.entry.Books;
import com.entry.Publishers;

/**
 * Servlet implementation class HibernateServlet
 */
@WebServlet("/HibernateServlet")
public class HibernateServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public HibernateServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    private void process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        MyHibernateDao dao = new MyHibernateDao();
        List<Books> list = dao.getAllBooks();
        PrintWriter out = response.getWriter();
        for(Books book : list){
            out.print("书名:" + book.getTitle() + "---作者:" + book.getAuthor() + "---出版社:" + book.getPublishers().getPublishName());
            out.print("<br>");
        }

        out.print("<br>");
        out.print("<br>");
        out.print("<br>");
        out.print("<br>");
        List<Publishers> tlist = dao.getAllPublishers();
        for(Publishers publishers : tlist){
            out.print("出版社: " + publishers.getPublishName());
            out.print("<br>");
            Set<Books> bookSet = publishers.getBooks();
            for(Books books : bookSet){
                out.print("&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp");
                out.print(books.getTitle() + " " + books.getAuthor() + "<br>");
            }
            out.print("<br>");
        }
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        process(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        process(request, response);
    }

}

6.简单的Dao层

package com.dao;

import java.util.List;

import org.hibernate.Session;

import com.entry.Books;
import com.entry.Publishers;
import com.hibtest1.HibernateSessionFactory;

public class MyHibernateDao {
    public List<Books> getAllBooks(){
        Session session = HibernateSessionFactory.getSession();
        List<Books> list = null;
        try{
            String hql = "from Books";
            list = session.createQuery(hql).list();
        } catch (Exception e){
            System.out.println("getAllBooks---errors");
            e.printStackTrace();
        }
        return list;
    }

    public List<Publishers> getAllPublishers(){
        Session session = HibernateSessionFactory.getSession();
        List<Publishers> list = null;
        try{
            String hql = "from Publishers";
            list = session.createQuery(hql).list();
        } catch (Exception e){
            System.out.println("getAllPublishers---errors");
            e.printStackTrace();
        }
        return list;
    }
}

7.实体Publishers和它的映射文件xml

package com.entry;

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

public class Publishers implements Serializable{
    private Integer publisherId;
    private String publishName;
    public Integer getPublisherId() {
        return publisherId;
    }
    public void setPublisherId(Integer publisherId) {
        this.publisherId = publisherId;
    }
    public String getPublishName() {
        return publishName;
    }
    public void setPublishName(String publishName) {
        this.publishName = publishName;
    }

    private Set<Books> books;
    public Set<Books> getBooks() {
        return books;
    }
    public void setBooks(Set<Books> books) {
        this.books = books;
    }

}
<?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="com.entry">
        <class name="Publishers" table="publishers">
            <!-- 定义持久化类的表示属性 -->
            <id name="publisherId" column="publisherId" type="java.lang.Integer">
                <!-- 定义主键生成策略 -->
                <generator class="identity"/>
            </id>
            <property name="publishName" column="publishName" type="java.lang.String"/>

            <!--
                反过来看,出版社和书籍是一对多的关系,这也就意味着每个Publishers对象会引用一组Books对象。
             -->
             <set name="books" lazy="false">
                 <key column="publisherId"/>
                 <one-to-many class="com.entry.Books"/>
             </set>
        </class>
</hibernate-mapping>

8.实体Books和它的映射文件xml

package com.entry;

import java.io.Serializable;

public class Books implements Serializable{
    private Integer bookId;
    private String title;
    private String author;
    private Publishers publishers;
    public Integer getBookId() {
        return bookId;
    }
    public void setBookId(Integer bookId) {
        this.bookId = bookId;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getAuthor() {
        return author;
    }
    public void setAuthor(String author) {
        this.author = author;
    }
    public Publishers getPublishers() {
        return publishers;
    }
    public void setPublishers(Publishers publishers) {
        this.publishers = publishers;
    }

}
<?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="com.entry">
        <class name="Books" table="books">
            <!-- 定义持久化类的表示属性 -->
            <id name="bookId" column="bookId" type="java.lang.Integer">
                <!-- 定义主键生成策略 -->
                <generator class="identity"/>
            </id>
            <property name="title" column="title" type="java.lang.String"/>
            <property name="author" column="author" type="java.lang.String"/>

            <many-to-one name="publishers" column="publisherId" class="com.entry.Publishers" lazy="false"/>
            <!--
                默认lazy="true", 这样Books对象加载之后就不会立即加载关联的Publishers对象,就会发生异常
             -->
        </class>
</hibernate-mapping>

9. HibernateSessionFactory代码

package com.hibtest1;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;

@SuppressWarnings("deprecation")
public class HibernateSessionFactory {

    /**
     * Location of hibernate.cfg.xml file.
     * Location should be on the classpath as Hibernate uses
     * #resourceAsStream style lookup for its configuration file.
     * The default classpath location of the hibernate config file is
     * in the default package. Use #setConfigFile() to update
     * the location of the configuration file for the current session.
     */

    private static String CONFIG_FIEL_LOACTION = "/hibernate.cfg.xml";
    private static String configFile = CONFIG_FIEL_LOACTION;
    private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
    private static org.hibernate.SessionFactory sessionFactory;
    private static Configuration configuration = new Configuration();
    private static ServiceRegistry serviceRegistry;
    /**
        在回话工厂类HibernateSessionFactory中, 首先通过一次静态代码块来启动Hibernate,该代码块只在HibernateSessionFactory类被加载时执行一次,
        用于建立sessionFactory。即SessionFactory是线程安全的,只能被实例化一次。
        在静态代码块中通过创建Configuration对象并调用其configure()方法读取Hibernate配置文件hibernate.cfg.xml信息,从而进行配置信息的管理。然后
        创建SessionFactory实例,在Hibernate4.0版本之前,该实例创建工作由Configuration对象的buildSessionFactory()方法来完成。而Hibernater4.0
        版本之后,创建SessionFactory实例的方法有所改变,Hibernate 增加了一个注册ServiceRegistryBuilder类。要生成一个注册机对象,然后所有的生成
        SessionFactory的对象向注册机注册一下再使用。生成方法还config.buildSessionFactory()方法,只不过加了一个注册机的参数。
    */
    static {
        try {
            configuration.configure(configFile);
            serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
            sessionFactory = configuration.buildSessionFactory(serviceRegistry);
        } catch (Exception e) {
            System.err.println("%%%% Error Creating SessionFactory %%%%" + e);
            e.printStackTrace();
        }
    }

    private HibernateSessionFactory() {
    }

    /**
     * Returns the ThreadLocal Session instance.  Lazy initialize
     * the <code>SessionFactory</code> if needed.
     *
     * 由于sessionFactory是线程安全的,因而同一个SessionFactory实例可以被多个线程共享,即多个并发线程可以同时访问一个SesisonFactory并
     * 获得Session的实例。但由于Session不是线程安全的,如果多个并发线程同时操作同一个Session对象,就可能出现一个线程在进行数据库操作,而另一个
     * 线程将Session对象关闭的情况,从而出现异常。如何才能保证线程安全呢?这就要求SessionFactory能够针对不同的线程创建不同的Session对象,即
     * 需要对Session进行有效的管理,Hibernate中使用ThreadLocal对象来维护和管理Session实例。
     *
     * ThreadLocal是线程局部变量,线程局部变量高效的为每一个使用它的线程提供单独的线程局部变量的副本。为了实现为每个线程维护一个变量的副本,ThreadLocal
     * 类提供了一个Map结构,是key值用来保存线程的ID, value用来保存一个Session实例的副本。这样多线程并发操作时,实在与自己绑定的Session实例副本上进行
     * 的,从而避免多个线程横在同一个Session实例上操作是可能导致的数据异常。
     *
     * 在HibernaterSessionFctory类的getSession()方法中,首先调用ThreadLocal类的get()方法获得当前线程的Session对象,然后判断当前线程是否已存在
     * Session对象或者对象未打开,在判断SessionFactory对象是否为空,如果SeesionFctory对象不存在,先调用rebuildSessionFactory()方法创建SesionFactory,
     * 如果SessionFactory对象已经存在,则调用SessionFactory对象的openSession()方法创建Session对象。创建完Session对象后,还需要调用ThreadLocal的set()
     * 方法为该线程保存Session对象。
     *
     *  @return Session
     *  @throws HibernateException
     */
    public static Session getSession() {
        Session session = (Session) threadLocal.get();
        try{
            if (session == null || !session.isOpen()) {
                if (sessionFactory == null) {
                    rebuildSessionFactory();
                }
                session = (sessionFactory != null) ? sessionFactory.openSession()
                        : null;
                threadLocal.set(session);
            }
        } catch (HibernateException e){
            System.err.println("%%%% Error Creating getSession %%%%" + e);
            e.printStackTrace();
        }
        return session;
    }

    /**
     *  Rebuild hibernate session factory
     *
     */
    public static void rebuildSessionFactory() {
        synchronized(sessionFactory){
            try {
                configuration.configure(configFile);
                serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
                sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            } catch (Exception e) {
                System.err.println("%%%% Error rebuildSessionFactory %%%%" + e);
                e.printStackTrace();
            }
        }
    }

    /**
     *  Close the single hibernate session instance.
     *
     *  @throws HibernateException
     */
    public static void closeSession(){
        Session session = (Session) threadLocal.get();
        threadLocal.set(null);
        try{
            if (session != null && session.isOpen()) {
                session.close();
            }
        } catch(HibernateException e){
            System.err.println("%%%% Error closeSession %%%%" + e);
            e.printStackTrace();
        }
    }

    /**
     *  return session factory
     *
     */
    public static org.hibernate.SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    /**
     *  return hibernate configuration
     *
     */
    public static Configuration getConfiguration() {
        return configuration;
    }

    public static void setConfigFile(String configFile){
        HibernateSessionFactory.configFile = configFile;
    }

}

一.上述代码可以简单的完成数据的查找

二.Hibernate添加数据

方法1.主控方为“一”的一方添加数据,默认, 注意“多”的一方的外键publisherId是允许空值的情况

在MyHibernateDao.java中添加如下的方法,用来进行级联数据的添加

//添加数据
    public void save(Publishers publisher){
        Session session = HibernateSessionFactory.getSession();
        Transaction tran = session.beginTransaction();
        session.save(publisher);
        tran.commit();
        session.close();
    }

在HibernateServlet.java中添加如下代码,进行数据添加

//添加数据
        Books book1 = new Books();
        book1.setAuthor("李xx");
        book1.setTitle("数学");
        Books book2 = new Books();
        book2.setAuthor("hxx");
        book2.setTitle("语文");

        Publishers publisher = new Publishers();
        publisher.setPublishName("hjzgg");
        publisher.getBooks().add(book1);
        publisher.getBooks().add(book2);
        dao.save(publisher);

        PrintWriter out = response.getWriter();
        List<Books> list = dao.getAllBooks();
        out.print(list.size() + "<br>");
        for(Books book : list){
            out.print("书名:" + book.getTitle() + "---作者:" + book.getAuthor() + "---出版社:" + book.getPublishers().getPublishName());
            out.print("<br>");
        }

        out.print("<br>");
        out.print("<br>");
        out.print("<br>");
        out.print("<br>");

publishers.hbm.xml代码更改如下:

 <set name="books" lazy="false" cascade="save-update">
                 <key column="publisherId"/>
                 <one-to-many class="com.entry.Books"/>
             </set>

注意如果 “多”的一方的外键publisherId是不允许空值的情况,会出现如下异常, 那么更改的地方就多了一些

%%%% Error Creating SessionFactory %%%%org.hibernate.MappingException: Repeated column in mapping for entity: com.entry.Books column: publisherId (should be mapped with insert="false" update="false")
org.hibernate.MappingException: Repeated column in mapping for entity: com.entry.Books column: publisherId (should be mapped with insert="false" update="false")

更改如下:

books.hbm.xml文件:

<many-to-one name="publishers" column="publisherId" class="com.entry.Publishers" lazy="false"
                    insert="false" update="false"/>

publishers.hbm.xml文件:

<set name="books" lazy="false" cascade="save-update">
                 <key column="publisherId" not-null="true"/>
                 <one-to-many class="com.entry.Books"/>
             </set>

方法2.主控方为“多”的一方添加数据,利用invers属性,将控制权进行反转,这样添加数据相对来说更为高效, 代码更改如下:

首先在HibernateServlet.java中添加如下代码,进行数据添加

//添加数据, 主控方为“多”
        Books book1 = new Books();
        book1.setAuthor("李xx");
        book1.setTitle("数学");
        Books book2 = new Books();
        book2.setAuthor("hxx");
        book2.setTitle("语文");

        Publishers publisher = new Publishers();
        publisher.setPublishName("hjzgg");
        book1.setPublishers(publisher);
        book2.setPublishers(publisher);
        dao.saveBooks(book1);
        dao.saveBooks(book2);

然后修改books.hbm.xml文件:

<many-to-one name="publishers" column="publisherId" class="com.entry.Publishers" lazy="false"
                    cascade="save-update"/>

接着修改publishers.hbm.xml文件:

 <set name="books" lazy="false" cascade="save-update" inverse="true">
                 <key column="publisherId" not-null="true"/>
                 <one-to-many class="com.entry.Books"/>
             </set>

最后在MyHibernateDao.java中添加如下代码:

//添加数据, 主控方为“多”
    public void saveBooks(Books book){
        Session session = HibernateSessionFactory.getSession();
        Transaction tran = session.beginTransaction();
        session.save(book);
        tran.commit();
        session.close();
    }
时间: 2025-01-02 03:11:28

Hibernate的数据查找,添加!的相关文章

解决hibernate保存数据到mysql中出现乱码问题

今天使用hibernate保存数据到mysql中,发现出现乱码问题,经过检查,发现接收到的是正确中文,说明客户端浏览器将中文编码发送到服务器过程中无乱码问题,后来查找资料: 首先要告诉数据库要插入的字符串的字符集,mysql 默认使用的字符集是 latin1.我要保存的字符串是 UTF-8 编码的(字符集是 Unicode),所以包含这个字段的表应该使用 UTF-8 编码. 这里有几种解决办法. 1.在建立数据库的时候指定数据库的字符集编码,这样,这个数据库的所有表都会默认使用数据库的字符集编码

。。。Hibernate 查询数据 事务管理。。。

在Hibernate中,查询数据的时候,可能会需要事务的管理,为什么呢?因为在查询数据库的时候,Hibernate将数据从数据库里面查询出来之后,会先把数据放入Hibernate的session缓存里,下次你再次发送同一个请求的时候,Hibernate会先从缓存里面查数据,如果发现有这样的数据,OK,那就直接把这个数据拿过去显示,如果没有的话,才会到数据库里面查询显示.这个就是说,在页面显示的数据是A,到后台把数据改为B,现在前台刷新,则还会显示数据A,不会显示数据B的原因!

向后端请求数据 以及像后端发送数据要求(删除数据和添加数据)

删除数据和添加数据只能后端操作 删除数据和添加数据都要用到的html(一)部分 <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="../css/bootstrap.min.css"

linux学习笔记——数据查找、数据截取

##########数据查找########## find             ##find 命令在系统中查找文件    -name        ##按照文件名查找    -user        ##按照文件属主查找    -group        ##按文件属组查找     -perm        ##按文件权限查找    -type        ##按文件类型查找            ##文件类型:b块设备c字符设备d目录f普通文件l链接s(socket)    -size

数据查找之80-20原则的JavaScript代码实现

作为前端开发人员,无论在工作还是找工作(笔试/面试),或多或少会涉及一些数据结构的知识. 数据结构即计算机存储和组织数据的方式. 常用的结构:数组.栈.队列.链表.树.图.堆和散列表 关于数据,我们常会用到排序.查找(检索)等操作.常用的排序有冒泡排序.选择排序.插入排序.希尔排序.归并排序和快速排序.常用的查找方式有:顺序查找和二分查找.其中顺序查找中,80/20规则由利于数据的组织与管理,提高数据查找效率. 80/20原则的前提是待查找数据的存储位置在数组的后80%里,其驱动力是数据的查找次

UITableView数据的添加、删除、移动

数据的添加.删除4个步骤: 1.现在导航视图上面添加一个按钮,可以是系统自带的编辑按钮editButtonItem 2.实现方法让将要执行删除添加操作的表视图处于编辑状态 3.指定表视图中哪些行可以处于编辑状态,默认所有行都可以进行编辑 4.指定编辑样式,到底是删除还是添加,此方法如果不重写的话,默认是删除样式 5.不管是删除还是添加,这个方法才是操作的核心方法,当点击删除/添加按钮时,需要做什么事情,怎么样才能完成删除或者添加操作 步骤:5.1表视图开始更新 5.2根据不同的编辑样式进行写代码

ThinkPHP完成对数据的添加以及表单数据的收集

数据添加分两种方式,一种是数组的方式,另一种是使用AR的方式添加,其实这两种方式添加的方式是相同的,就是一个规范的不同而已,所谓AR,就是: //一个数据模型代表一张表. //一个对象代表一条数据. //一个字段代表一个对象的属性 这样也符合传统的思维习惯,下面是数据模拟添加的实例代码: $temp=D('User'); // $arr=array('id'=>11,'name'=>'add_1_xuning','password'=>md5('123456')); //下面是通过AR的

C#数据层添加事务功能

例子 public bool Save(ProjectModel project) { int Result = 0; SqlConnection Conn = new SqlConnection(SqlHelper.connectionString); Conn.Open(); SqlTransaction Trans; Trans = Conn.BeginTransaction(); try { SqlParameter[] sqlParameter1 = new SqlParameter[

easyui datagrid 每条数据后添加操作按钮

easyui datagrid 每条数据后添加“编辑.查看.删除”按钮 1.给datagrid添加操作字段:字段值 <table class="easyui-datagrid" data-options="singleSelect:true,collapsible:true,url:'publish.json',method:'get',fit:true" style="width: 700px; height: 250px"> &l