新建java project项目:
Add Hibernate Capabilities
hibernate.cfg.xml
1 <?xml version='1.0' encoding='UTF-8'?> 2 <!DOCTYPE hibernate-configuration PUBLIC 3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 5 6 <!-- Generated by MyEclipse Hibernate Tools. --> 7 <hibernate-configuration> 8 9 <session-factory> 10 <property name="dialect"> 11 org.hibernate.dialect.MySQLDialect 12 </property> 13 <property name="connection.url"> 14 jdbc:mysql://localhost:3307/users 15 </property> 16 <property name="connection.username">root</property> 17 <property name="connection.password">root</property> 18 <property name="connection.driver_class"> 19 com.mysql.jdbc.Driver 20 </property> 21 <property name="myeclipse.connection.profile"> 22 mysqlusers 23 </property> 24 <property name="format_sql">true</property> 25 <property name="show_sql">true</property> 26 <property name="current_session_context_class">thread</property> 27 <mapping class="com.b510.examples.Product" /> 28 <mapping class="com.b510.examples.Category" /> 29 30 </session-factory> 31 32 </hibernate-configuration>
利用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<Product> products = new HashSet<Product>(0); 33 34 35 public Category() { 36 } 37 38 public Category(String name, String description, Set<Product> 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<Product> getProducts() { 84 return this.products; 85 } 86 87 public void setProducts(Set<Product> 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 /** 2 * 3 */ 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 /** 14 * 15 * @author XHW 16 * 17 * @date 2011-7-20 18 * 19 */ 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<Product> 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=?
相关推荐
使用hibernate注解,必须要使用库hibernate-commons-annotations,hibernate-core,hibernate-jpa,ejb3-persistence,javassist等
本文档将深入探讨Hibernate注解的使用,帮助开发者更好地理解和利用这些注解来实现对象关系映射(ORM)。 一、Hibernate注解基础 Hibernate注解是一种元数据方式,用于在Java类和属性上声明数据库映射信息,从而...
本主题将深入探讨Hibernate注解的相关知识点。 1. **注解概述**: 注解(Annotations)是Java 5引入的一种元数据,它提供了在源代码中嵌入信息的方式,这些信息可以被编译器或者在运行时的Java虚拟机使用。在...
这个"springmvc+hibernate注解框架整合 demo"是一个示例项目,展示了如何通过注解方式将这两个框架无缝集成。 **Spring MVC 框架** Spring MVC 是 Spring 框架的一部分,它提供了处理HTTP请求、转发响应以及管理...
### Hibernate注解API知识点概述 #### 一、Hibernate注解简介 Hibernate 是一款非常流行的 Java 持久层框架,它极大地简化了数据库操作。在 Hibernate 中,可以通过使用注解来映射对象与数据库表之间的关系,从而...
在Java的ORM框架Hibernate中,注解是一种强大的工具,它允许开发者无需XML配置就能实现对象关系映射。本文主要总结了Hibernate中常见的注解用法,涵盖了类级别和属性级别的注解,以及与主键和非主键相关的注解。 1....
在实际项目中,除了这三个核心的Hibernate注解库,可能还需要其他的依赖,例如: - **jta.jar**:Java Transaction API,用于支持分布式事务处理。 - **javax.persistence-api.jar**:JPA规范的API,包含了一些基础...
SSH框架,全称为Struts2、Spring和Hibernate的组合,是Java Web开发中常见的三大开源框架。本教程将深入探讨如何使用注解来构建SSH框架,以实现更简洁、高效的应用程序开发。 **Struts2** 是MVC设计模式的一个实现...
《Hibernate注解详解》 Hibernate 是一款强大的Java对象关系映射(ORM)框架,它极大地简化了数据库操作。在Hibernate中,注解是用于描述Java类及其属性如何映射到数据库表的重要工具。本文将全面解析Hibernate中的...
在使用 Hibernate 注解映射之前,需要添加相关的 jar 包,包括 hibernate-annotations.jar、ejb3-persistence.jar 和 hibernate-commons-annotations.jar。 在实体类中,可以使用 JPA 的标准注解来描述对象关系映射...
本实例"spring-hibernate注解配置源码"旨在展示如何在不使用XML配置的情况下,通过注解的方式整合Struts2、Spring和Hibernate,创建一个完整的MVC(模型-视图-控制器)架构的应用。下面将详细介绍这个过程中的关键...
下面我们将详细讲解如何配置和使用Spring+Hibernate注解声明式事务: 1. **配置Spring**: - 首先,我们需要在Spring配置文件中启用事务管理器,通常是`HibernateTransactionManager`,并配置数据源。 - 然后,...
在本文中,我们将深入探讨Hibernate注解的几个核心方面:级联关系、增删改查操作、二级缓存、日志配置以及注解解析。 1. **级联关系**: Hibernate中的级联关系允许我们在一个实体的操作中自动处理与其关联的其他...
一。实体Bean 每个持久化POJO类都是一个实体Bean, 通过在类的定义中使用 @Entity 注解来进行声明。...Hibernate 可以对类的属性或者方法进行注解。属性对应field类别,方法的 getXxx()对应property类别。
hibernate 注解 annotation 教程
3. **主键注解(@Id)** `@Id`注解标识类中的一个字段作为主键,对应数据库表的主键字段: ```java @Entity public class User { @Id private Long id; // ... } ``` 4. **生成主键策略注解(@GeneratedValue...
在 Hibernate 中,注解是一种简洁且强大的工具,用于替代传统的 XML 配置文件来描述对象模型和数据库之间的映射关系。这篇文档将深入探讨 Hibernate 注解的使用。 ### 第 1 章 创建一个注解项目 在开始使用 ...
**Hibernate 3 注解技术详解** 在Java世界中,Hibernate是一个强大的对象关系映射(ORM)框架,它极大地简化了数据库操作。 Hibernate 3引入了注解支持,使得开发人员无需XML配置文件即可进行对象持久化,提高了...
在Java的持久化框架Hibernate中,注解是用于简化对象关系映射(ORM)的一种方式。本篇文章将详细探讨在Hibernate中如何使用注解来处理各种关联关系,特别是`mappedBy`属性的用法。 首先,`@OneToMany`注解用于表示...
《Hibernate注解详解》 在Java开发领域,Hibernate作为一个强大的对象关系映射(ORM)框架,极大地简化了数据库操作。而随着Java注解的普及,Hibernate也开始广泛采用注解方式来替代XML配置,使得代码更加简洁,...