`
zxsqi
  • 浏览: 28115 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

hibernate 4.1.5 学习笔记(2)

阅读更多

hibernate 使用annotation进行的操作方法:

 

1. 需要在实体类中添加注解,例如下列代码:

 

/*
 * Hibernate, Relational Persistence for Idiomatic Java
 *
 * Copyright (c) 2010, Red Hat Inc. or third-party contributors as
 * indicated by the @author tags or express copyright attribution
 * statements applied by the authors.  All third-party contributions are
 * distributed under license by Red Hat Inc.
 *
 * This copyrighted material is made available to anyone wishing to use, modify,
 * copy, or redistribute it subject to the terms and conditions of the GNU
 * Lesser General Public License, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
 * for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this distribution; if not, write to:
 * Free Software Foundation, Inc.
 * 51 Franklin Street, Fifth Floor
 * Boston, MA  02110-1301  USA
 */
package com.buy.user;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.GenericGenerator;

@Entity
@Table( name = "EVENTS" )
public class Event {
	private Long id;

	private String title;
	private Date date;

	public Event() {
		// this form used by Hibernate
	}

	public Event(String title, Date date) {
		// for application use, to create new events
		this.title = title;
		this.date = date;
	}

	@Id
	@GeneratedValue(generator="increment")
	@GenericGenerator(name="increment", strategy = "increment")
	public Long getId() {
		return id;
	}

	private void setId(Long id) {
		this.id = id;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "EVENT_DATE")
	public Date getDate() {
		return date;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
}

 

2. hibernate.cfg.xml 配置如下:

 

<?xml version='1.0' encoding='utf-8'?>
<!--
  ~ Hibernate, Relational Persistence for Idiomatic Java
  ~
  ~ Copyright (c) 2010, Red Hat Inc. or third-party contributors as
  ~ indicated by the @author tags or express copyright attribution
  ~ statements applied by the authors.  All third-party contributions are
  ~ distributed under license by Red Hat Inc.
  ~
  ~ This copyrighted material is made available to anyone wishing to use, modify,
  ~ copy, or redistribute it subject to the terms and conditions of the GNU
  ~ Lesser General Public License, as published by the Free Software Foundation.
  ~
  ~ This program is distributed in the hope that it will be useful,
  ~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  ~ or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
  ~ for more details.
  ~
  ~ You should have received a copy of the GNU Lesser General Public License
  ~ along with this distribution; if not, write to:
  ~ Free Software Foundation, Inc.
  ~ 51 Franklin Street, Fifth Floor
  ~ Boston, MA  02110-1301  USA
  -->
<!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/buy360</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>

        <!-- 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">create</property>

		<!-- <mapping resource="com/buy/user/Event.hbm.xml"/> -->
		<mapping class="com.buy.user.Event"/>

    </session-factory>

</hibernate-configuration>
 
分享到:
评论

相关推荐

    hibernate 4.1.5 source code

    《深入剖析Hibernate 4.1.5源码》 Hibernate,作为Java领域中著名的对象关系映射(ORM)框架,自发布以来就以其强大的功能和易用性深受开发者喜爱。本文将聚焦于Hibernate 4.1.5版本的源代码,通过对源码的深入解析...

    Hibernate 4.1.5 SP1 API chm格式

    Hibernate 4.1.5 SP1 API chm格式方便携带和查询 如果文件打开看不到右边的内容,是因为你的操作系统为了安全对下载的chm文件进行了锁定,只需要在打开前右键单击该chm文件选择“属性”,然后在“常规”选项卡的...

    Hibernate-4.1.5-API

    Hibernate4.1.5的API文档,英文版

    hibernate-release-4.1.5.SP1

    《Hibernate 4.1.5.SP1:持久化框架的核心技术与应用》 Hibernate,作为Java领域中的一个著名对象关系映射(ORM)框架,它极大地简化了数据库操作,使得开发者能够以面向对象的方式处理数据库交互。本次我们将深入...

    hibernate-release-4.1.5.SP1.zip

    《Hibernate 4.1.5.SP1:Java ORM框架的核心技术与实践》 Hibernate,作为Java领域中的一个著名对象关系映射(ORM)框架,它极大地简化了数据库操作,使得开发者能够以面向对象的方式处理数据库交互。这次我们关注...

    struts2.3.2+hibernate4.3.8+spring4.1.5整合jar包

    Struts2、Hibernate4和Spring4(简称SSH)是Java Web开发中三个非常重要的框架,它们各自负责不同的职责,但可以协同工作以构建高效、灵活的业务应用。在这个"struts2.3.2+hibernate4.3.8+spring4.1.5整合jar包"中,...

    spring-3.2.0+struts-2.3.4+hibernate-4.1.5

    本人的此jar合集只能保证SSH2的基础环境,至于更复杂的功能,可能此集合已经包含,或者您可以从您下载的Spring或者hibernate或者struts中寻找您需要的jar包,按要求添加即可。 【因本人能力有限,不能保证此jar集合...

    spring-3.2.0+struts-1.3.10+hibernate-4.1.5

    本人的此jar合集只能保证SSH2的基础环境,至于更复杂的功能,可能此集合已经包含,或者您可以从您下载的Spring或者hibernate或者struts中寻找您需要的jar包,按要求添加即可。 【因本人能力有限,不能保证此jar集合...

    Struts2.3.20Spring4.1.5Hibernate4.3.8整合

    关于目前最新S2SH版本的整合测试,Struts2.3.20、Spring4.1.5、Hibernate4.3.8

    ffmpeg-4.1.5.tar.bz2

    ffmpeg-4.1.5.tar.bz2

    SSH整合jar包--spring4.1.5 hibernate4.3.1 struts2.2.1

    SSH整合是Java Web开发中的一种常见技术组合,指的是Spring、Struts2和Hibernate这三大框架的集成应用。这个压缩包包含的是Spring 4.1.5、Hibernate 4.3.1和Struts2.2.1的核心库,用于帮助开发者快速搭建基于这些...

    spring 4.1.5 官方lib包

    2. **Spring Beans**:定义了如何创建、配置和管理应用中的bean。XML和Java配置都是可选的,4.1.5版本对这两种方式都进行了优化。 3. **Spring AOP**:提供了面向切面编程的支持,可以创建可重用的切面来封装系统级...

    artdialog4.1.5

    "artdialog4.1.5" 是一个知名的JavaScript弹出对话框插件,它为Web开发者提供了方便且可自定义的对话框解决方案。这个版本(4.1.5)是artdialog...通过深入学习和实践,开发者可以充分利用其功能,提升网站的用户体验。

    mapper-4.1.5.jar

    tk.mabatis的jar包 4.1.5版本。可参考以下方式使用 &lt;tk-mapper.version&gt;4.1.5 &lt;pagehelper.version&gt;1.2.10 &lt;!-- Mybatis通用Mapper --&gt; &lt;groupId&gt;tk.mybatis &lt;artifactId&gt;mapper ${tk-...

    httpasyncclient-4.1.5-API文档-中文版.zip

    赠送jar包:httpasyncclient-4.1.5.jar; 赠送原API文档:httpasyncclient-4.1.5-javadoc.jar; 赠送源代码:httpasyncclient-4.1.5-sources.jar; 赠送Maven依赖信息文件:httpasyncclient-4.1.5.pom; 包含翻译后...

    Spring4.1.5-javadoc-api.rar

    数据访问与集成(Data Access/Integration)模块是Spring处理数据库操作的关键,包括JDBC、ORM(对象关系映射)支持如Hibernate、MyBatis等,以及JPA(Java Persistence API)。Spring MVC作为Web开发的一部分,提供...

    Spring Framework4.1.5

    4.1.5版本对JPA和Hibernate的支持更加完善,事务管理也更为智能化,可以更好地适应各种数据库环境。 四、Web层 Spring的Web MVC框架在4.1.5版本中提供了对RESTful风格的支持,包括JSON绑定和HTTP方法约束。同时,它...

    eLabsim4.1.5.exe

    eLabsim4.1.5.exe

    hibernate-release-5.4.15.Final.zip

    2. **持久化模型**:Hibernate支持JPA(Java Persistence API)标准,提供了Entity、EntityManager和EntityManagerFactory等接口,用于处理对象的创建、查询和删除。 3. **查询语言HQL**:Hibernate Query Language...

    zeromq-4.1.5.zip

    zeromq-4.1.5.zip ZeroMQ \zero-em-queue\, \ØMQ\:  Ø  Connect your code in any language, on any platform.  Ø  Carries messages across inproc, IPC, TCP, TIPC, multicast.  Ø  Smart patterns ...

Global site tag (gtag.js) - Google Analytics