引自:http://www.cnblogs.com/hongten/archive/2011/07/20/2111773.html

进入:http://www.hibernate.org

说明文档:

英文:http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html_single/

中文:http://docs.jboss.org/hibernate/annotations/3.4/reference/zh_cn/html_single/

下载:hibernate annotation 3.4.0 GA

得到:hibernate-annotations.jar

   hibernate-commons-annotation.jar

   ejb3-persistence.jar

数据库:mysql

category表:id,name,description  
    id

product表:id,name ,price, description ,category_id
     
     
     id
 category_id

新建java project项目:

Add Hibernate Capabilities

hibernate.cfg.xml

 1

 2

 3    
     
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"

 4    
     
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

 5 

 6

 7

 8 

 9  

10  

11  
 org.hibernate.dialect.MySQLDialect

12  

13  

14  
 jdbc:mysql://localhost:3307/users

15  

16   root

17   root

18  

19  
 com.mysql.jdbc.Driver

20  

21  

22    mysqlusers

23  

24   true

25   true

26   thread

27  

28  

29 

30  

31 

32

利用Hibernate的逆向工程生成:

Category.java    
 and    
     
Product.java   

Category.java

 1 package com.b510.examples;

 2 

 3 import java.util.HashSet;

 4 import java.util.Set;

 5 

 6 // 标准注解

 7 

 8 import
javax.persistence.CascadeType;

 9 import javax.persistence.Column;

10 import javax.persistence.Entity;

11 import javax.persistence.FetchType;

12 import javax.persistence.GeneratedValue;

13 import javax.persistence.Id;

14 import javax.persistence.OneToMany;

15 import javax.persistence.Table;

16 

17 //增加的注解

18 

19 import org.hibernate.annotations.GenericGenerator;

20 

21 //当前的类是一个持久化类,是Category这个类。他映射了一个表category。所对应的
数据库是users

22 //这句:@Table(name = "category", catalog = "users")
 可以省略

23 @Entity

24 @Table(name = "category", catalog = "users")

25 

26 public class Category implements java.io.Serializable
{

27 

28  private static final long
serialVersionUID = 3240281547213597385L;

29  private Integer id;

30  private String name;

31  private String description;

32  private Set products = new
HashSet(0);

33 

34  

35  public Category() {

36  }

37 

38  public Category(String name, String
description, Set products) {

39   this.name = name;

40   this.description = description;

41   this.products = products;

42  }

43 

44  // 主键 :@Id  
 主键生成方式:strategy = "increment"

45  //映射表中id这个字段,不能为空,并且是唯一的

46  @GenericGenerator(name = "generator",
strategy = "increment")

47  @Id

48  @GeneratedValue(generator =
"generator")

49  @Column(name = "id", unique = true,
nullable = false)

50  public Integer getId() {

51   return this.id;

52  }

53 

54  public void setId(Integer id) {

55   this.id = id;

56  }

57 

58  //映射表中name这个字段 ,长度是500

59  @Column(name = "name", length =
500)

60  public String getName() {

61   return this.name;

62  }

63 

64  public void setName(String name) {

65   this.name = name;

66  }

67  

68  //映射表中description这个字段 ,长度是500

69  @Column(name = "description", length =
500)

70  public String getDescription() {

71   return this.description;

72  }

73 

74  public void setDescription(String
description) {

75   this.description = description;

76  }

77 

78  //级联操作:cascade = CascadeType.ALL

79  //延迟加载:fetch = FetchType.LAZY

80  //映射:mappedBy = "category"

81  //一对多方式

82  @OneToMany(cascade = CascadeType.ALL,
fetch = FetchType.LAZY, mappedBy = "category")

83  public Set getProducts() {

84   return this.products;

85  }

86 

87  public void setProducts(Set products)
{

88   this.products = products;

89  }

90 

91 }

Product.java

 1 package com.b510.examples;

 2 

 3 import javax.persistence.Column;

 4 import javax.persistence.Entity;

 5 import javax.persistence.FetchType;

 6 import
javax.persistence.GeneratedValue;

 7 import javax.persistence.Id;

 8 import javax.persistence.JoinColumn;

 9 import javax.persistence.ManyToOne;

10 import javax.persistence.Table;

11 import org.hibernate.annotations.GenericGenerator;

12 

13 

14 @Entity

15 @Table(name = "product", catalog = "users")

