zoukankan      html  css  js  c++  java
  • Hibernate注解Annotation

    大部分仍然使用javax.persistence中的注解,有些Hibernate扩展的注解,在org.hibernate.annotations包中,如GenericGenerator.

    使用hibernate.cfg.xml中配置的事务,使用<mapping class="完整类名"/>加载注解的持久化类。Hibernate中可以混合使用注解和xml映射文件。

    注解后不能使用Configuration类,而是AnnotationConfiguration类初始化Hibernate

    1         SessionFactory sFactory=new AnnotationConfiguration()
    2 .configure().buildSessionFactory();
    3 Session session=sFactory.getCurrentSession();
    4 Transaction tx=session.beginTransaction();
    5 User user=new User();
    6 user.setUsername("tazi2");
    7 user.setPassword("12345");
    8 session.save(user);
    9 tx.commit();

    注解种类:

    @Entity @Table @Id @Column @Transient(非持久化属性) @Temporal(时间精度) @ManyToOne @... 

    在类前

    @NamedQueries({@NamedQuery(name="getById",query="from User where id=:id")})

    使用方法与在xml配置的命名查询相同

    1         Query query=session.getNamedQuery("getById");
    2 query.setInteger("id", 4);
    3 User user=(User)query.uniqueResult();
    4 System.out.println(user.getUsername());

    @Version乐观锁 @Cache二级缓存 @Filters @FilterDef 

    @GeneratedValue默认为AUTO,根据底层数据库决定。还可以用Hibernate扩展的@GenericGenerator

    例子:

    package com.tazi.domin;

    import java.sql.Timestamp;
    import java.util.Date;
    import java.util.HashSet;
    import java.util.Set;

    import javax.persistence.CascadeType;
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.JoinColumn;
    import javax.persistence.Lob;
    import javax.persistence.ManyToOne;

    /**
    * Product entity.
    @author MyEclipse Persistence Tools
    */
    @Entity
    public class Product implements java.io.Serializable {
    // Fields
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;
    @Column(length=20,nullable=false)
    private String name;
    @Column(scale=2)
    private Float price;
    @Lob
    private String description;
    @ManyToOne
    @JoinColumn(name="CATEGORY_ID")//,referencedColumnName="ID"
    private Category category;
    // Constructors
    public Category getCategory() {
    return category;
    }

    public void setCategory(Category category) {
    this.category = category;
    }

    //... Property accessors
    }

    Category.java

     1 package com.tazi.domin;
    2
    3 import java.util.HashSet;
    4 import java.util.Set;
    5
    6 import javax.persistence.CascadeType;
    7 import javax.persistence.Column;
    8 import javax.persistence.Entity;
    9 import javax.persistence.FetchType;
    10 import javax.persistence.GeneratedValue;
    11 import javax.persistence.GenerationType;
    12 import javax.persistence.Id;
    13 import javax.persistence.OneToMany;
    14
    15 /**
    16 * Category entity. @author MyEclipse Persistence Tools
    17 */
    18 @Entity
    19 public class Category implements java.io.Serializable {
    20
    21 // Fields
    22 @Id
    23 @GeneratedValue(strategy=GenerationType.IDENTITY)
    24 private Integer id;
    25 @Column
    26 private String name;
    27 private String description;
    28 @OneToMany(
    29 cascade={CascadeType.ALL},
    30 fetch=FetchType.EAGER, //使用List不能使用eager,使用Set可以
    31 mappedBy="category"
    32 )
    33 private Set<Product> products = new HashSet(0);
    34
    35 // Constructors
    36
    37 /** default constructor */
    38 public Category() {
    39 }
    40 /** minimal constructor */
    41 public Category(String name) {
    42 this.name = name;
    43 }
    44 /** full constructor */
    45 public Category(String name, String description, Set products) {
    46 this.name = name;
    47 this.description = description;
    48 this.products = products;
    49 }
    50 //... Property accessors
    51 }








  • 相关阅读:
    Vim的行号、语法显示等设置,即.vimrc文件的配置
    Vim 配置详解
    经典SQL语句大全
    几种流行的AJAX框架jQuery,Mootools,Dojo,Ext JS的对比
    GHOST XP SP3无法安装IIS服务的解决方法
    开发中巧用Enum枚举类型
    代码的坏味道
    C#转义字符
    Dijit、ExtJS、jQuery UI 异同浅析
    未雨绸缪 Plan to Throw One Away
  • 原文地址:https://www.cnblogs.com/tazi/p/2303833.html
Copyright © 2011-2022 走看看