`

Hibernate Annotation使用实例

阅读更多
jar说明  hibernate 3.3.0  hibernate-annotations(3.4.0.CR2)

数据库(oracle)结构

CREATE TABLE KANG_USER
(
  ID        VARCHAR2(32 BYTE)     NOT NULL,
  USERNAME  VARCHAR2(30 BYTE),
  PASSWORD  VARCHAR2(32 BYTE),
  CITY      VARCHAR2(32 BYTE),
  ADDTIME   DATE
)

CREATE TABLE KANG_CITY
(
  ID        VARCHAR2(32 BYTE),
  CITYNAME  VARCHAR2(20 BYTE)
)

一个用户表,一个城市表,用户和城市是多对一的关系

Java代码

   1. import java.util.Date; 
   2.  
   3. import javax.persistence.Entity; 
   4. import javax.persistence.GeneratedValue; 
   5. import javax.persistence.Id; 
   6. import javax.persistence.JoinColumn; 
   7. import javax.persistence.ManyToOne; 
   8. import javax.persistence.Table; 
   9. import javax.persistence.Temporal; 
  10. import javax.persistence.TemporalType; 
  11.  
  12. import org.hibernate.annotations.Cache; 
  13. import org.hibernate.annotations.CacheConcurrencyStrategy; 
  14. import org.hibernate.annotations.Cascade; 
  15. import org.hibernate.annotations.CascadeType; 
  16. import org.hibernate.annotations.GenericGenerator; 
  17. import org.hibernate.annotations.NotFound; 
  18. import org.hibernate.annotations.NotFoundAction; 
  19.  
  20. /**
  21.  * @author yukang
  22.  *
  23.  */ 
  24. @Entity 
  25. @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) 
  26. @Table(name="KANG_USER") 
  27. public class User { 
  28.      
  29.     @Id 
  30.     @GeneratedValue(generator = "system-uuid") 
  31.     @GenericGenerator(name = "system-uuid", strategy = "uuid") 
  32.     private String id; 
  33.      
  34.     private String userName; 
  35.      
  36.     private String passWord; 
  37.      
  38.     @Temporal(TemporalType.TIMESTAMP) 
  39.     private Date addTime; 
  40.      
  41.     @ManyToOne()  
  42.     @Cascade(value={CascadeType.PERSIST, CascadeType.MERGE})  
  43.     @JoinColumn(name="CITY") 
  44.     @NotFound(action=NotFoundAction.IGNORE) 
  45.     private City city; 
  46.      
  47.     public User() { 
  48.          
  49.     } 
  50.      
  51.      
  52.     /**
  53.      * @return
  54.      */ 
  55.     public String getId() { 
  56.         return id; 
  57.     } 
  58.      
  59.     /**
  60.      * @param id the id to set
  61.      */ 
  62.     public void setId(String id) { 
  63.         this.id = id; 
  64.     } 
  65.  
  66.     /**
  67.      * @return the userName
  68.      */ 
  69.     public String getUserName() { 
  70.         return userName; 
  71.     } 
  72.  
  73.  
  74.     /**
  75.      * @param userName the userName to set
  76.      */ 
  77.     public void setUserName(String userName) { 
  78.         this.userName = userName; 
  79.     } 
  80.  
  81.  
  82.     /**
  83.      * @return the passWord
  84.      */ 
  85.     public String getPassWord() { 
  86.         return passWord; 
  87.     } 
  88.  
  89.  
  90.     /**
  91.      * @param passWord the passWord to set
  92.      */ 
  93.     public void setPassWord(String passWord) { 
  94.         this.passWord = passWord; 
  95.     } 
  96.  
  97.     /**
  98.      * @return
  99.      */ 
100.     public Date getAddTime() { 
101.         return addTime; 
102.     } 
103.      
104.     /**
105.      * @param addTime the addTime to set
106.      */ 
107.     public void setAddTime(Date addTime) { 
108.         this.addTime = addTime; 
109.     } 
110.      
111.     /**
112.      * @return
113.      */ 
114.     public City getCity() { 
115.         return city; 
116.     } 
117.      
118.     /**
119.      * @param city the city to set
120.      */ 
121.     public void setCity(City city) { 
122.         this.city = city; 
123.     } 
124.  
125.     
126.  
127. } 

import java.util.Date;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

/**
* @author yukang
*
*/
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Table(name="KANG_USER")
public class User {
   
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    private String id;
   
    private String userName;
   
    private String passWord;
   
    @Temporal(TemporalType.TIMESTAMP)
    private Date addTime;
   
    @ManyToOne()
    @Cascade(value={CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name="CITY")
    @NotFound(action=NotFoundAction.IGNORE)
    private City city;
   
    public User() {
       
    }
   
   
    /**
     * @return
     */
    public String getId() {
        return id;
    }
   
    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }


    /**
     * @param userName the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }


    /**
     * @return the passWord
     */
    public String getPassWord() {
        return passWord;
    }


    /**
     * @param passWord the passWord to set
     */
    public void setPassWord(String passWord) {
        this.passWord = passWord;
    }

    /**
     * @return
     */
    public Date getAddTime() {
        return addTime;
    }
   
    /**
     * @param addTime the addTime to set
     */
    public void setAddTime(Date addTime) {
        this.addTime = addTime;
    }
   
    /**
     * @return
     */
    public City getCity() {
        return city;
    }
   
    /**
     * @param city the city to set
     */
    public void setCity(City city) {
        this.city = city;
    }

  

}


Java代码

   1. import java.util.HashSet; 
   2. import java.util.Set; 
   3.  
   4. import javax.persistence.Entity; 
   5. import javax.persistence.FetchType; 
   6. import javax.persistence.GeneratedValue; 
   7. import javax.persistence.Id; 
   8. import javax.persistence.OneToMany; 
   9. import javax.persistence.Table; 
  10.  
  11. import org.hibernate.annotations.Cache; 
  12. import org.hibernate.annotations.CacheConcurrencyStrategy; 
  13. import org.hibernate.annotations.Cascade; 
  14. import org.hibernate.annotations.CascadeType; 
  15. import org.hibernate.annotations.GenericGenerator; 
  16.  
  17. /**
  18.  * @author yukang
  19.  *
  20.  */ 
  21.  
  22. @Entity 
  23. @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE) 
  24. @Table(name="KANG_CITY") 
  25. public class City { 
  26.      
  27.     @Id 
  28.     @GeneratedValue(generator = "system-uuid") 
  29.     @GenericGenerator(name = "system-uuid", strategy = "uuid") 
  30.     private String id; 
  31.      
  32.     private String cityName; 
  33.      
  34.     @OneToMany(mappedBy="city",fetch=FetchType.EAGER) 
  35.     @Cascade(value={CascadeType.DELETE_ORPHAN})//级联删除 
  36.     private Set<User> userSet; 
  37.      
  38.     public City() { 
  39.          
  40.     } 
  41.  
  42.     /**
  43.      * @return the id
  44.      */ 
  45.     public String getId() { 
  46.         return id; 
  47.     } 
  48.  
  49.     /**
  50.      * @param id the id to set
  51.      */ 
  52.     public void setId(String id) { 
  53.         this.id = id; 
  54.     } 
  55.  
  56.     /**
  57.      * @return the cityName
  58.      */ 
  59.     public String getCityName() { 
  60.         return cityName; 
  61.     } 
  62.  
  63.     /**
  64.      * @param cityName the cityName to set
  65.      */ 
  66.     public void setCityName(String cityName) { 
  67.         this.cityName = cityName; 
  68.     } 
  69.  
  70.     /**
  71.      * @return the userSet
  72.      */ 
  73.     public Set<User> getUserSet() { 
  74.         return userSet; 
  75.     } 
  76.  
  77.     public void addUser(User user) { 
  78.         if ( userSet == null ) { 
  79.             userSet = new HashSet<User>(); 
  80.         } 
  81.         user.setCity(this); 
  82.         userSet.add( user ); 
  83.     } 
  84.  
  85. } 

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.GenericGenerator;

/**
* @author yukang
*
*/

@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Table(name="KANG_CITY")
public class City {
   
    @Id
    @GeneratedValue(generator = "system-uuid")
    @GenericGenerator(name = "system-uuid", strategy = "uuid")
    private String id;
   
    private String cityName;
   
    @OneToMany(mappedBy="city",fetch=FetchType.EAGER)
    @Cascade(value={CascadeType.DELETE_ORPHAN})//级联删除
    private Set<User> userSet;
   
    public City() {
       
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }

    /**
     * @return the cityName
     */
    public String getCityName() {
        return cityName;
    }

    /**
     * @param cityName the cityName to set
     */
    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    /**
     * @return the userSet
     */
    public Set<User> getUserSet() {
        return userSet;
    }

    public void addUser(User user) {
        if ( userSet == null ) {
            userSet = new HashSet<User>();
        }
        user.setCity(this);
        userSet.add( user );
    }

}

Spring中的sessionFactory的class要更改为org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean这个class

hibernate.cfg.xml要把User和City这两个class加上去

<mapping class="com.xxxx.domain.pojo.User"/>
<mapping class="com.xxxx.domain.pojo.City"/>

其他的地方以前怎么写的,现在不要改动,例子较简单,代码不做说明了
分享到:
评论

相关推荐

    hibernate annotation hibernate3

    本篇将深入探讨Hibernate 3版本中的注解使用,帮助开发者理解如何利用注解进行数据持久化,提高开发效率。 一、Hibernate 3简介 Hibernate 3是Hibernate ORM框架的一个重要版本,它引入了许多新特性,如对JPA(Java...

    Hibernate Annotation 中文文档

    这篇文档主要讨论的是Hibernate Annotation的使用方法,以及它如何替代传统的XML配置文件(*.hbm.xml)进行对象-关系映射。 **1. Hibernate Annotation简介** Hibernate Annotation是Hibernate框架的一个扩展,它...

    Hibernate Annotation 学习笔记

    1. 隐式映射:在上面的例子中,我们没有显式指定每个属性的映射,这是因为Hibernate Annotation采用了一种“配置例外”(configuration by exception)的理念。除主键和实体标识外,其他属性默认按照Java Bean的规则...

    Hibernate Annotation入门

    这篇博文将带你了解如何使用Hibernate Annotation进行开发。 首先,我们需要理解Java注解的基本概念。注解是一种元数据,它提供了在编译时或运行时处理类、方法和属性的能力。在Hibernate中,注解用于声明实体类...

    sping hibernate Annotation(注释配置) demo(例子)

    总结来说,这个 "Spring Hibernate Annotation demo" 展示了如何在 Spring 框架中使用注解配置来管理依赖,以及如何利用 Hibernate 的注解进行数据持久化。同时,它还涉及到了 Flex 前端与后端的交互。通过学习这个 ...

    Hibernate Annotation 笔记 总结 注解

    【标题】:深入理解Hibernate Annotation及其使用 【描述】:本文将全面介绍Hibernate Annotation的使用,包括事务管理和声明式事务处理,以及如何通过注解简化数据库持久化操作。 【标签】:Hibernate, ...

    HibernateAnnotation

    其中,HibernateAnnotation技术是指在Java类上使用注解来替代传统的XML配置文件,这不仅简化了配置过程,还提高了代码的可读性和维护性。 ### 核心知识点详解 #### 1. `@Entity` 注解 `@Entity` 是用于标记一个...

    hibernate annotation jar

    【标题】:“Hibernate Annotation JAR” 【描述】:“Hibernate 3.3 Annotation JAR”指的是Hibernate框架的一个特定版本,即3.3版本中用于对象...而文件“user1”则可能是一个演示如何使用这些注解的实际代码实例。

    hibernate-Annotation.jar

    Hibernate是一个开源的对象关系映射(ORM)框架,它允许开发者使用面向对象的编程方式来操作数据库,极大地简化了Java开发中的数据访问层。在Hibernate 3.x版本中,引入了Annotation注解,这是一种元数据的方式,...

    struts Spring Hibernate标签和注解资料合集

    springmvc注解.doc,Spring注解讲解.doc,struts2标签详解.pdf,struts2常用标签.pdf,Struts2页面开发中常用标签.pdf,Struts2注解详细说明文档.doc,Hibernate注解教程:Hibernate Annotation使用实例.maff

    hibernate annotation 3.40

    Hibernate Annotation是Hibernate 3.x引入的新特性,它允许开发者通过在Java类和类属性上使用特定的注解,来定义对象和数据库表之间的映射关系。这使得开发者可以脱离XML配置文件,直接在源代码中管理ORM配置。 2....

    hibernate 中文文档 and _annotation.chm

    《Hibernate中文文档与...通过阅读《Hibernate_3.2.0_Reference_zh_CN.chm》和《hibernate_annotation.chm》这两份文档,开发者可以深入了解Hibernate的使用方法,熟练掌握ORM技术,提升Java企业级应用的开发效率。

    hibernate-annotation 所需要的jar包

    在Java开发中,Hibernate与Annotation的结合使用极大地简化了数据持久化的复杂性,使得开发人员无需编写大量的SQL代码。下面将详细介绍Hibernate-Annotation所涉及到的知识点。 1. **Hibernate框架**: Hibernate是...

    hibernate中文APIhibernate annotation 中文API

    "Hibernate_Annotation.chm"则可能是专门针对Hibernate Annotation的中文指南,深入讲解如何使用注解进行数据库映射和操作。 **总结知识点:** 1. Hibernate是一个Java ORM框架,它将Java对象与数据库表对应,简化...

    hibernate 以Annotation方式配置在oracle和mysql

    hibernate 以Annotation方式配置在oracle和mysql hibernate,这里面提供了两个小例子,一个是配置跟oracle数据库相关联时的配置方法,一个是配置跟mysql数据库相关联时的配置方法。

    hibernate_annotation中文版

    本文将详细解析Hibernate Annotation的相关知识,旨在为使用Hibernate注解进行开发的朋友们提供详尽的指导。 一、Hibernate Annotation概述 在Hibernate早期版本中,对象-关系映射主要依赖XML配置文件来完成。然而...

    Hibernate Annotation 基于外键的一对多双向关联

    本文将深入探讨如何使用Hibernate注解实现基于外键的一对多双向关联。我们将通过具体实例来解析这个主题,并结合源码分析其工作原理。 首先,我们要明白一对多关联意味着一个实体可以拥有多个关联的实体。在数据库...

    spring+hibernate annotation 完整示例,带数据库脚本

    这个"spring+hibernate annotation 完整示例,带数据库脚本"的项目,旨在提供一个全面的示例,帮助开发者了解如何在实际项目中结合Spring和Hibernate使用注解来实现数据访问。下面我们将深入探讨这些知识点。 1. **...

    hibernate annotation+slf4j

    2. 日志API使用:使用`org.slf4j.Logger`和`org.slf4j.LoggerFactory`获取日志实例,然后通过`logger.info()`, `logger.debug()`, `logger.error()`等方法输出不同级别的日志。 3. 日志配置:配置相应的日志实现库...

Global site tag (gtag.js) - Google Analytics