hibernate最新版本:hibernate-release-4.2.5.Final
Myeclipse 8.5 + jdk1.6.10(使用自己的jdk1.5报错,提示版本过低,网上说是hibernate4以上版本需要jdk1.6,但是使用myeclipse自带的1.5没有问题)
jar包:hibernate-release-4.2.5.Final\lib\required下的所有包+mysql-connector-java-5.1.15-bin.jar
项目目录如下
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://127.0.0.1:3306/hibernate4</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.MySQL5Dialect</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.internal.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> <!-- xml文件--> <mapping resource="com/test/pojo/Student.hbm.xml"/> <!--annotation注解--> <mapping class="com.test.pojo.Teacher" /> </session-factory> </hibernate-configuration>
Student类
package com.test.pojo; public class Student { public int id; public String name; public int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
Student.hbm.xml配置
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="com.test.pojo"> <class name="Student"> <id name="id"></id> <property name="name" /> <property name="age" /> </class> </hibernate-mapping>
juit4 StudentTest类
package com.test.pojo; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.service.ServiceRegistryBuilder; import org.junit.BeforeClass; import org.junit.Test; public class StudentTest { private static SessionFactory sessionFactory = null; @BeforeClass public static void beforeClass() { Configuration configuration = new Configuration(); configuration.configure(); ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); } @Test public void testSave() { Student s = new Student(); s.setId(1); s.setName("zhangsan"); s.setAge(20); Session session = sessionFactory.getCurrentSession(); session.beginTransaction(); session.save(s); session.getTransaction().commit(); } }
结果输出:
2013-9-8 14:25:31 org.hibernate.annotations.common.Version <clinit> INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final} 2013-9-8 14:25:31 org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.2.5.Final} 2013-9-8 14:25:31 org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found 2013-9-8 14:25:31 org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist 2013-9-8 14:25:31 org.hibernate.cfg.Configuration configure INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 2013-9-8 14:25:31 org.hibernate.cfg.Configuration getConfigurationInputStream INFO: HHH000040: Configuration resource: /hibernate.cfg.xml 2013-9-8 14:25:31 org.hibernate.cfg.Configuration addResource INFO: HHH000221: Reading mappings from resource: com/test/pojo/Student.hbm.xml 2013-9-8 14:25:31 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide! 2013-9-8 14:25:31 org.hibernate.cfg.Configuration doConfigure INFO: HHH000041: Configured SessionFactory: null 2013-9-8 14:25:31 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!) 2013-9-8 14:25:31 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000115: Hibernate connection pool size: 1 2013-9-8 14:25:31 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000006: Autocommit mode: false 2013-9-8 14:25:31 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://127.0.0.1:3306/hibernate4] 2013-9-8 14:25:31 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000046: Connection properties: {user=root, password=****} 2013-9-8 14:25:32 org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect 2013-9-8 14:25:32 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService INFO: HHH000399: Using default transaction strategy (direct JDBC transactions) 2013-9-8 14:25:32 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init> INFO: HHH000397: Using ASTQueryTranslatorFactory 2013-9-8 14:25:33 org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000228: Running hbm2ddl schema update 2013-9-8 14:25:33 org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000102: Fetching database metadata 2013-9-8 14:25:33 org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000396: Updating schema 2013-9-8 14:25:33 org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000261: Table found: hibernate4.student 2013-9-8 14:25:33 org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000037: Columns: [id, age, name] 2013-9-8 14:25:33 org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000108: Foreign keys: [] 2013-9-8 14:25:33 org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000126: Indexes: [primary] 2013-9-8 14:25:33 org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000261: Table found: hibernate4.teacher 2013-9-8 14:25:33 org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000037: Columns: [id, age, name] 2013-9-8 14:25:33 org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000108: Foreign keys: [] 2013-9-8 14:25:33 org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000126: Indexes: [primary] 2013-9-8 14:25:33 org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000232: Schema update complete Hibernate: insert into Student (name, age, id) values (?, ?, ?)
相关推荐
spring2.5+hibernate基于xml配置的实例
Hibernate XML配置表映射实例
springmvc5+hibernate5+junit4+log4j2整合实例 包含 代码实例 数据库建表sql 运行环境 myeclipes2015 jdk 1.8 tomcat 8.0
(1)Configuration:用于解析hibernate.cfg.xml文件和XXXXX.hbm.xml文件,并创建SessionFactory对象。Configuration对象用于配置并且启动Hibernate。Hibernate应用通过Configuration实例来指定对象--关系映射文件的...
Hibernate 是一个开源的O/R mappimg的框架,基于JDBC提供了一种持久性数据管理的方案,相对于EntityBean来说是相当轻量级的。由于Hibernate是基于 JDBC的,所以它的数据库查寻的能力相对于CMP来说也是异常强大的,...
关于hibernate的极简实例,小白都能看懂,注解方式和xml配置方式均有!使用语言JAVA,数据库Mysql。
配置了tomcat之后发现jndi好简单啊,可是碰到了hibernate该怎么做呢,本例详细解析
基于Struts2, Spring, Hibernate的整合实例设计源码,该项目包含27个文件,主要文件类型有14个java源文件,以及7个xml配置文件。此外,还包括2个properties文件,以及2个jsp页面文件。该项目是一个基于Struts2, ...
含JAR包,Spring4.2.0.RC3+Hibernate4.3.10.Final,目前最新包,事务使用XML配置,含通用DAO,内有运行小实例,整合多位大神资料,感谢他们!
ext+hibernate+spring 入库的实例。和各个.xml的配置。
一个实例小工程,说的是spring3+hibernate4怎么配置声明式事务管理(xml方式)
Hibernate连接MySql小实例 环境:eclipse3.5 数据库:mysql 需要建立tc数据局,tc下有张user表,user表有id,name,pass项。 请在连接数据库配置文件hibernate.ctf.xml中配置用户名和密码 很容易理解的
采用在web.xml中加载spring配置文件的方法降低struts和spring的耦合度,使用自定义的MyDelegatingRequestProcessor类来代替spring的DelegatingRequestProcessor类来解决spring中action的bean的重复配置问题。...
本例主要是实现了struts2+spring3+hibernate3的 基本框架搭建的注册登录,以及用户增删改查,适于初学者学习。 ... 分页的实现 前端校验 ...4,OK 现在就可以完美运行ssh框架的实例了,开始行动吧!
该项目为通过IDEA使用Hibernate框架,详细制作过程见文章:...数据库默认并不是本地数据库,所以运行的时候记得去hibernate.cfg.xml改为所用的数据库地址及账号密码
hibernate单向多对多映射(XML版)
3.数据库采用的是oracle10g,sid采用的是orcl,如果oracle安装是sid不是orcl,请修改本实例下面的applicationContext.xml中的连接池代码,将orcl改为自己的数据库sid 4.请将实例中的数据库驱动ojdbc6放到tomcat的...
这个小实例 适合初学者学习hibernate 用的mysql数据库,建立表格User ,表格字段在User.java理由 然后要修改hibernate.cfg.xml里的数据库名、密码等
spring-hibernate-dwr做的AJAX操作CRUD实例 环境:myeclipse6.0+jdk1.6 所需lib列表,请自行加入 mysql-connector-java-3.1.7-bin.jar antlr-2.7.6rc1.jar asm-attrs.jar cglib-2.1.3.jar ...
struts2的小例子。数据库表的创建请参见工程中的sql.txt文件。实体映射采用的是实体标注映射,省去了*.hbm.xml文件。 /WEB-INF/lib中的包请在另一个资源中下载。(由于超过了10M,所以分开了。)