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 (?, ?, ?)