搭建环境:
Eclipse3.4
Build Path Configeration:
新建
hibernate
库,并加进外部类包:
基本的有
hibernate3
以及
hibernate-distribution-3.3.2.GA\lib\required
目录下的全部类包
还有
hibernate-annotations
,
hibernate-commons-annotations
,
ejb3-persistence
日志系统的:
slf4j-nop-1.5.11
和
slf4j-api-1.5.11
测试系统的:
junit-4.8.2
Hibernate
可以通过
XML
解析或
annotations
标注对实体对象进行映射,实现用面向对象的方式操作数据库。
Hibernate
基本配置:
Hibernate.cfg.xml
文件:(注意该文件最好不要私自改名,且要放在项目的根目录下)
最主要的声明部分:
<
hibernate-configuration
>
<
session-factory
>
<!-- Database connection settings -->
<
property
name
=
"connection.driver_class"
>
com.microsoft.sqlserver.jdbc.SQLServerDriver
</
property
>
<
property
name
=
"connection.url"
>
jdbc:sqlserver://localhost
:1433;databaseName=Test
</
property
>
<
property
name
=
"connection.username"
>
username
</
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.SQLServerDialect
</
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.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
>
<!—-format the sql-->
<property name="format_sql">true</property>
<!--
实体映射对象
-->
<
mapping
resource
=
"com/Test/Demo.hbm.xml"
/>
<
mapping
class
=
"com.Test.Demo"
/>
<!--
以上声明为
XML
映射方式,下面为
annotations
映射方式的声明
-->
<
mapping
class
=
"com.Test.T"
/>
</
session-factory
>
</
hibernate-configuration
>
XML
解析方式:
对象映射文件
Demo.hbm.xml
,命名规则一般把被映射的类的类名作为最顶级的名再加上后缀
.hbm.xml
,
并与被映射的类放在同一个包里
。
<
hibernate-mapping
>
<
class
name
=
"com.Test.Demo"
table
=
"[dbo].[T]"
>
<
id
name
=
"id"
column
=
"id"
type
=
"java.lang.Integer"
>
<
generator
class
=
"native"
/>
</
id
>
<
property
name
=
"name"
column
=
"name"
/>
<
property
name
=
"no"
column
=
"no"
/>
</
class
>
</
hibernate-mapping
>
/*
被映射的对象类代码与类
T
一样,见下
*/
Annotations
注析方式:
package com.Test;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="_T")
/*@Table
要选
javax
那个,如果
_T
不存在,则自动生成
*/
public class T {
private int id;
@Id
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Column(name="_name")
/*
指定表的列明,如果表中的列明不是
_name,
则跟新
*/
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
private String Name;
private int no;
}
/*
以上为被映射的对象类,之后还要在
hibernate
文件中添加映射声明,详细见上
*/
Junit
测试
使用
Junit
进行对象映射的测试,同时验证数据库里的数据
package
com.TestHibernate;
//
测试包命名规则,一般为
Test+
被测试的项目名
import
org.hibernate.Session;
import
org.hibernate.SessionFactory;
import
org.hibernate.cfg.AnnotationConfiguration;
import
org.junit.After;
import
org.junit.Before;
import
org.junit.Test;
import
static
org.junit.Assert.*;
import
static
org.hamcrest.CoreMatchers.*;
//assertThat
和
is
断言要用到的外部类包
import
com.Test.T;
import
junit.framework.TestCase;
public
class
TestT
extends
TestCase {
Session
session
;
SessionFactory
sf
;
T
t
;
@Before
public
void
setUp()
throws
Exception {
AnnotationConfiguration cfg =
new
AnnotationConfiguration();
sf
= cfg.configure().buildSessionFactory();
session
=
sf
.openSession();
session
.beginTransaction();
t
=
new
T();
/*
单元测试进行前的程序环境初始化
*/
}
@After
public
void
tearDown()
throws
Exception {
session
.close();
sf
.close();
/*
测试完成后,关闭所占的资源
*/
}
@Test
public
void
testT() {
int
x= 3;
String y =
"x"
;
int
z = 11;
t
.setId(x);
t
.setName(y);
t
.setNo(z);
session
.save(
t
);
session
.getTransaction().commit();
/*
结果测试
*/
assertThat
(x,is
(
t
.getId()));
assertThat
(y,is
(
t
.getName()));
assertThat
(z,is
(
t
.getNo()));
}
}
分享到:
相关推荐
hibernate-commons-annotations-4.0.1.Final.jar hibernate-core-4.1.12.Final.jar hibernate-ehcache-4.1.12.Final.jar hibernate-entitymanager-4.1.12.Final.jar hibernate-jpa-2.0-api-1.0.1.Final.jar ...
赠送jar包:hibernate-jpa-2.1-api-1.0.2.Final.jar; 赠送原API文档:hibernate-jpa-2.1-api-1.0.2.Final-javadoc.jar; 赠送源代码:hibernate-jpa-2.1-api-1.0.2.Final-sources.jar; 赠送Maven依赖信息文件:...
赠送jar包:hibernate-jpa-2.1-api-1.0.2.Final.jar; 赠送原API文档:hibernate-jpa-2.1-api-1.0.2.Final-javadoc.jar; 赠送源代码:hibernate-jpa-2.1-api-1.0.2.Final-sources.jar; 赠送Maven依赖信息文件:...
hibernate-jpa-2.1-api-1.0.0.final-sources.jar 源码 hibernate-jpa-2.1-api-1.0.0.final-sources.jar 源码
hibernatetools-Update-4.1.1.zip
Eclipse中Hibernate插件。
Eclipse用于Hibernate框架开发插件,全CSDN最低资源分下载,让你吃上真正的石灰。。。
该整合包含hibernatetools-Update-4.1.2.Final_2014-03-18_15-46-19-B706、hibernate-release-5.4.1.Final、sqljdbc_6.4.0.0_chs.tar
hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-jpa-2.1-api-1.0.0.final.jar.zip,内含hibernate-jpa-2.1-api-1.0.0.final.jar
hibernate-commons-annotations-5.0.1.Final.jar的源码
Hibernate稳定版(hibernate-release-5.3.23.Final.zip),Hibernate ORM 是一个为应用程序、库和框架提供对象/关系映射 (ORM) 支持的库。它还提供了 JPA 规范的实现,这是 ORM 的标准 Java 规范。
hibernate-jpa-2.1-api-1.0.0.Final.jar官方下载,请放心使用
搭建hibernate环境
hibernate many-to-many级联保存,级联更新,级联删除
hibernate-jpa-2.0-api-1.0.1.Final-sources.jar hibernate jpa 源代码
hibernate-release-5.0.7.Final压缩包 -document -lib -project 内部Hibernate依赖库: antlr-2.7.7.jar dom4j-1.6.1.jar geronimo-jta_1.1_spec-1.1.1.jar hibernate-commons-annotations-5.0.1.Final.jar ...
java.lang.NoClassDefFoundError: javax/persistence/spi/ProviderUtil 或javax/persistence/entitylistener问题
hibernate one-to-one 一对一唯一外键关联映射_单向 and 双向
很多人为了配置jpa找这个动态产生字节码的jar文件,hibernate-distribution-3.3.1.GA包太大,而hibernate-distribution-3.3.2.GA的jar没有这个jar文件,希望对大家有用