首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

Hibernate4 基于xml范例

2013-10-01 
Hibernate4 基于xml实例hibernate最新版本:hibernate-release-4.2.5.Final?Myeclipse 8.5?+ jdk1.6.10(使

Hibernate4 基于xml实例

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

?

项目目录如下
Hibernate4 基于xml范例


?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 /></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;@BeforeClasspublic static void beforeClass() {Configuration configuration = new Configuration();configuration.configure();ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();sessionFactory = configuration.buildSessionFactory(serviceRegistry);} @Testpublic 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 logVersionINFO: HHH000412: Hibernate Core {4.2.5.Final}2013-9-8 14:25:31 org.hibernate.cfg.Environment <clinit>INFO: HHH000206: hibernate.properties not found2013-9-8 14:25:31 org.hibernate.cfg.Environment buildBytecodeProviderINFO: HHH000021: Bytecode provider name : javassist2013-9-8 14:25:31 org.hibernate.cfg.Configuration configureINFO: HHH000043: Configuring from resource: /hibernate.cfg.xml2013-9-8 14:25:31 org.hibernate.cfg.Configuration getConfigurationInputStreamINFO: HHH000040: Configuration resource: /hibernate.cfg.xml2013-9-8 14:25:31 org.hibernate.cfg.Configuration addResourceINFO: HHH000221: Reading mappings from resource: com/test/pojo/Student.hbm.xml2013-9-8 14:25:31 org.hibernate.internal.util.xml.DTDEntityResolver resolveEntityWARN: 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 doConfigureINFO: HHH000041: Configured SessionFactory: null2013-9-8 14:25:31 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)2013-9-8 14:25:31 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000115: Hibernate connection pool size: 12013-9-8 14:25:31 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: HHH000006: Autocommit mode: false2013-9-8 14:25:31 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configureINFO: 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 configureINFO: HHH000046: Connection properties: {user=root, password=****}2013-9-8 14:25:32 org.hibernate.dialect.Dialect <init>INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect2013-9-8 14:25:32 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateServiceINFO: HHH000399: Using default transaction strategy (direct JDBC transactions)2013-9-8 14:25:32 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>INFO: HHH000397: Using ASTQueryTranslatorFactory2013-9-8 14:25:33 org.hibernate.tool.hbm2ddl.SchemaUpdate executeINFO: HHH000228: Running hbm2ddl schema update2013-9-8 14:25:33 org.hibernate.tool.hbm2ddl.SchemaUpdate executeINFO: HHH000102: Fetching database metadata2013-9-8 14:25:33 org.hibernate.tool.hbm2ddl.SchemaUpdate executeINFO: HHH000396: Updating schema2013-9-8 14:25:33 org.hibernate.tool.hbm2ddl.TableMetadata <init>INFO: HHH000261: Table found: hibernate4.student2013-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.teacher2013-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 executeINFO: HHH000232: Schema update completeHibernate: insert into Student (name, age, id) values (?, ?, ?)

?

热点排行