16 public class Product implements java.io.Serializable
{

17 

18  private static final long
serialVersionUID = -1546206493725028472L;

19  private Integer id;

20  private Category category;

21  private String name;

22  private String price;

23  private String descripton;

24 

25  

26  public Product() {

27  }

28 

29  public Product(Category category, String
name, String price,

30    String descripton)
{

31   this.category = category;

32   this.name = name;

33   this.price = price;

34   this.descripton = descripton;

35  }

36  

37  @GenericGenerator(name = "generator",
strategy = "increment")

38  @Id

39  @GeneratedValue(generator =
"generator")

40  @Column(name = "id", unique = true,
nullable = false)

41  public Integer getId() {

42   return this.id;

43  }

44 

45  public void setId(Integer id) {

46   this.id = id;

47  }

48 

49  //延迟加载:多对一方式

50  //关联信息:外键name = "category_id"

51  @ManyToOne(fetch = FetchType.LAZY)

52  @JoinColumn(name = "category_id")

53  public Category getCategory() {

54   return this.category;

55  }

56 

57  public void setCategory(Category
category) {

58   this.category = category;

59  }

60 

61  @Column(name = "name", length =
500)

62  public String getName() {

63   return this.name;

64  }

65 

66  public void setName(String name) {

67   this.name = name;

68  }

69 

70  @Column(name = "price", length =
10)

71  public String getPrice() {

72   return this.price;

73  }

74 

75  public void setPrice(String price)
{

76   this.price = price;

77  }

78 

79  @Column(name = "descripton", length =
500)

80  public String getDescripton() {

81   return this.descripton;

82  }

83 

84  public void setDescripton(String
descripton) {

85   this.descripton = descripton;

86  }

87 

88 }

测试代码:

HibernateTest.java

 1

 4 package com.b510.examples;

 5 

 6 import java.util.Set;

 7 

 8 import org.hibernate.Session;

 9 import org.hibernate.SessionFactory;

10 import org.hibernate.cfg.AnnotationConfiguration;

11 import org.hibernate.cfg.Configuration;

12 

13

