Jquery中文网 www.jquerycn.cn
Jquery中文网 >  脚本编程  >  java  >  正文 java中Hibernate中的数据库对象详解

java中Hibernate中的数据库对象详解

发布时间:2017-12-12   编辑:www.jquerycn.cn
jquery中文网为您提供java中Hibernate中的数据库对象详解 等资源,欢迎您收藏本站,我们将为您提供最新的java中Hibernate中的数据库对象详解 资源
本文章来给各位同学介绍一下关于 java中Hibernate中的数据库对象详解 ,希望此教程对大家会有所帮助。

1.hibernate.cfg.xml

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6865')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6865>

<!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>
    <!-- 配置数据库的url -->
    <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate</property>
    <!-- 配置数据池的最大容量 -->
    <property name="hibernate.c3p0.max_size">20</property>
    <!-- 配置数据池的最小容量 -->
    <property name="hibernate.c3p0.min_size">1</property>
    <!-- 配置数据链接的超时界限 -->
    <property name="hibernate.c3p0.timeout">5000</property>
    <!-- 在控制台显示后台是否打印执行的sql -->
    <property name="hibernate.show_sql">true</property>
    <!-- 是否以友好的格式显示打印的sql -->
    <property name="hibernate.format_sql">true</property>
    <!-- 打印一些辅助性的注释 -->
    <property name="hibernate.use_sql_comments">true</property>

    <property name="hibernate.c3p0.max_statements">100</property>
    <property name="hibernate.c3p0.idle_test_period">3000</property>
    <property name="hibernate.c3p0.acquire_increment">2</property>
    <property name="hibernate.c3p0.validate">true</property>
    <!-- 配置数据操作的方式 -->
    <property name="hbm2ddl.auto">create</property>
    <!-- 将我们上面 pen 的映射文件添加进来 -->
    <mapping resource="org/Rudiment/hibernate/pen.hbm.xml" />
</session-factory>
</hibernate-configuration>


2.持久化类 pen.java

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1335')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1335>

package org.Rudiment.hibernate;

public class pen {
    private int id;
    private int price;
    private String type;
   
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getPrice() {
        return price;
    }
    public void setPrice(int price) {
        this.price = price;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
}

3. 持久化类的映射文件 pen.cfg.xml

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy8381')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy8381>

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2013-9-8 23:34:47 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="org.Rudiment.hibernate.pen" table="PEN">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="price" type="int">
            <column name="PRICE" />
        </property>
        <property name="type" type="java.lang.String">
            <column name="TYPE" />
        </property>
    </class>
    <!-- 这个database-object 要放在class下面,否则会报错 -->
    <database-object>
        <!-- create中的sql会在drop之后执行 -->
        <create>create table test(t_name varchar(255));</create>
        <!-- 下面的内容会在create之前就执行了 -->
        <drop>drop table test</drop>
        <!-- 下面这两个句指明这个数据库对象对那些数据库生效 -->
        <dialect-scope name="org.hibernate.dialect.MySQLDialect" />
        <dialect-scope name="org.hibernate.dialect.MySQLInnoDBDialect" />
    </database-object>
</hibernate-mapping>


4.操作持久化类的处理类 penHandler.java

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy6362')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy6362>

package org.Rudiment.hibernate;

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

public class penHandler {
    public static void main(String[] args)
    {
        Configuration conf = new Configuration();
        conf.configure();
        ServiceRegistry sr = new ServiceRegistryBuilder().applySettings(conf.getProperties()).buildServiceRegistry();
        SessionFactory sf = conf.buildSessionFactory(sr);
    }
}


注:

通过上面可以看出,penHandler中的main只是执行到了conf.buildSessionFactory(sr);这个时候我们在配置文件中的这部分配置就生效了。

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy4238')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy4238>

<database-object>
    <!-- create中的sql会在drop之后执行 -->
    <create>create table test(t_name varchar(255));</create>
    <!-- 下面的内容会在create之前就执行了 -->
    <drop>drop table test</drop>
    <!-- 下面这两个句指明这个数据库对象对那些数据库生效 -->
    <dialect-scope name="org.hibernate.dialect.MySQLDialect" />
    <dialect-scope name="org.hibernate.dialect.MySQLInnoDBDialect" />
</database-object>

查看数据库的时候,看到表已经成功建立起来了。

<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9834')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9834>

mysql> desc test;
-------- -------------- ------ ----- --------- -------
| Field  | Type         | Null | Key | Default | Extra |
-------- -------------- ------ ----- --------- -------
| t_name | varchar(255) | YES  |     | NULL    |       |
-------- -------------- ------ ----- --------- -------
1 row in set (0.02 sec)

最后还有一点要注明的就是代码要执行到conf.buildSessionFactory(sr);如果条代码从main中给删除掉了,那么hibernate将不会执行数据库对象,即如果要让数据库对象生效,至少需要执行到conf.buildSessionFactory(sr);这一条代码。

您可能感兴趣的文章:
Hibernate下数据批量处理解决方案
Hibernate学习笔记之基本配置详解
java中Hibernate中的数据库对象详解
Java中Hibernate单向(1-N)映射实例详解
使用MiddleGen 产生hibernate的数据库表映射文件
Hibernate save()与persist()区别
Hibernate含List属性的持久化类的CRUD操作范例
Hibernate中FetchMode.JOIN FetchMode.SELECT FetchMode.SUBSELECT介绍
java中Hibernate多对多双向关联的配置
linux中SSH环境搭建详解

[关闭]