hibernate annotation 之 注解声明
@Entity
将一个 POJO 类注解成一个实体 bean ( 持久化 POJO 类 )
@Table
为实体 bean 映射指定具体的表,如果该注解没有被声明,系统将使用默认值 ( 即实体 bean 不带包名的短类名 )
@Id
将实体bean中的某个属性定义为标识符 ( identifier )
@GeneratedValue
该注解可以定义该标识符的生成策略 ( 默认是 AUTO 策略 ) :
AUTO — 可以是 IDENTITY,或 SEQUENCE 或 TABLE 类型,这取决于不同的底层数据库。
TABLE — 使用表保存id值
IDENTITY — 自然递增
SEQUENCE — 序列
@Transient
被注解成 @Transient 的 getter 方法或属性,将不会被持久化,hibernate 会忽略这些字段和属性。
@Basic
所有没有定义注解的属性,等价于在其上面添加了 @Basic 注解.。通过 @Basic注解可以声明属性的获取策略 ( fetch strategy )
@Temporal
在核心的 Java API 中并没有定义时间精度 ( temporal precision )。因此处理时间类型数据时,你还需要定义将其存储在数据库中所预期的精度。
在数据库中,表示时间类型的数据有 DATE,TIME,和 TIMESTAMP 三种精度 ( 即单纯的日期,时间,或者两者兼备 )。 可使用 @Temporal 注解来调整精度。
@Column
将实体 bean 中的属性映射到表中的列。
@Column(
name = "columnName"; (1)
boolean unique() default false; (2)
boolean nullable() default true; (3)
boolean insertable() default true; (4)
boolean updatable() default true; (5)
String columnDefinition() default ""; (6)
String table() default ""; (7)
int length() default 255; (8)
int precision() default 0; (9)
int scale() default 0; (10)
(1) name 可选,列名(默认值是属性名)
(2) unique 可选,是否在该列上设置唯一约束(默认值false)
(3) nullable 可选,是否设置该列的值可以为空(默认值true)
(4) insertable 可选,该列是否作为生成的insert语句中的一个列(默认值true)
(5) updatable 可选,该列是否作为生成的update语句中的一个列(默认值true)
(6) columnDefinition 可选,为这个特定列覆盖SQL DDL片段 (这可能导致无法在不同数据库间移植)
(7) table 可选,定义对应的表(默认为主表)
(8) length 可选,列长度(默认值255)
(9) precision 可选,列十进制精度(decimal precision)(默认值0)
(10) scale 可选,如果列十进制数值范围(decimal scale)可用,在此设置(默认值0)
环境 : JDK 1.6,eclipse 3.6,maven 3.0.4,hibernate 3.3.2,junit 4.7,mysql 5.1
pom.xml 清单   
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.fancy</groupId>
<artifactId>hibernate_annotation</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>hibernate_annotation Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<!-- Hibernate framework -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.3.2.GA</version>
</dependency>
<!-- Hibernate Dependency Start -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.9.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.4.0.GA</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.5.8</version>
</dependency>
<!-- Hibernate Dependency End -->
<!-- mysql driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.17</version>
</dependency>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>hibernate_annotation</finalName>
</build>
</project>
注 : 此处配置 pom.xml 是使用 maven 来管理 jar 包,如果你没有使用 maven,则需手动导入相关 jar 包。
实体 bean
package net.yeah.fancydeepin.po;
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 javax.persistence.Transient;
import java.io.Serializable;
/**
* -----------------------------------------
* @描述 实体类
* @作者 fancy
* @邮箱 fancydeepin@yeah.net
* @日期 2012-10-12 <p>
* -----------------------------------------
*/
@Entity
@Table(name = "user")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
/**
* ID,主键
*/
private Integer id;
/**
* 用户名
*/
private String name;
/**
* 昵称
*/
private String nickName;
/**
* 邮箱地址
*/
private String email;
/**
* 注册日期时间
*/
private Date registerDate;
/**
* 最近登录时间
*/
private Date recentLoginTime;
/**
* 上一次登录时间
*/
private Date lastLoginDay;
@Id
@GeneratedValue
public Integer getId() {
return id;
}
@Column(length = 18, nullable = false)
public String getName() {
return name;
}
@Transient
public String getNickName() {
return nickName;
}
@Column(name = "mail", length = 40, nullable = false)
public String getEmail() {
return email;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false)
public Date getRegisterDate() {
return registerDate;
}
@Temporal(TemporalType.TIME)
public Date getRecentLoginTime() {
return recentLoginTime;
}
@Temporal(TemporalType.DATE)
public Date getLastLoginDay() {
return lastLoginDay;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public void setEmail(String email) {
this.email = email;
}
public void setRegisterDate(Date registerDate) {
this.registerDate = registerDate;
}
public void setRecentLoginTime(Date recentLoginTime) {
this.recentLoginTime = recentLoginTime;
}
public void setLastLoginDay(Date lastLoginDay) {
this.lastLoginDay = lastLoginDay;
}
}
注 : 注解可以是在属性或 getter 方法上进行声明,但不建议混合使用这两种声明方式,相反,应该尽量避免。另外,由 static 修饰的属性不会被持久化到数据库。
hibernate.cfg.xml 清单   
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/temp</property>
<property name="connection.username">username</property>
<property name="connection.password">password</property>
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping class="net.yeah.fancydeepin.po.User"/>
</session-factory>
</hibernate-configuration>
Junit Test    
package junit.test;
import java.util.Date;
import net.yeah.fancydeepin.po.User;
import org.hibernate.Session;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* -----------------------------------------
* @描述 Junit Test
* @作者 fancy
* @邮箱 fancydeepin@yeah.net
* @日期 2012-10-12 <p>
* -----------------------------------------
*/
public class TestApp {
private static Session session = null;
@BeforeClass
public static void beforeClass() throws Exception {
session = new AnnotationConfiguration().configure().buildSessionFactory().getCurrentSession();
}
@Test
public void createTable(){
new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);
}
@Test
public void insert(){
User user = new User();
Date date = new Date();
user.setName("fancy");
user.setEmail("fancydeepin@yeah.net");
user.setRegisterDate(date);
user.setRecentLoginTime(date);
user.setLastLoginDay(date);
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
}
}
在 Junit 测试类中执行建表方法 createTable,后台打印输出的 SQL 语句 :
drop table if exists user
create table user (
id integer not null auto_increment,
name varchar(18) not null,
mail varchar(40) not null,
registerDate datetime not null,
recentLoginTime time,
lastLoginDay date,
primary key (id)
)
数据库中生成的表结构 :

