今天有时间大体看了一下Hibernate Annotations,决定放弃xdoclet生成hbm的想法,开始学习annotation.
首先大体的来看一下:
一:Class Level:
1:通过@Entity声明当前pojo为实体Bean
2:通过@Table定义对应数据库表的属性
name:表名,默认实体类名
uniqueConstraints:定义表的唯一性约束
例:定义表名为tb_user,并且用户名唯一,不能重复










二:Method Level:
@Basic
@Transient
@Column
所有非static非transient都会被持久化,也就是说所有加@Transient是不会被保存到数据库中的,所有没有加注的默认为@Basic,通过Column我们可以更一步的定义列名,是否为空,长度,是否可更新等等属性值,
例:













password加注为@Transient,所以通过SchemaExport导入Schema,查看表结构是没有这个字段的。
@Temporal
日期类型,分三种,Time,Date,Timestamp
例:




@Lob
1:所有的Clob,Character,char,String都会被转为Clob
2:所有的Blob,Byte[],byte[],serializable都会被转为Blob
例:









@Id
就是标识了,暂时还没有详读文档,看了一下test
最简单方式如下:





我看了一下mysql生成的sql


应该是hibernate根据数据库类型自动选择的(说了一句废话

当然我们可以根据我们需要选择其他的生成方式,例如常用的uuid






@Version
乐观锁
三:跑起来
和Hibernate官方网站一样,来个HibernateUtil

/**
* @author martin
*/
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
AnnotationConfiguration cfg =
new AnnotationConfiguration().addAnnotatedClass(User.class);
new SchemaExport(cfg).execute(true, true, false, true);
sessionFactory = cfg.buildSessionFactory();
}
public static Session getSession() {
return sessionFactory.openSession();
}
}
* @author martin
*/
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
AnnotationConfiguration cfg =
new AnnotationConfiguration().addAnnotatedClass(User.class);
new SchemaExport(cfg).execute(true, true, false, true);
sessionFactory = cfg.buildSessionFactory();
}
public static Session getSession() {
return sessionFactory.openSession();
}
}
注意这里的 new AnnotationConfiguration()!
Test:
/**
* @author martin
*/
public class HibernateUtilTest extends TestCase {
public void testAddUser() {
User user = new User();
user.setUserName("martin");
save(user);
assertNotNull(user.getId());
}
public void testUpdateUser() {
Session session = HibernateUtil.getSession();
Query query =
session.createQuery("from User as user where user.userName=:name");
query.setParameter("name", "martin");
List list = query.list();
session.close();
assertEquals(1, list.size());
User user = (User) list.get(0);
user.setUserName("martin xus");
save(user);
assertEquals(new Integer(1), user.getVersion());
}
private void save(User user) {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(user);
session.flush();
tx.commit();
session.close();
}
}
* @author martin
*/
public class HibernateUtilTest extends TestCase {
public void testAddUser() {
User user = new User();
user.setUserName("martin");
save(user);
assertNotNull(user.getId());
}
public void testUpdateUser() {
Session session = HibernateUtil.getSession();
Query query =
session.createQuery("from User as user where user.userName=:name");
query.setParameter("name", "martin");
List list = query.list();
session.close();
assertEquals(1, list.size());
User user = (User) list.get(0);
user.setUserName("martin xus");
save(user);
assertEquals(new Integer(1), user.getVersion());
}
private void save(User user) {
Session session = HibernateUtil.getSession();
Transaction tx = session.beginTransaction();
session.saveOrUpdate(user);
session.flush();
tx.commit();
session.close();
}
}
创建的表结构为:











完整User代码(省掉了所有属性的setter):






































































