`

Hibernate4 基于annotation实例

阅读更多

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+

junit4.10.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>
<!-- <mapping resource="com/test/pojo/Student.hbm.xml"/> -->
<mapping class="com.test.pojo.Teacher" />
</session-factory>
</hibernate-configuration>

pojo类Teacher

package com.test.pojo;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class Teacher {
	@Id
	private int id;
	private String name;
	private 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;
	}
	
}

 

 junit4测试类TeacherTest

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 TeacherTest {
	
	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);
		//sessionFactory = configuration.addAnnotatedClass(Teacher.class).buildSessionFactory(serviceRegistry);

	} 
	@Test
	public void testSave() {
		Teacher t = new Teacher();
		t.setId(1);
		t.setName("wangwu");
		t.setAge(38);
		Session session = sessionFactory.getCurrentSession();
		session.beginTransaction();
		session.save(t);
		session.getTransaction().commit();
	}
}

 输出:

2013-9-8 14:46:04 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
2013-9-8 14:46:04 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.5.Final}
2013-9-8 14:46:04 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
2013-9-8 14:46:04 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
2013-9-8 14:46:04 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
2013-9-8 14:46:04 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
2013-9-8 14:46:05 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
2013-9-8 14:46:05 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
2013-9-8 14:46:05 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000115: Hibernate connection pool size: 1
2013-9-8 14:46:05 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
2013-9-8 14:46:05 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:46:05 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=root, password=****}
2013-9-8 14:46:08 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
2013-9-8 14:46:08 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
2013-9-8 14:46:08 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
2013-9-8 14:46:09 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
2013-9-8 14:46:09 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000102: Fetching database metadata
2013-9-8 14:46:09 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000396: Updating schema
2013-9-8 14:46:10 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000261: Table found: hibernate4.teacher
2013-9-8 14:46:10 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000037: Columns: [id, age, name]
2013-9-8 14:46:10 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000108: Foreign keys: []
2013-9-8 14:46:10 org.hibernate.tool.hbm2ddl.TableMetadata <init>
INFO: HHH000126: Indexes: [primary]
2013-9-8 14:46:10 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000232: Schema update complete
Hibernate: insert into Teacher (age, name, id) values (?, ?, ?)

 

 

  • 大小: 20.6 KB
  • 大小: 20.6 KB
分享到:
评论

相关推荐

    学习JPA——Hibernate_Annotation使用实例

    学习JPA——Hibernate_Annotation使用实例学习JPA——Hibernate_Annotation使用实例学习JPA——Hibernate_Annotation使用实例

    hibernate annotation

    hibernate annotation 实例

    spring3+hibernate4配置声明式事务管理(annotation方式)

    一个小实例工程,说的是spring3+hibernate4配置声明式事务管理(annotation方式)

    JSF+Spring+Hibernate(Annotation)

    JSF+Spring+Hibernate(Annotation)的login小实例,建议入门的朋友看看,老鸟就免了,呵呵。环境:SQLSever2000+jdk5.0+spring2.0+hibernate3.2+jsf

    Struts2+Spring2.5+Hibernate3全注解实例详解

    超级详细的SSH2项目实例详解,并且附带两个项目详解。两种注解实现方式。不同的生成数据脚本实现。 在JavaEE企业级开发中,以SSH2框架为核心的应用非常广,大象根据项目实践经验,通过二个实例,详细的为大家讲解...

    Hibernate Annotation 学习笔记

    一步步hibernate annotation练习笔记, 实例操作,从经验中学习

    使用Annotation并对DAO层封装具有分页功能的S2SH整合实例_好资源0分送

    个人认为由Sun官方支持的EJB规范会越来越流行,此时如果使用基于Annotation的SSH框架很容易转移到Struts+EJB+Spring的项目中,而且使用Annotation,很容易实现0配置,像在这个实例中就一个配置,这样避免了配置文件...

    springMVC+Hibernate3整合实例

    里面整合的是springMVC+hibernate3的配置文件和实例,用的是mysql数据库,使用注解annotation,下载后,只需修改jdbc.properties配置文件和加载对应的数据库驱动jar包就可以运行。亲测通过,有问题可以交流。

    Hibernate的Annotation版Hello world实例

    主要介绍了Hibernate的Annotation版Hello world实现方法,详细分析了Annotation的具体使用步骤与Hello world实现方法,需要的朋友可以参考下

    struts Spring Hibernate标签和注解资料合集

    springmvc注解.doc,Spring注解讲解.doc,struts2标签详解.pdf,struts2常用标签.pdf,Struts2页面开发中常用标签.pdf,Struts2注解详细说明文档.doc,Hibernate注解教程:Hibernate Annotation使用实例.maff

    SSH2 annotation 实现struts2.1.6 spring2.5.6 hibernate3.3 全注解开发

    SSH2 annotation 实现struts2.1.6 spring2.5.6 hibernate3.3 全注解开发 hibernate延迟加载_懒加载 具体应用

    joeylv#joscrapy#Hibernate的Annotation注解1

    声明为双向关联多对多关联关系多对多关联声明。实例:有如下两个实体,商品:Goods,分类Category。两者是多对一的关联关系。使用Hibernate Ann

    Hibernate hql查询代码实例

    主要介绍了Hibernate hql查询代码实例,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下

    SpringMVC+Hibernate3+CXF整合详细实例

    里面有关于springMVC利用cxf发布webservice和调用接口的详细代码,后台用annotation注解关联数据库,dao层完美封装,架构层次分明,用不到cxf的可以到这个链接免费下载springMVC+hibernate3的实例代码...

    SpringMVC+Hibernate实例

    &lt;mvc:annotation-driven/&gt; &lt;!--视图解析--&gt; &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt; ...

    Hibernate+中文文档

    5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent ...

    JQuery1.4.2+Struts2.1.8+JSON0.34+Spring2.5.6+Hibernate3.5+XFire1.2.6整合实例

    JQuery1.4.2+Struts2.1.8+JSON0.34+Spring2.5.6+Hibernate3.5+XFire1.2.6整合实例(已上传) 1、JSON0.34使用的是struts2附带的struts2-json-plugin-2.1.8.1.jar 2、db是mysql,名字为test,用户名root,密码空 3、...

    hibernate3.2中文文档(chm格式)

    5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent ...

    HibernateAPI中文版.chm

    5.5.2. 使用 JDK 5.0 的注解(Annotation) 5.6. 数据库生成属性(Generated Properties) 5.7. 辅助数据库对象(Auxiliary Database Objects) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent ...

    最全Hibernate 参考文档

    5.4.2. 使用 JDK 5.0 的注解(Annotation) 6. 集合类(Collections)映射 6.1. 持久化集合类(Persistent collections) 6.2. 集合映射( Collection mappings ) 6.2.1. 集合外键(Collection foreign keys) 6.2.2. 集合...

Global site tag (gtag.js) - Google Analytics