hiberante 的 annotation历史:

在hibernate3以后,开始支持Annotation;

先有hiberante再有JPA,有了JPA标准之后,hibernate写了Annotation来支持JPA;
所以 hibernate的annotation是JPA标准之下的,一般都直接用JPA的annotation,hibernate的annotation只有在极少的情况下才使用。

1、创建teacher表,create table teacher(id int primary key, name varchar(20),title varchar(10));
2、创建 Teacher 类

3、在 hibernate lib 中加入 annotation 的 jar 包

在 hibernate 这个 User Libaray 中添加支持annotation的jar包:

  a) hibernate annotations jar


  b) ejb3 persistence jar
  c) hibernate common annotations jar
  d) 注意文档中没有提到 hibernate common annotations jar 文件

4、参考 Annotation 文档建立对应的注解

为 Teacher 类添加注解

 package com.bjsxt.hibernate.model;

 import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table; @Entity
@Table(name="teacher")
public class Teacher { private Integer id; private String name; private String title; @Id
public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getTitle() {
return title;
} public void setTitle(String title) {
this.title = title;
} }

注意:类属性的注解一般都写在该属性的get方法上,约定俗成:如id属性的注解写在getId()上。

5、在 hibernate.cfg.xml 建立对应映射 <mapping class=... />

  Teacher的映射:<mapping class="com.bjsxt.hibernate.model.Teacher" />

6、参考文档进行测试(注意文档中缺少 configure()的小bug)

 import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration; import com.bjsxt.hibernate.model.Teacher; public class TeacherTest { public static void main(String[] args) {
Teacher t = new Teacher();
t.setId(1);
t.setName("t1");
t.setTitle("中级"); //改为new AnnotationConfiguration()
Configuration cfg = new AnnotationConfiguration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession();
session.beginTransaction(); session.save(t); session.getTransaction().commit();
session.close();
sf.close();
} }

注意 Configuration cfg = new AnnotationConfiguration();

链接: http://pan.baidu.com/s/1eSvHWbg 密码: 63cy

所需jar包:链接: http://pan.baidu.com/s/1dEDKHln 密码: s4bq

FAQ:@不给提示
a) content assist - activation - 加上@,具体参考:http://www.cnblogs.com/ShawnYang/p/6692735.html

Annotation版本的HelloWorld的更多相关文章

  1. 第二个hibernate Annotation版本的helloworld

    经过第一次的 hibernate  我发现每一个数据库表都对应了一个类,并且每一个类都要新建一个文件进行配置 很麻烦!  于是便出现了Annotation版本的hibernate. 具体如下: 1.同 ...

  2. Hibernate学习笔记1.2(Annotation版本的Helloworld)

    hibernate 3.0之后开始支持Annotation 接着1.1的项目 首先 需要创建model Teacher.java. package com.hw.hibernate.model; pu ...

  3. Hibernate---第一个helloworld程序 (XML版本, annotation版本)

    Hibernate作为JPA的一种实现,jpa的注解已经是hibernate的核心,hibernate只提供了一些补充,而不是两套注解.hibernate对jpa的支持够足量,在使用hibernate ...

  4. EJB2.0版本的HelloWorld

    EJB2.0版本的HelloWorld   虽然EJB3.1已经出来了,可是EJB2.0的项目还需要维护啊.下面写个简单EJB2.0的HelloWorld程序,练练手.   环境: JBoss 4.0 ...

  5. 汇编2.汇编版本的helloworld

    寻址方式 立即数寻址 寄存器寻址 存储器寻址 直接寻址 : mov ax, [ 01000h ]; 直接在[]内给出一个内存地址 寄存器间接寻址: mov ax ,[si]; 在[]以寄存器的值给出内 ...

  6. JDK-windows7环境变量配置-亲测版本 以及HelloWorld

    1.下载并安装jdk,假设安装1.6.0_45版本到C:\Program Files\Java,则安装完毕后,目录结构为: C:\PROGRAM FILES\JAVA├─jdk1.6.0_45│ ├─ ...

  7. Go语言版本的helloworld

    新建一个project,然后建立一个main目录,在main目录下新建一个go类文件:main.go 内容如下: package main import "fmt" func ma ...

  8. 汇编语言版本的HelloWorld

    平台 macOS 工具 nasm clang 文件 main.asm extern _printf ; 这里调用系统的一个系统调用函数, _printf, 使用extern告诉链接器该label在其他 ...

  9. 马士兵hibernate(原始笔记)

    马士兵hibernate(原始笔记) 课程内容 1        HelloWorld a)   Xml b)   annotation 2        Hibernate原理模拟 - 什么是O/R ...

随机推荐

  1. CF1076C Meme Problem 数学

    Try guessing the statement from this picture: You are given a non-negative integer d . You have to f ...

  2. mybatis和jdbc分析

    从这个图上可以看出mybatis的整体执行图 jdbc的 mybatis对很多类型进行了转化,减少了开发的量

  3. 架构师 AI 技术

    架构师是大忽悠吗?阿里技术大牛告诉你真相! - huangshulang1234的博客 - CSDN博客https://blog.csdn.net/huangshulang1234/article/d ...

  4. UVA1714 Keyboarding

    传送门 坑很多的一题 这里要感谢crk大佬提前帮我把所有的坑都踩了一遍...233 讲一下题目的意思: 给你一个神奇的 r*c 的键盘 (r,c<=50) 上面有大写的字母,数字,' - '号 ...

  5. hdu6446 Tree and Permutation 2018ccpc网络赛 思维+dfs

    题目传送门 题目描述:给出一颗树,每条边都有权值,然后列出一个n的全排列,对于所有的全排列,比如1 2 3 4这样一个排列,要算出1到2的树上距离加2到3的树上距离加3到4的树上距离,这个和就是一个排 ...

  6. C#工具类之字符串扩展类

    /// <summary> /// 字典串帮忙类 /// </summary> public static class StringHelper { /// <summa ...

  7. IntelliJ Save Action

    https://blog.csdn.net/hustzw07/article/details/82824713

  8. sqlserver 事务日志

    预写式日志(Write-Ahead Logging (WAL)) --在数据写入到数据库之前,先写入到日志. 1.”Begin Tran”记录  -> 缓冲区 2. 日志             ...

  9. win7旗舰版+caffe+vs2013+matlab2014b(无GPU版)

    参考网站: http://www.cnblogs.com/njust-ycc/p/5776286.html 无法找到gpu/mxGPUArray.h: No such file or director ...

  10. PHP rand 和 mt_rand

    PHP mt_rand() 函数 定义和用法 mt_rand() 使用 Mersenne Twister 算法返回随机整数. 语法 mt_rand(min,max) 说明 如果没有提供可选参数 min ...