在 Junit 测试类中执行插入数据的方法 insert,后台打印输出的 SQL 语句 :
Hibernate: 
    insert 
    into
        user
        (mail, lastLoginDay, name, recentLoginTime, registerDate) 
    values
        (?, ?, ?, ?, ?)
数据库中的数据 :

hibernate annotation 之 注解声明的更多相关文章
- Rhythmk 学习 Hibernate 07 - Hibernate annotation 实体注解
		参考: http://docs.jboss.org/hibernate/annotations/3.4/reference/zh_cn/html_single/ 1.系统配置: 可以通过使用 map ... 
- hibernate Annotation 以及注解版的数据关联 4.4
		目的是不写xxx.hbm.xml映射文件,使用注解 主配置文件还是要有hibernate.cfg.xml <?xml version="1.0" encoding=" ... 
- 转Hibernate Annotation mappedBy注解理解
		在Annotation 中有这么一个@mappedBy 属性注解,相信有些同学还是对这个属性有些迷惑,上网找了些理解@mappedBy比较深刻的资料,下面贴出来供大家参考. http://xiaoru ... 
- hibernate Annotation 以及注解版的数据关联
		目的是不写xxx.hbm.xml映射文件,使用注解 主配置文件还是要有hibernate.cfg.xml <?xml version="1.0" encoding=" ... 
