- 浏览: 715166 次
- 性别:
- 来自: 上海
最新评论
-
TheUniqueGirl:
Tomcat系统架构与设计模式:http://www.doci ...
Tomcat -
aykjy:
...
UML常见工具之NetBeans(downmoon) -
不老肖邦:
谢谢提供的简单示例
JAVA toString()
hibernate3.6中,要使用annotations,请引入 hibernate-distribution-3.6.0.Final\lib\jpa\hibernate-jpa-2.0-api-1.0.0.Final.jar这个jar文件。 引:“部分合并到core了,另一部分合并到entity manager了, 其实是希望用户尽量使用JPA标准的annotation了,annotation的定义可以去看JPA规范。”
1.目前最新的hibernate版本为hibernate3.6 ,此版本已经包含了Hibernate Annotations 和 Hibernate EntityManager,所以现在只要下载hibernate3.6就ok了。
官网地址为:http://www.hibernate.org/
或:http://nchc.dl.sourceforge.net/project/hibernate/hibernate3/3.6.0.Final/hibernate-distribution-3.6.0.Final-dist.zip
即必须导入的包都在改文件夹下:\lib\required(解压后的)
注:在该解压包中:hibernate-distribution-3.6.0.Final\documentation\下有2个文件夹,javadoc和manual,一个是API,一个是帮助文档
2.使用log4j(虽然hibernate使用的是slf4j)
下载地址:http://www.apache.org/dyn/closer.cgi/logging/log4j/1.2.16/apache-log4j-1.2.16.zip
即需要的jar包:log4j-1.2.16.jar
3.单元测试Junit4.9
下载地址:https://github.com/downloads/KentBeck/junit/junit4.9b2.zip
即需要的jar包:junit-4.9b2.jar
4. ejb3-persistence.jar的下载地址:http://www.java2s.com/Code/JarDownload/ejb3-persistence.jar.zip
如果没有改jar包;将有可能出现:javax/persistence/EntitvListeners提示
5. slf4j的下载地址: http://www.slf4j.org/dist/slf4j-1.6.1.zip
即需要slf4j-log4j12-1.6.1.jar包,此包是slf4j转log4j的jar,当你使用log4j的时候所需要的,如果想要使用其他的日志,这需要该包下其他的转换jar包(因开发而异)。
6. hibernate-jpa-2.0-api-1.0.0.Final.jar;该jar在hibernate3.6解压文件中:hibernate-distribution-3.6.0.Final\lib\jpa
如果没有该jar包;将有可能出现:javax.persistence.Caheable的提示。
7. jar全部准备好后,开始建立项目,名称Hibernate_FirstProject,并在src文件夹下导入相应的配置文件: hibernate.cfg.xml和log4j.properties该文件都可以在hibernate3.6包中找到:
hibernate-distribution-3.6.0.Final\project\etc;
但是hibernate.cfg.xml的内容有点少了,所以最好可以去帮助文档里copy一份过来,修改一下就好啦,内容如下:
hibernate.cfg.xml
view plaincopy to clipboardprint?
01.<?xml version='1.0' encoding='utf-8'?>
02.<!DOCTYPE hibernate-configuration PUBLIC
03. "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
04. "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
05.
06.<hibernate-configuration>
07.
08. <session-factory>
09.
10. <!-- Database connection settings -->
11. <property name="connection.driver_class">
12. com.microsoft.sqlserver.jdbc.SQLServerDriver
13. </property>
14. <property name="connection.url">
15. jdbc:sqlserver://localhost:1433;DatabaseName=db_FirstProject
16. </property>
17. <property name="connection.username">sa</property>
18. <property name="connection.password"></property>
19.
20. <!-- JDBC connection pool (use the built-in) -->
21. <!--<property name="connection.pool_size">1</property>-->
22.
23. <!-- SQL dialect -->
24. <property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
25.
26. <!-- Enable Hibernate's automatic session context management -->
27. <property name="current_session_context_class">thread</property>
28.
29. <!-- Disable the second-level cache -->
30. <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider
31. </property>
32.
33. <!-- Echo all executed SQL to stdout -->
34. <property name="show_sql">true</property>
35.
36. <!-- Drop and re-create the database schema on startup -->
37. <property name="hbm2ddl.auto">update</property>
38.
39. </session-factory>
40.
41.</hibernate-configuration>
<?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">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="connection.url">
jdbc:sqlserver://localhost:1433;DatabaseName=db_FirstProject
</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.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>
</session-factory>
</hibernate-configuration>
<property name="connection.driver_class">驱动</property>
<property name="connection.url">URL地址</property>
<property name="connection.username">你自己的用户名</property>
<property name="connection.password">密码</property>
这些就相当于编写JDBC,而hibernate就把这些写入配置文件中
还需要修改的为:
<property name="dialect">使用相应的sql的方言,方言的值可以在帮助文档中找到,即表示hibernate会翻译成相应数据的sql语句</property>
view plaincopy to clipboardprint?
01.DB2 org.hibernate.dialect.DB2Dialect
02.DB2 AS/400 org.hibernate.dialect.DB2400Dialect
03.DB2 OS390 org.hibernate.dialect.DB2390Dialect
04.PostgreSQL org.hibernate.dialect.PostgreSQLDialect
05.MySQL org.hibernate.dialect.MySQLDialect
06.MySQL with InnoDB org.hibernate.dialect.MySQLInnoDBDialect
07.MySQL with MyISAM org.hibernate.dialect.MySQLMyISAMDialect
08.Oracle(any version) org.hibernate.dialect.OracleDialect
09.Oracle 9i org.hibernate.dialect.Oracle9iDialect
10.Oracle 10g org.hibernate.dialect.Oracle10gDialect
11.Sybase org.hibernate.dialect.SybaseDialect
12.Sybase Anywhere org.hibernate.dialect.SybaseAnywhereDialect
13.Microsoft SQL Server org.hibernate.dialect.SQLServerDialect
14.SAP DB org.hibernate.dialect.SAPDBDialect
15.Informix org.hibernate.dialect.InformixDialect
16.HypersonicSQL org.hibernate.dialect.HSQLDialect
17.Ingres org.hibernate.dialect.IngresDialect
18.Progress org.hibernate.dialect.ProgressDialect
19.Mckoi SQL org.hibernate.dialect.MckoiDialect
20.Interbase org.hibernate.dialect.InterbaseDialect
21.Pointbase org.hibernate.dialect.PointbaseDialect
22.FrontBase org.hibernate.dialect.FrontbaseDialect
23.Firebird org.hibernate.dialect.FirebirdDialect
DB2 org.hibernate.dialect.DB2Dialect
DB2 AS/400 org.hibernate.dialect.DB2400Dialect
DB2 OS390 org.hibernate.dialect.DB2390Dialect
PostgreSQL org.hibernate.dialect.PostgreSQLDialect
MySQL org.hibernate.dialect.MySQLDialect
MySQL with InnoDB org.hibernate.dialect.MySQLInnoDBDialect
MySQL with MyISAM org.hibernate.dialect.MySQLMyISAMDialect
Oracle(any version) org.hibernate.dialect.OracleDialect
Oracle 9i org.hibernate.dialect.Oracle9iDialect
Oracle 10g org.hibernate.dialect.Oracle10gDialect
Sybase org.hibernate.dialect.SybaseDialect
Sybase Anywhere org.hibernate.dialect.SybaseAnywhereDialect
Microsoft SQL Server org.hibernate.dialect.SQLServerDialect
SAP DB org.hibernate.dialect.SAPDBDialect
Informix org.hibernate.dialect.InformixDialect
HypersonicSQL org.hibernate.dialect.HSQLDialect
Ingres org.hibernate.dialect.IngresDialect
Progress org.hibernate.dialect.ProgressDialect
Mckoi SQL org.hibernate.dialect.MckoiDialect
Interbase org.hibernate.dialect.InterbaseDialect
Pointbase org.hibernate.dialect.PointbaseDialect
FrontBase org.hibernate.dialect.FrontbaseDialect
Firebird org.hibernate.dialect.FirebirdDialect
8. 编写自己的实体类Teacher.java
view plaincopy to clipboardprint?
01.package com.hero.model;
02.
03.import java.util.Date;
04.
05.import javax.persistence.Column;
06.import javax.persistence.Entity;
07.import javax.persistence.GeneratedValue;
08.import javax.persistence.GenerationType;
09.import javax.persistence.Id;
10.import javax.persistence.Table;
11.import javax.persistence.Temporal;
12.import javax.persistence.TemporalType;
13.import javax.persistence.Transient;
14.
15.import org.hibernate.annotations.GenericGenerator;
16.
17.@Entity
18.public class Teacher {
19. private int id;
20. private String password;
21. private String username;
22. public Teacher(){}
23.
24. @Id
25. @GeneratedValue(strategy=GenerationType.AUTO)
26. public int getId() {
27. return id;
28. }
29. public void setId(int id) {
30. this.id = id;
31. }
32. public String getPassword() {
33. return password;
34. }
35. public void setPassword(String password) {
36. this.password = password;
37. }
38. public String getUsername() {
39. return username;
40. }
41. public void setUsername(String username) {
42. this.username = username;
43. }
44.
45.}
package com.hero.model;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import org.hibernate.annotations.GenericGenerator;
@Entity
public class Teacher {
private int id;
private String password;
private String username;
public Teacher(){}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
9.编写自己的测试类TeacherTest.java
view plaincopy to clipboardprint?
01.package com.hero.model;
02.
03.import static org.junit.Assert.*;
04.
05.import java.util.Date;
06.
07.import junit.framework.TestCase;
08.
09.import org.hibernate.Session;
10.import org.hibernate.SessionFactory;
11.import org.hibernate.cfg.Configuration;
12.
13.public class TeacherTest extends TestCase{
14. private SessionFactory sessionFactory;
15.
16. @Override
17. protected void setUp() throws Exception {
18. // A SessionFactory is set up once for an application
19. sessionFactory = new Configuration()
20. .configure() // configures settings from hibernate.cfg.xml
21. .buildSessionFactory();
22. }
23.
24. @Override
25. protected void tearDown() throws Exception {
26. if ( sessionFactory != null ) {
27. sessionFactory.close();
28. }
29. }
30.
31. @SuppressWarnings({ "unchecked" })
32. public void testBasicUsage() {
33. // create a couple of events...
34. Teacher t=new Teacher();
35. t.setPassword("tea1");
36. t.setUsername("teacher2");
37. Session session = sessionFactory.openSession();
38. session.beginTransaction();
39. session.save(t);
40. session.getTransaction().commit();
41. session.close();
42. }
43.
44.}
package com.hero.model;
import static org.junit.Assert.*;
import java.util.Date;
import junit.framework.TestCase;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class TeacherTest extends TestCase{
private SessionFactory sessionFactory;
@Override
protected void setUp() throws Exception {
// A SessionFactory is set up once for an application
sessionFactory = new Configuration()
.configure() // configures settings from hibernate.cfg.xml
.buildSessionFactory();
}
@Override
protected void tearDown() throws Exception {
if ( sessionFactory != null ) {
sessionFactory.close();
}
}
@SuppressWarnings({ "unchecked" })
public void testBasicUsage() {
// create a couple of events...
Teacher t=new Teacher();
t.setPassword("tea1");
t.setUsername("teacher2");
Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(t);
session.getTransaction().commit();
session.close();
}
}
在hibernate3.6中,已经不再使用AnnotationConfiguration了,该内容的东西都被加入到了Configuration中
10.在hibernate.cfg.xml中加入如下代码:
<mapping class="com.hero.model.Teacher" />
11.测试,perfect,ok!!!
发表评论
-
讲解MSSQL数据库中SQL锁机制和事务隔离级别
2011-03-27 01:09 1830锁机制 NOLOCK和READPAST的区别。 1. 开启 ... -
hibernate:1+N问题
2011-03-26 20:02 915Hibernate中常会用到set,bag等集合表示1对多的关 ... -
QBC查询方式
2011-03-26 18:35 855QBC(Query By Criteria)查询方式是Hibe ... -
hibernate--关系映射CRUD
2011-03-25 22:41 948CRUD中主要学习了 1.cascade=(Cascad ... -
hibernate3 一对多多对一单向关联级联增删改查
2011-03-25 22:38 1759hibernate中如何对一对多,多对一的单向关联进行级联 ... -
Hibernate加载其数据库配置
2011-03-23 21:58 1125Hibernate加载其配置文件hibernate.prop ...
相关推荐
下面将详细介绍如何整合Struts2.2.1、Hibernate3.6和Spring3.0.5。 首先,确保准备好所有必要的库文件,包括: 1. Struts2的struts2-2.2.1-all.zip 2. Hibernate的hibernate-distribution-3.6.0.Final-dist.zip 3. ...
在本项目中,我们探讨了如何整合Spring 3.0、Hibernate 3.6和Struts2.2.1这三大框架,以构建一个高效、灵活的企业级Web应用程序。这三者结合,提供了模型-视图-控制器(MVC)架构、持久层管理和AOP(面向切面编程)...
### Hibernate Annotation 中文文档知识点概览 #### 一、创建注解项目 ##### 1.1 系统需求 在创建一个使用 Hibernate 注解的项目之前,需要满足一定的系统环境需求,例如支持 Java 的开发环境、JDK 版本、支持 ...
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> org.whvcse.model.Userinfo ...
3.6. Hibernate事务属性 3.7. 其他属性 3.8. Hibernate SQL方言 (hibernate.dialect) 3.9. Hibernate日志类别 3.10. JTA TransactionManagers 9.1. 继承映射特性(Features of inheritance mappings) 16.1. ...
3.6. Hibernate事务属性 3.7. 其他属性 3.8. Hibernate SQL方言 (hibernate.dialect) 3.9. Hibernate日志类别 3.10. JTA TransactionManagers 9.1. 继承映射特性(Features of inheritance mappings) 16.1. ...
3.6. Hibernate事务属性 3.7. 其他属性 3.8. Hibernate SQL方言 (hibernate.dialect) 3.9. Hibernate日志类别 3.10. JTA TransactionManagers 9.1. 继承映射特性(Features of inheritance mappings) 16.1. ...
在ORM框架如Hibernate中,注解被用于映射Java对象到数据库表,简化了配置并增强了可读性。 5.3 AOP(面向切面编程) Spring框架中的注解支持AOP,如@Transactional,用于声明式事务管理。 六、实际应用案例 6.1 ...
3.6. Hibernate事务属性 3.7. 其他属性 3.8. Hibernate SQL方言 (hibernate.dialect) 3.9. Hibernate日志类别 3.10. JTA TransactionManagers 9.1. 继承映射特性(Features of inheritance mappings) 16.1. ...
5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) ...
5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent ...
3.6. 实现NamingStrategy 3.7. XML配置文件 3.8. J2EE应用程序服务器的集成 3.8.1. 事务策略配置 3.8.2. JNDI绑定的SessionFactory 3.8.3. JTA和Session的自动绑定 3.8.4. JMX部署 4. 持久化类(Persistent ...
3.6. 实现NamingStrategy 3.7. XML配置文件 3.8. J2EE应用程序服务器的集成 3.8.1. 事务策略配置 3.8.2. JNDI绑定的SessionFactory 3.8.3. 在JTA环境下使用Current Session context (当前session上下文)管理 ...
3.6. 实现NamingStrategy 3.7. XML配置文件 3.8. J2EE应用程序服务器的集成 3.8.1. 事务策略配置 3.8.2. JNDI绑定的SessionFactory 3.8.3. JTA和Session的自动绑定 3.8.4. JMX部署 4. 持久化类(Persistent ...
3.6. 实现NamingStrategy 3.7. XML配置文件 3.8. J2EE应用程序服务器的集成 3.8.1. 事务策略配置 3.8.2. JNDI绑定的SessionFactory 3.8.3. 在JTA环境下使用Current Session context (当前session上下文)管理 ...
| 10 | commons-lang3 | 3.6 | 数据操作封装 | | 11 | c3p0 | 0.9.5.2 | Datasource数据源 | | 12 | commons-collections| 3.2.2 | 集合数据对象操作扩展 | | 13 | mchange-commons-java| 0.2.14 | 数据源封装连接 |...
7.5.1 @annotation() 7.5.2 execution() 7.5.3 args()和@args() 7.5.4 within() 7.5.5 @within()和@target() 7.5.6 target()的this() 7.6 @AspectJ进阶 7.6.1 切点复合运算 7.6.2 命名切点 7.6.3 ...
7.5.1 @annotation() 7.5.2 execution() 7.5.3 args()和@args() 7.5.4 within() 7.5.5 @within()和@target() 7.5.6 target()的this() 7.6 @AspectJ进阶 7.6.1 切点复合运算 7.6.2 命名切点 7.6.3 ...
3.6. bean定义的继承 3.7. 容器扩展点 3.7.1. 用BeanPostProcessor定制bean 3.7.2. 用BeanFactoryPostProcessor定制配置元数据 3.7.3. 使用FactoryBean定制实例化逻辑 3.8. The ApplicationContext 3.8.1. ...