20 public class HibernateTest {

21 

22  public static void main(String[] args)
{

23   HibernateTest test=new
HibernateTest();

24   test.add();

25   test.find();

26  }

27  public void add(){

28  Configuration config=new
AnnotationConfiguration();

29  config.configure();

30  SessionFactory
sessionFactory=config.buildSessionFactory();

31  Session
session=sessionFactory.getCurrentSession();

32  session.beginTransaction();

33  Category
c=(Category)session.get(Category.class, 5);

34  

35  Product p=new Product();

36  p.setName("计算机科学与技术");

37  p.setPrice("123");

38
 p.setDescripton("计算机科学与技术,好啊,真是红啊");

39  

40  p.setCategory(c);

41  c.getProducts().add(p);

42  

43  session.save(p);

44  session.getTransaction().commit();

45  }

46  

47  

48  public void find(){

49   Configuration config=new
AnnotationConfiguration();

50   config.configure();

51   SessionFactory
sessionFactory=config.buildSessionFactory();

52   Session
session=sessionFactory.getCurrentSession();

53   session.beginTransaction();

54   Category
c=(Category)session.get(Category.class, 5);

55  
 System.out.println("id: "+c.getId()+"
 name:"+c.getName());

56    Set
p=c.getProducts();

57    for(Product
product:p){

58    
System.out.println("id:"+product.getId()+"
 name:"+product.getName()+"
 description:"+product.getDescripton());

59    }

60  
 session.getTransaction().commit();

61  }

62 }

运行效果:

 1 log4j:WARN No appenders could be found for
logger (org.hibernate.cfg.annotations.Version).

 2 log4j:WARN Please initialize the log4j
system properly.

 3 Hibernate: 

 4    
select

 5    
    category0_.id as
id1_0_,

 6    
    category0_.description as
descript2_1_0_,

 7    
    category0_.name as
name1_0_ 

 8    
from

 9    
    users.category
category0_ 

10     where

11      
  category0_.id=?

12 Hibernate: 

13     select

14      
  products0_.category_id as category5_1_,

15      
  products0_.id as id1_,

16      
  products0_.id as id0_0_,

17      
  products0_.category_id as category5_0_0_,

18      
  products0_.descripton as descripton0_0_,

19      
  products0_.name as name0_0_,

20      
  products0_.price as
price0_0_ 

21     from

22      
  users.product products0_ 

23     where

24      
  products0_.category_id=?

25 Hibernate: 

26     select

27      
  max(id) 

28     from

29      
  product

30 Hibernate: 

31    
insert 

32     into

33      
  users.product

34      
  (category_id, descripton, name, price,
id) 

35     values

36      
  (?, ?, ?, ?, ?)

37 Hibernate: 

38     select

39      
  category0_.id as id5_0_,

40      
  category0_.description as descript2_5_0_,

41      
  category0_.name as
name5_0_ 

42     from

43      
  users.category
category0_ 

44     where

45      
  category0_.id=?

46 id: 5  name:xml33

47 Hibernate: 

48     select

49      
  products0_.category_id as category5_1_,

50      
  products0_.id as id1_,

51      
  products0_.id as id4_0_,

52      
  products0_.category_id as category5_4_0_,

53      
  products0_.descripton as descripton4_0_,

54      
  products0_.name as name4_0_,

55      
  products0_.price as
price4_0_ 

56     from

57      
  users.product products0_ 

58     where

59      
  products0_.category_id=?

60 id:9  name:计算机科学与技术
 description:计算机科学与技术,好啊,真是红啊

 

分类: Hibernate

标签: java, hibernate, annotation, 注解

绿色通道: 好文要顶 关注我 收藏该文与我联系 

Hongten

关注 - 19

粉丝 - 131

+加关注

5 0

(请您对文章做出评价)

« 博主上一篇:HIbernate的“1+N”问题

» 博主下一篇:Hibernate EntityManager

posted @ 2011-07-20 16:21 Hongten 阅读(25647) 评论(3) 编辑 收藏


版权声明:本文为博主原创文章,未经博主允许不得转载。

Hibernate Annotation (…的更多相关文章

  1. Hibernate的Annotation注解

    当项目变得比较大的时候,如何还使用hbm.xml文件来配置Hibernate实体就会变得比较复杂.这里Hibernate提供了Annotation注解方式,使得Hibernate的映射文件变得很方便管 ...

  2. Spring的annotation用在set方法上 hibernate的annotation用get方法上

    1.Spring的annotation用在set方法上 2.hibernate的annotation用在get方法上

  3. Hibernate之Annotation(注解的方式,非映射)

    在hibernate 3.0之后,可以建立一个符合JPA标准的Annotation,以hibernate3.3.2GA为例 Annotation 以 hibernate Annotation 3.3. ...

  4. Hibernate学习笔记2.2(Hibernate基础Annotation配置)

    如果数据库表名与类名不一致 可以用使用 @Table(name="_teacher") 来指定表名,没有就会自己创建 也可以在配置文件上修改 为class添加table属性 如果什 ...

  5. hibernate spring annotation setup

    First step setup for the pom.xml with hibernate dependency , hibernate dependency need to before the ...

  6. Hibernate常用Annotation标签说明

    @ javax.persistence.Entity 实体类定义,该标签表示当前类是一个Hibernate的数据库实体,对应着数据库中的某个表 位置:用于类级别 参数:无 样例:@Entity 注意: ...

  7. Hibernate Annotation笔记

    (1)简介:在过去几年里,Hibernate不断发展,几乎成为Java数据库持久性的事实标准.它非常强大.灵活,而且具备了优异的性能.在本文中,我们将了解如何使用Java 5 注释来简化Hiberna ...

  8. hibernate中增加annotation @后不提示信息【转】

    此文转自:http://blog.knowsky.com/252047.htm 所需要用到的3个jar包分别是: hibernate-annotations.jar ejb3-persistence. ...

  9. Hibernate Annotation (Hibernate 注解)

    简介: 传统上,Hibernate的配置依赖于外部 XML 文件:数据库映射被定义为一组 XML 映射文件,并且在启动时进行加载. 然而现在借助新的 Hibernate   Annotation 库, ...

随机推荐

  1. POJ2594 Treasure Exploration[DAG的最小可相交路径覆盖]

    Treasure Exploration Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 8301   Accepted: 3 ...

  2. 八大排序的python实现

    以下是八大排序的python实现,供以后参考,日后扩展 一.插入排序 #-*- coding:utf-8 -*- ''' 描述 插入排序的基本操作就是将一个数据插入到已经排好序的有序数据中,从而得到一 ...

  3. EasyNVR无插件流媒体服务器前端技术防止重复提交的方法

    现在随着接触EasyNVR时间越来越长,越发的觉得EasyNVR真的是一个"神器".从功能上来说自身不仅可以拉出来使用(具体功能搜索EasyNVR一定有惊喜!),也可以作为设备端与 ...

  4. static 不被实例调用

    static - JavaScript | MDN https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/ ...

  5. await 暂停 等待 暂停的是什么

    体验异步的终极解决方案-ES7的Async/Await var sleep = function (time) { return new Promise(function (resolve, reje ...

  6. 【题解】P4799[CEOI2015 Day2]世界冰球锦标赛

    [题解][P4799 CEOI2015 Day2]世界冰球锦标赛 发现买票顺序和答案无关,又发现\(n\le40\),又发现从后面往前面买可以通过\(M\)来和从前面往后面买的方案进行联系.可以知道是 ...

  7. 爬虫之重要的requests模块

    一 . requests模块 什么是requests模块 requests模块是python中原生的基于网络请求的模块,其主要作用是用来模拟浏览器发起请求.功能强大,用法简洁高效.在爬虫领域中占据着半 ...

  8. ansible3

    一.setup模块 ansible的setup模块主要用来收集信息,查看参数: [root@localhost ~]# ansible-doc -s setup # 查看参数,部分参数如下: filt ...

  9. 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛 G 旋转矩阵 【模拟】

    链接:https://www.nowcoder.com/acm/contest/90/G 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言65536 ...

  10. Hadoop- Hadoop详解

    首先所有知识以官网为准,所有的内容在官网上都有展示,所有的变动与改进,新增内容都以官网为准.hadoop.apache.org Hadoop是一个开源的可拓展的分布式并行处理计算平台,利用服务器集群根 ...