- Rhythmk 学习 Hibernate 08 - Hibernate annotation 关联关系注解
		1.一对一 (One to One) 共三种情况: 1.1 主键共享 1.2 外键共享 1.3 中间表关联 1.1 code: @Entity public class arti ... 
- hibernate annotation注解方式来处理映射关系
		在hibernate中,通常配置对象关系映射关系有两种,一种是基于xml的方式,另一种是基于annotation的注解方式,熟话说,萝卜青菜,可有所爱,每个人都有自己喜欢的配置方式,我在试了这两种方式 ... 
- Hibernate Annotation (Hibernate 注解)
		简介: 传统上,Hibernate的配置依赖于外部 XML 文件:数据库映射被定义为一组 XML 映射文件,并且在启动时进行加载. 然而现在借助新的 Hibernate Annotation 库, ... 
- Hibernate Annotation笔记
		(1)简介:在过去几年里,Hibernate不断发展,几乎成为Java数据库持久性的事实标准.它非常强大.灵活,而且具备了优异的性能.在本文中,我们将了解如何使用Java 5 注释来简化Hiberna ... 
- Hibernate实体类注解
		常用的hibernate annotation标签如下: @Entity --注释声明该类为持久类.将一个Javabean类声明为一 个实体的数据库表映射类,最好实现序列化.此时,默认情况下,所有的类 ... 
随机推荐
- ps去除元素的三种常用方法
			1.仿制图章工具,alt+鼠标左键进行选取复制区域,然后左键点击需要覆盖的区域. 2.套锁工具--选择区域--右键填充--内容识别. 3.修补工具,选中区域--拖动适配. 附带另一份较 ... 
- canvas画布导出图片并下载
			近期在学习关于画布知识,关于 画布导出图片, 在导出jpeg格式的图片时,会发现图片背景色变成了黑色,原因是画布透明的地方 默认转成了黑色,可以在绘制画布前设置透明处背景色为白色. // 背景色转换成 ... 
- Win7如何部署定制的Quicklaunch图标
			在严格的网络管理环境中,最终用户的权限被限制得比较严格,被禁止随意改变系统的行为.在我们的网络环境中,学生是被禁止添加/删除QuickLaunch上的图标的,不仅如此,无线网络,打印机等等,都受到严格 ... 
- NOIp2018集训test-9-5(pm)
			老张说:这套题太简单啦,你们最多两个小时就可以AK啦! 题 1 数数 我看到T1就懵了,这就是老张两个小时可以AK的题的T1?? 然后我成功地T1写了1h+,后面1h打了t2.t3暴力,就很开心. 等 ... 
- NX二次开发-遍历当前part所有component,把装配子部件设置成工作部件
			NX11+VS2013 #include <uf.h> #include <uf_disp.h> #include <uf_modl.h> #include < ... 
- iOS 获取音频或是视频的时间
			AVURLAsset* audioAsset =[AVURLAssetURLAssetWithURL:audioFileURL options:nil]; CMTime audioDuration = ... 
- cordova 与 phonegap关系
			Apache Cordova是PhoneGap贡献给Apache后的开源项目,是从PhoneGap中抽出的核心代码,是驱动PhoneGap的核心引擎.PhoneGap是一个开源的开发框架,使用HTML ... 
- Python-爬虫-爬取知乎的标题和当页显示的文字
			# coding:utf-8 import requests from bs4 import BeautifulSoup quesNumStr = str(input("请输入搜索关键字:& ... 
- Python3 From Zero——{最初的意识:004~迭代器和生成器}
			一.反向迭代:reversed() >>> a [1, 2, 3, 4] >>> for x in reversed(a): ... print(x, end=' ... 
- Day15:Python 【模块】及__name__:
			什么是模块: 在Python中,随着这代码的撰写,代码越来越长,所以产生了,模块这个概念,模块是什么?模块就是一个.py文件,在撰写代码时,我们把不同的功能的代码封装到一个.py文件里,用得时候导入 ... 