四:next,明天的学习计划.
参考
Hibernate Annotations Refrence
相关推荐
1. **注解驱动**: Hibernate Annotations 允许开发者使用 Java 注解来定义实体类、属性、关联关系等,这些注解替代了传统的 XML 映射文件,使得代码更加简洁,更易于维护。 2. **实体注解**: `@Entity` 注解标记一...
在这个“hibernate 和hibernate_annotations(中文API)帮助文档”中,我们将深入探讨Hibernate的核心概念、特性以及如何使用Hibernate Annotations进行对象的持久化。 一、Hibernate核心概念 1. 实体(Entity):在...
《Hibernate Annotations 3.4.0.GA:深入理解与应用》 Hibernate是Java领域中广泛应用的对象关系映射(ORM)框架,它极大地简化了数据库操作,使得开发者可以使用面向对象的方式来处理数据。而Hibernate ...
现在已经不用*.hbm.xml这种映射文件了,都是用Annotation(注解)方式来完成实体与表之间的映射关系,这样看起来比用xml文件来映射更具有可读性,自我感觉以后Hibernate Annotation的映射方式将代替hibernate 的*....
Hibernate Annotations API 中文文档 前言 1. 创建一个注解项目 1.1. 系统需求 1.2. 系统配置 2. 实体Bean 2.1. 简介 2.2. 用EJB3注解进行映射 2.2.1. 声明实体bean 2.2.1.1. 定义表(Table) 2.2.1.2. 乐观...
**标题:“Hibernate Annotations”** **概述** Hibernate是一个流行的Java对象关系映射(ORM)框架,它简化了在Java应用程序中处理数据库的过程。Hibernate Annotations是Hibernate框架的一个重要组成部分,它...
webwork2.1.7 中文开发文档 Hibernate Annotations参考文档-3.20中文开发文档 Hibernate_3.2.0_Reference_zh_CN中文开发文档
### Hibernate Annotations 中文参考手册概览 #### 版本说明 根据提供的信息,这份文档的版本为3.2.0CR1。此版本标志着Hibernate框架中的一个特定阶段,特别是关于如何利用注解(Annotations)来简化Java对象与...
使用注解Annotation时用的jar包。 包括: hibernate-commons-annotations-3.3.0.ga.jar hibernate-entitymanager.jar ejb3-persistence.jar hibernate-annotations.jar
《Hibernate Annotations 3.4.0.GA API详解》 Hibernate是Java开发中广泛使用的对象关系映射(ORM)框架,它极大地简化了数据库操作,将数据库操作与业务逻辑解耦。Hibernate Annotations是Hibernate框架的一个重要...
1. **Hibernate Annotations介绍** Hibernate Annotations是Hibernate 3.x引入的一个模块,它允许开发者使用Java 5及以上版本的注解来定义对象关系映射,替代传统的XML配置文件。3.4.0.GA版本是该组件的一个稳定...
《Hibernate Annotations 3.4.0:持久化框架的注解增强详解》 Hibernate作为Java领域最著名的对象关系映射(ORM)框架之一,极大地简化了数据库操作。在3.4.0版本中,它引入了Annotations特性,使得Java开发者能够...
《Hibernate Annotations 框架详解》 在Java领域,Hibernate是一个广泛应用的对象关系映射(ORM)框架,它极大地简化了数据库操作。而`hibernate-annotations.jar`是Hibernate框架中的一个重要组件,主要负责处理...
1. 引入Hibernate和Hibernate Annotations相关jar包:包括hibernate-annotations.jar、hibernate-commons-annotations.jar、hibernate-entitymanager.jar以及JPA规范所需的jar包。 2. 创建实体类:在实体类上添加@...
1. **注解基础**:Hibernate Annotations 使用 Java 注解来标注实体类、属性、集合等,如 `@Entity` 表示一个数据库表,`@Table` 指定表名,`@Id` 定义主键,`@GeneratedValue` 管理主键生成策略。 2. **实体...
《Hibernate Annotations 中文文档》是针对Hibernate框架注解配置的详细指南,旨在帮助开发者更深入地理解和使用Hibernate的注解功能。Hibernate是一个流行的Java对象关系映射(ORM)框架,它允许开发人员将数据库...
《深入理解Hibernate 3.3.2与Annotations 3.4.0:核心特性与实践应用》 Hibernate作为Java领域中的一个强大ORM(对象关系映射)框架,为开发者提供了在Java应用程序中操作数据库的强大工具。它允许我们将数据库操作...
hibernate annotations 核心包,欢迎下载