- 浏览: 2299973 次
- 性别:
- 来自: 成都
-
文章分类
- 全部博客 (357)
- J2EE (49)
- JavaScript (40)
- Spring (19)
- Struts (5)
- CSS (8)
- Hibernate (16)
- Java (67)
- DWR (4)
- JSON (3)
- XFIRE (1)
- Tomcat (1)
- Ant (2)
- 设计模式 (2)
- 经典收藏 (2)
- JSP (10)
- Linux (0)
- WebLogic (11)
- myeclipse (13)
- Buffalo (4)
- 文件上传相关 (1)
- oracle (33)
- html (6)
- JSTL (3)
- SVN (2)
- GIT (1)
- 孙卫琴(Java网络编程精解) (1)
- DOM4J (2)
- Swing (1)
- AJAX (1)
- Eclipse (5)
- 日志组件 (3)
- PowerDesigner (1)
- Jquery (22)
- IT技术开发相关网址 (1)
- Nutz (1)
- 其它 (1)
- Velocity (3)
- WebService (1)
- MySql (2)
- Android (1)
- Maven (2)
- Quartz (11)
- Lucene (1)
- springsource (1)
- Junit (1)
- Activiti (0)
最新评论
-
yzlseu:
拼凑,没有营养
Activiti进阶—分配组任务 -
zhangsenhao:
非常赞!代码很清楚
SpringMVC3.0+MyIbatis3.0(分页示例) -
xiamw2000:
分页写得不对,应该是 : order by ${orderNa ...
SpringMVC3.0+MyIbatis3.0(分页示例) -
sheertewtw:
...
SpringMVC:上传与下载 -
kingtoon:
...
XSS之xssprotect
一 环境:W7+Eclipse3.6
二 所用Hibernate版本:hibernate-distribution-3.6.0.Final
工程目录结构如下
三 参考资料
1 Hibernate3.6的Annotation问题
http://mayuchuan99.blog.163.com/blog/static/320023442011029438354/
hibernate3.6之前的版本使用Annotation,还需要下载Annotation库,需要添加ejb3-persistence.jar hibernate-annotations.jar hibernate-commons-annotations.jar,而获得SessionFactory必须以下这样写
在hibernate3.6的这个版本中,Annotation类库集成到了hibernate3.6,所以不在需要添加hibernate-annotations.jar hibernate-commons-annotations.jar等类库了。但是必须添加hibernate-jpa-2.0-api-1.0.0.Final.jar。
hibernate 3.6要获取一个SessionFactory,我可以直接像用xml来配置实体与数据库表的映射关系那样。代码如下:
四 具体代码
1 hibernate.cfg.xml
2 Student.hbm.xml
3 Student.java
4 HibernateUtil.java
5 Test.java
6 mysql.sql
二 所用Hibernate版本:hibernate-distribution-3.6.0.Final
工程目录结构如下

三 参考资料
1 Hibernate3.6的Annotation问题
http://mayuchuan99.blog.163.com/blog/static/320023442011029438354/
hibernate3.6之前的版本使用Annotation,还需要下载Annotation库,需要添加ejb3-persistence.jar hibernate-annotations.jar hibernate-commons-annotations.jar,而获得SessionFactory必须以下这样写
Configuration cfg=new AnnotationConfiguration(); SessionFactory s=cfg.configure().buildSessionFactory();
在hibernate3.6的这个版本中,Annotation类库集成到了hibernate3.6,所以不在需要添加hibernate-annotations.jar hibernate-commons-annotations.jar等类库了。但是必须添加hibernate-jpa-2.0-api-1.0.0.Final.jar。
hibernate 3.6要获取一个SessionFactory,我可以直接像用xml来配置实体与数据库表的映射关系那样。代码如下:
Configuration cfg=new Configuration(); SessionFactory s=cfg.configure().buildSessionFactory();
四 具体代码
1 hibernate.cfg.xml
<?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.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- JDBC connection pool (use the built-in) <property name="connection.pool_size">1</property> --> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQLDialect</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> --> <!-- <mapping resource="org/hibernate/model/Student.hbm.xml"/> --> <mapping class="org.hibernate.model.Student"/> </session-factory> </hibernate-configuration>
2 Student.hbm.xml
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping > <class name="org.hibernate.model.Student"> <id name="id" column="id"> <generator class="native"/> </id> <property name="name"/> <property name="age"/> </class> </hibernate-mapping>
3 Student.java
@Entity public class Student { @Id @GeneratedValue private int id; private String name; private int age; //get set }
4 HibernateUtil.java
package org.hibernate.model; 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; } }
5 Test.java
import org.hibernate.Session; public class Test { public static void main(String[] args) { Student s = new Student(); // 设置了ID的自动递增,就不用在指定ID值 // s.setId(1); s.setName("zhangsan"); s.setAge(8); Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); session.save(s); session.getTransaction().commit(); HibernateUtil.getSessionFactory().close(); } }
6 mysql.sql
DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `id` int(20) NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL, `age` int(3) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1; INSERT INTO `student` VALUES ('1', 'zhangsan', '8');
- Hibernate-HelloWorld-Project.zip (9.7 KB)
- 下载次数: 29
- Hibernate-lib.zip (1.8 MB)
- 下载次数: 47
- mysql-connector-java-5.1.10.jar (707.3 KB)
- 下载次数: 13
发表评论
-
Hibernate之自定义ID生成器
2011-09-15 10:40 6340一 xxx.hml.xml <id name=&q ... -
Hibernate之java.lang.UnsupportedOperationException: Can't write to a readonly obj
2011-09-15 08:56 3229参考资料 1 java.lang.UnsupportedOpe ... -
Hibernate:LazyInitializationException:could not initialize proxy - no Session
2011-05-11 14:33 11755一 参考资料 hibernate延迟加载的传说级错误 org. ... -
Hibernate:一对一双向主键关联
2011-04-05 11:53 4618参考资料: Hibernate系列:映射一对一主键双向关联 h ... -
Hibernate:setting property value with CGLIB (set hibernate.cglib.use_reflection_
2011-03-23 15:44 2124参考资料 1 Hibernate org.hibernate. ... -
Hibernate:一对一单向主键关联
2011-03-19 18:55 1513一 环境:W7+Eclipse3.6+JDK1.6+MySQL ... -
Hibernate:一对一双向外键关联
2011-03-18 20:44 1330一 环境:W7+Eclipse3.6+JDK1.6+MySQL ... -
Hibernate:一对一单向外键关联
2011-03-18 20:24 1419一 环境:W7+Eclipse3.6+JDK1.6+MySQL ... -
Hibernate:connection is not valid without active transaction
2011-03-18 13:44 3594参考资料 1 org.hibernate.HibernateE ... -
Hibernate之GenericJDBCException(不能插入错误)
2011-03-14 00:27 16094参考资料 SSH+Oracle问题:org.hibernate ... -
Hibernate搭建日志环境(log4j)及打印DDL语句
2011-03-13 16:26 3783一 参考资料 Log4j over SLF4J http:// ... -
Hibernate: 主键生成策略(ID为varcahr2)
2011-02-23 16:11 13577一 环境: XP+Oracle10+Myeclipse6.6 ... -
S2SH整合:java.lang.IllegalArgumentException: node to traverse cannot be null
2011-02-23 10:38 7386参考资料 1 java.lang.IllegalArgumen ... -
Hibernate学习资料
2011-01-29 12:37 1660良葛格 Hibernate 3 入門和JPA 批注参考(中文) ... -
转载: get方法和load方法区别
2010-08-02 09:43 1357转载: get方法和load方法区别 http://wlh26 ...
相关推荐
第1章 初识MyEclipse 1 1.1 MyEclipse简介 1 1.2 MyEclipse的安装 1 1.2.1 JDK的安装与配置 1 1.2.2 MyEclipse 7.0的安装和运行 4 1.3 获取和阅读MyEclipse帮助文档 5 1.4 本章小结 5 第2章 MyEclipse集成开发环境的...
第2章 初识Struts 2 16 2.1 声明性架构 16 2.1.1 两种配置 16 2.1.2 声明架构的两种方式 17 2.1.3 智能默认值 20 2.2 简单的HelloWorld示例 20 2.2.1 部署示例应用程序 20 2.2.2 探索HelloWorld应用程序 24 2.3 使用...
第2章初识Struts 14 2.1 Struts的优点 14 2.2 Struts的动作处理流程 15 2.3 拦截器 17 2.4 Struts配置文件 18 2.4.1 struts.xml文件 19 2.4.2 struts.properties文件 26 2.5 Struts应用程序示例 26 2.5.1 ...
3.6 异常处理:exception-mapping元素 47 3.7 通配符映射 48 3.8 动态方法调用 51 3.9 对动作类进行测试 51 3.10 小结 51 第4章 OGNL 52 4.1 Value Stack栈 52 4.2 读取Object Stack里的对象的属性 53 4.3 读取...