hibernate4在maven环境下使用hsqldb

 

1      简介

Hsqldb是一个开放源代码的JAVA数据库,其具有标准的SQL语法和JAVA接口,它可以自由使用和分发,非常简洁和快速的。

HSqlDB非常适合在用于快速的测试和演示的Java程序中。

http://hsqldb.org/

2      配置mavenpom.xml文件

                   <dependency>

                            <groupId>org.hibernate</groupId>

                            <artifactId>hibernate-search-orm</artifactId>

                            <version>4.4.2.Final</version>

                   </dependency>

                   <dependency>

                            <groupId>org.hsqldb</groupId>

                            <artifactId>hsqldb</artifactId>

                            <version>2.3.0</version>

                   </dependency>

 

3      启动数据库的mvn命令

Mvn exec:java  -Dexec.mainClass="org.hsqldb.Server" -Dexec.args="-database.0  file:target/data/tutorial"

 

4      启动数据库管理控制台的mvn命令

Mvn exec:java  -Dexec.mainClass="org.hsqldb.util.DatabaseManager"

 

5      Hibernate配置文件

<?xml version='1.0'  encoding='utf-8'?>

<!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>

                   <!--  Database connection settings -->

                   <property  name="connection.driver_class">org.hsqldb.jdbcDriver</property>

                   <property  name="connection.url">jdbc:hsqldb:hsql://localhost</property>

                   <property  name="connection.username">sa</property>

                   <property  name="connection.password"></property>

 

                  <!-- JDBC connection pool (use the  built-in) -->

                   <property  name="connection.pool_size">1</property>

 

                   <!--  SQL dialect -->

                   <property  name="dialect">org.hibernate.dialect.HSQLDialect</property>

 

                   <!--  Enable Hibernate's automatic session context management -->

                   <property  name="current_session_context_class">thread</property>

 

                   <!--  Disable the second-level cache -->

                   <property  name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

 

                   <!--  Echo all executed SQL to stdout -->

                   <property  name="show_sql">true</property>

 

                   <!--  Drop and re-create the database schema on startup -->

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

 

                   <mapping  class="fire.entity.Author" />

                   <mapping  class="fire.entity.Book" />

         </session-factory>

</hibernate-configuration>

 

6      测试下是否可以使用

hibernate工具类:

import org.hibernate.SessionFactory;

import org.hibernate.cfg.Configuration;

 

public class HibernateUtil {

 

     private static final SessionFactory sessionFactory =  buildSessionFactory();

 

     private static SessionFactory buildSessionFactory() {

         try {

            // Create the SessionFactory from  hibernate.cfg.xml

            return new  Configuration().configure().buildSessionFactory();

         }

         catch (Throwable ex) {

            // Make sure you log the  exception, as it might be swallowed

            System.err.println("Initial  SessionFactory creation failed." + ex);

            throw new  ExceptionInInitializerError(ex);

         }

     }

 

     public static SessionFactory getSessionFactory() {

         return sessionFactory;

     }

 

}

 

Hibernate测试方法(java bean自己创建吧):

public static void main(String[] args) {

 

        Session session =  HibernateUtil.getSessionFactory().getCurrentSession();

                  

                   session.beginTransaction();

//               SchemaExport  schemaExport = new SchemaExport(configuration);

//               schemaExport.create(true,  true);

//               Author  author = new Author();

//               author.setName("是的");

//               session.save(author);

                   Query  query =  session.createQuery("from  Author");

                   List<Author>  list = query.list();

                   System.out.println(list.size());

                  

                   session.getTransaction().commit();

                  

         }

 

 

转载于:https://my.oschina.net/kingfire/blog/186517

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值