用myeclipse实现hibernate注解简单例子

目录:

配置文件:

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.itcast.cn.demol.Product" />  
  28.    <mapping class="com.itcast.cn.demol.Category" />  
  29.    
  30.   </session-factory>  
  31.    
  32.  </hibernate-configuration></span>  
 

利用Hibernate的逆向工程生成:

Category.java

Product.java   

Category.java

  1.  package com.itcast.cn.demol;  
  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 truenullable 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.  }</span>  
 

Product.java

  1. package com.itcast.cn.demol;  
  2.  import javax.persistence.Column;  
  3.  import javax.persistence.Entity;  
  4.  import javax.persistence.FetchType;  
  5.  import javax.persistence.GeneratedValue;  
  6.  import javax.persistence.Id;  
  7.  import javax.persistence.JoinColumn;  
  8.  import javax.persistence.ManyToOne;  
  9.  import javax.persistence.Table;  
  10.  import org.hibernate.annotations.GenericGenerator;  
  11.    
  12.    
  13.  @Entity  
  14.  @Table(name "product"catalog "users" 
  15.  public class Product implements java.io.Serializable  
  16.    
  17.   private static final long serialVersionUID -1546206493725028472L;  
  18.   private Integer id;  
  19.   private Category category;  
  20.   private String name;  
  21.   private String price;  
  22.   private String descripton;  
  23.    
  24.     
  25.   public Product()  
  26.    
  27.    
  28.   public Product(Category category, String name, String price,  
  29.     String descripton)  
  30.    this.category category 
  31.    this.name name 
  32.    this.price price 
  33.    this.descripton descripton 
  34.    
  35.     
  36.   @GenericGenerator(name "generator"strategy "increment" 
  37.   @Id  
  38.   @GeneratedValue(generator "generator" 
  39.   @Column(name "id"unique truenullable false 
  40.   public Integer getId()  
  41.    return this.id;  
  42.    
  43.    
  44.   public void setId(Integer id)  
  45.    this.id id 
  46.   
  47.    
  48.   //延迟加载:多对一方式  
  49.   //关联信息:外键name "category_id"  
  50.   @ManyToOne(fetch FetchType.LAZY)  
  51.   @JoinColumn(name "category_id" 
  52.   public Category getCategory()  
  53.    return this.category;  
  54.    
  55.    
  56.   public void setCategory(Category category)  
  57.    this.category category 
  58.    
  59.    
  60.   @Column(name "name"length 500 
  61.   public String getName()  
  62.    return this.name;  
  63.    
  64.    
  65.   public void setName(String name)  
  66.    this.name name 
  67.    
  68.    
  69.   @Column(name "price"length 10 
  70.   public String getPrice()  
  71.    return this.price;  
  72.    
  73.    
  74.   public void setPrice(String price)  
  75.    this.price price 
  76.    
  77.    
  78.   @Column(name "descripton"length 500 
  79.   public String getDescripton()  
  80.    return this.descripton;  
  81.    
  82.    
  83.   public void setDescripton(String descripton)  
  84.    this.descripton descripton 
  85.    
  86.    
  87.  }</span>  
 

测试代码:

  1. package com.itcast.cn.demol;  
  2.   
  3.  import java.util.Set;  
  4.  import org.hibernate.Session;  
  5.  import org.hibernate.SessionFactory;  
  6.  import org.hibernate.cfg.AnnotationConfiguration;  
  7.  import org.hibernate.cfg.Configuration;  
  8.   
  9.  public class HibernateTest  
  10.    
  11.  public static void main(String[] args)  
  12.    HibernateTest test=new HibernateTest();  
  13.    test.add();  
  14.    test.find();  
  15.   
  16.   public void add(){  
  17.   Configuration config=new AnnotationConfiguration();  
  18.   config.configure();  
  19.   SessionFactory sessionFactory=config.buildSessionFactory();  
  20.   Session session=sessionFactory.getCurrentSession();  
  21.   session.beginTransaction();  
  22.   Category c=(Category)session.get(Category.class, 5);  
  23.     
  24.   Product p=new Product();  
  25.   p.setName("IT技术");  
  26.   p.setPrice("123");  
  27.   p.setDescripton("IT技术,好啊,真是红啊");  
  28.     
  29.   p.setCategory(c);  
  30.   c.getProducts().add(p);  
  31.     
  32.   session.save(p);  
  33.   session.getTransaction().commit();  
  34.     
  35.    
  36.   public void find(){  
  37.    Configuration config=new AnnotationConfiguration();  
  38.    config.configure();  
  39.    SessionFactory sessionFactory=config.buildSessionFactory();  
  40.    Session session=sessionFactory.getCurrentSession();  
  41.    session.beginTransaction();  
  42.    Category c=(Category)session.get(Category.class, 5);  
  43.    System.out.println("id: "+c.getId()+"  name:"+c.getName());  
  44.    Set<Product> p=c.getProducts();  
  45.    for(Product product:p){  
  46.    System.out.println("id:"+product.getId()  
  47.                  +"  name:"+product.getName()  
  48.                  +"  description:"+product.getDescripton());  
  49.     
  50.     session.getTransaction().commit();  
  51.    
  52. }</span>  
 

运行效果:

  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:  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:IT技术  description:IT技术,好啊,真是红啊</span> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值