Hibernate配置文件

Hibernate配置文件

通过配置文件(hibernate.properties或hibernate.cfg.xml)和映射文件(.hbm.xml)把java对象或持久化对象(Persistent Object,PO)映射到数据库中的数据表,
然后通过操作PO,对数据表中的数据进行增,删,改,查等操作.

Hibernate配置文件主要用来配置数据库连接参数,例如数据库的驱动程序URL,用户名,密码等。
    两种格式:hibernate.properties和hibernate.cfg.xml。
    一般情况下,hibernate.cfg.xml是Hibernate的默认配置文件。


(1) hibernate.properties:


在Hibernate-3.1的etc目录下有一个hibernate.properties模板。该配置模板文件定义了连接各种数据库所需要的参数。 需要使用hibernate.properties时,修改该模板即可。该模板文件中每一个配置项前面的“#”是注释符号。


(2)hibernate.cfg.xml:

除了要定义Hibernate的各项属性,还要定义程序中用的映射文件(xxx.hbm.xml)。一般情况下是hibernate的默认配置文件。


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


<!-- Generated by MyEclipse Hibernate Tools. -->

<hibernate-configuration>

<session-factory>

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

<property name="connection.url">

jdbc:sqlserver://localhost:1433;databaseName=hbDB

</property>

<property name="dialect">

org.hibernate.dialect.SQLServerDialect

</property>

<property name="myeclipse.connection.profile">hbDB</property>

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

<property name="connection.driver_class">

com.microsoft.sqlserver.jdbc.SQLServerDriver

</property>

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

<property key="hibernate.connection.autocommit">false</property >

<mapping resource="cn/Customers.hbm.xml" />


</session-factory>


</hibernate-configuration>


hibernate.cfg.xml配置文件属性

属性
说明

connection.username
指定连接数据库的用户名

connection.url
指定连接数据库的URL

dialect
用于配置hibernate使用的不同的数据类型。 如Oracle、DB2、MS SQL Server、MySQL等。

myeclipse.connection.profile
数据库的配置文件

connection.password
指定连接数据库的密码

connection.driver_class
指定数据库的驱动程序

show_sql
若为true,表示程序在运行时,在控制台输出SQL语句。




mapping
数据库表和实体的映射信息要在另外的映射文件中定义,但要在配置文件中声明。


实体类

Customers实体类要实现implemetns java.io.Serializable接口。

package cn;


public class Customers implements java.io.Serializable {

private Integer id;

private String name;

private Integer age;

public Integer getAge() {

return age;

}

public void setAge(Integer age) {

this.age = age;

}

public Integer getId() {

return id;

}

public void setId(Integer id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}



}


映射文件

另外,还要告诉Hibernate实体类Customers映射到数据库的哪个表,以及哪个属性对应数据库中的哪个字段。

Customers.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">

<!--

Mapping file autogenerated by MyEclipse Persistence Tools

-->

<hibernate-mapping>

<class name="cn.Customers" table="Customers" schema="dbo" catalog="hbDB">

<id name="id" type="java.lang.Integer">

<column name="ID" />

<generator class="identity" />

</id>

<property name="name" type="java.lang.String">

<column name="Name" length="20" not-null="true" />

</property>

<property name="age" type="java.lang.Integer">

<column name="Age" not-null="true" />

</property>

</class>

</hibernate-mapping>


1 <class name="cn.Customers" table="Customers" schema="dbo" catalog="hbDB"通信dynamic-update="false" dynamic-insert="false" mutable="true"></class>

每一个<class>节点配置一个实体类的映射信息。

(1) name属性:对应指定持久化实体类:Customers。

(2) table属性:对应数据库表的名字。

(3) schema属性:

(4) catalog属性:

(5)dynamic-update:若为false,表示当保存一个实例时,会动态生成update语句,只有该实例中的字段取值变化,才会把它包含到insert语句中。默认值为true。

(6)dynamic-insert:若为false,表示当插入一个实例时,会动态生成inset语句,只有该实例中的字段取值不为null时,才会把它包含到insert语句中。默认值为true。

(7)mutable:若为false,等价于所有的<property>元素的update属性为false,表示整个实例不能被更新。默认为true。

2 <id name="id" type="java.lang.Integer">

<column name="ID" />

<generator class="identity" />

</id>

<id>节点用于定义实体的标识属性(对应数据库表的主键)

(1)name属性:对应实体类的属性。

(2)type属性:指定hibernate映射的数据类型。对应Java数据类型。

(3)column属性:通过name属性指定其对应的数据库表的主键。

(4)generator属性:指定主键的生成策略。


3 <property name="name" type="java.lang.String" update="true" insert="true">

<column name="Name" length="20" not-null="true" />

</property>

与<id>节点类似,但不能包括<generator>节点。

(1)name属性:指定持久化类的属性。

(2)type属性:指定hibernate映射的数据类型。对应Java数据类型。

(3)column属性:通过name属性指定其对应的数据库表的字段名。

(4)length属性:通过name属性指定其对应的数据库表的字段名的长度。

(5)not-null属性:通过name属性指定其对应的数据库表的字段名是否为空。

(6)update:若为false则在更新数据时,不会更新该字段。默认为true.即更新。

(7)insert:若为false则在插入数据时,不会插入该字段。默认为true.即插入。

<column>元素的属性

属 性
说 明

Name
指定持久化类的属性。

length
指定段长度

not-null
指明字段是否为空。默认为false。

unique
指明字段是否具有唯一索引。默认为false。

unique-key
为多个字段设定惟一约束

foreign-key
指明一个外键的名字,它是为关联生成的。

sql-type
设定字段的SQL类型。

check
设定SQL检查约束。


在做唯一外键关联的时候都要用到property-ref="readTypeName"

在many-to-one加个属性,这个属性默认是指向主键的,把它改为主表的关联字段
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值