Hibernate 注解的用法以及说明(二)

注解映射必须满足两大条件:Hibernate3.2以上版本和JSEE 5。 
@Entity 类注释,所有要持久化的类都要有
@Entity   
public class Org  implements java.io.Serializable {    
}   
@Id 主键       
@Id   
     @GeneratedValue   
     private String orgId;    
     private String orgName;   
@Column(name="...") 该属性对应表中的字段是什么,没有name表示一样 
@Table 对象与表映射 
@UniqueConstraint 唯一约束 
@Version 方法和字段级,乐观锁用法,返回数字和timestamp,数字为首选 
@Transient 暂态属性,表示不需要处理 
@Basic 最基本的注释。有两个属性:fetch是否延迟加载,optional是否允许null 
@Enumerated 枚举类型 
@Temporal 日期转换。默认转换Timestamp 
@Lob 通常与@Basic同时使用,提高访问速度。 
@Embeddable 类级,表可嵌入的 
@Embedded 方法字段级,表被嵌入的对象和@Embeddable一起使用 
@AttributeOverrides 属性重写 
@AttributeOverride 属性重写的内容和@AttributeOverrides一起嵌套使用 
@SecondaryTables 多个表格映射 
@SecondaryTable 定义辅助表格映射和@SecondaryTables一起嵌套使用 
@GeneratedValue 标识符生成策略,默认Auto 
表与表关系映射 
@OneToOne:一对一映射。它包含五个属性: 
targetEntity:关联的目标类 
Cascade:持久化时的级联操作,默认没有 
fetch:获取对象的方式,默认EAGER 
Optional:目标对象是否允许为null,默认允许 
mappedBy:定义双向关联中的从属类。 
单向: 
    @JoinColumn:定义外键(主表会多一字段,做外键) 
@OneToMany:一对多映射;@ManyToOne:多对一映射 
单向一对多: 
    @OneToMany(cascade=CascadeType.ALL) 
    @JoinColumn(name="book_oid")/**book:表;oid:book表的主键;无name会按此规则自动生成*/ 
单向多对一: 
    @ManyToOne(cascade=CascadeType.ALL) 
    @JoinColumn(name="author_oid") 
关联表格一对多: 
    @OneToMany(cascade=CascadeType.ALL) 
    @JoinTable(joinColumn={@JoinColumn(name="BOOK_OBJECT_OID")},inverseJoinColumns={@JoinColumn(name="AUTHER_OBJECT_OID")}) 
双向一对多或多对一: 
    不需要多一张表,只是使用mappedBy:使用在One一方,值为One方类名表示Many的从属类。 
@Entity  
Java代码 
@Entity   
public class Org  implements java.io.Serializable {    
   
   
    // Fields        
    @Id   
    @GeneratedValue   
     private String orgId;    
     private String orgName;    
     @OneToMany(mappedBy = "org")    
     private List<Department> departments;    
   
    // Constructors    
...    
    // Property accessors    
...    
}   

@Entity  public class Org  implements java.io.Serializable {        // Fields       @Id   @GeneratedValue       private String orgId;       private String orgName;       @OneToMany(mappedBy = "org")       private List<Department> departments;        // Constructors  ...      // Property accessors  ...  }


@Entity
public class Department implements java.io.Serializable {
    // Fields    
@Id
@GeneratedValue
     private String id;
     private String name;
     @ManyToOne(fetch=FetchType.EAGER)
     @JoinColumn(name="org_orgId")
     private Org org;
     @OneToMany(mappedBy = "department")
     private List<Employee> employees;
    // Constructors
    public List<Employee> getEmployees() {
return employees;
}
public void setEmployees(List<Employee> employees) {
this.employees = employees;
}
public Org getOrg() {
return org;
}
public void setOrg(Org org) {
this.org = org;
}
/** default constructor */
             .
             .
             .
}
Java代码 
@Entity   
public class Employee  implements java.io.Serializable {    
   
   
    // Fields        
    @Id   
    @GeneratedValue   
     private String employeeId;    
     private String employeeName;    
     private String passWord;    
     private Integer age;    
     private Integer sex;    
     @ManyToOne(fetch=FetchType.EAGER)    
     @JoinColumn(name="department_id")    
     private Department department;    
   
         
    public Department getDepartment() {    
        return department;    
     }    
   
    public void setDepartment(Department department) {    
        this.department = department;    
     }    
   
    /** default constructor */   
     ...    
    // Property accessors    
     ...    
}   

@Entity  public class Employee  implements java.io.Serializable {        // Fields       @Id   @GeneratedValue       private String employeeId;       private String employeeName;       private String passWord;       private Integer age;       private Integer sex;       @ManyToOne(fetch=FetchType.EAGER)       @JoinColumn(name="department_id")       private Department department;               public Department getDepartment() {    return department;   }     public void setDepartment(Department department) {    this.department = department;   }     /** default constructor */      ...      // Property accessors      ...  }


双向多对多:@ManyToMany.单向多对多这里不在赘述(没有太多实际意义) 
这个比较简单,看下代码就明白了: 
@Entity
public class Book implements java.io.Serializable {
@Id
private int id;
private String name;
private float money;
@ManyToMany(cascade = CascadeType.ALL)
private List<Author> authors;
public List<Author> getAuthors() {
return authors;
}
public void setAuthors(List<Author> authors) {
this.authors = authors;
}
         ...
}
@Entity
public class Author implements java.io.Serializable {
@Id
private int id;
private String name;
private int age;
@ManyToMany(mappedBy="authors")
private List<Book> books;
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
         ...
}
基于注解的hibernate主键设置:@Id. 
那么它的生成规则是什么呢?是由@GeneratedValue来规定的。 
我们先来看看它是如何定义的: 
Java代码 
@Target({METHOD,FIELD})    
    @Retention(RUNTIME)    
    public @interface GeneratedValue{    
         GenerationType strategy() default AUTO;    
         String generator() default "";    
     }   

@Target({METHOD,FIELD})   @Retention(RUNTIME)   public @interface GeneratedValue{    GenerationType strategy() default AUTO;    String generator() default "";   }


         
Java代码 
public enum GenerationType{    
         TABLE,    
         SEQUENCE,    
         IDENTITY,    
         AUTO    
     }   

public enum GenerationType{    TABLE,    SEQUENCE,    IDENTITY,    AUTO   }


现在我们看到了,它提供了4种生成策略: 
TABLE:使用一个特定的数据库表格来保存标识符序列。 
SEQUENCE:生成序列化标识符。 
IDENTITY:标识符有数据库自动生成(主要是自动增长型) 
AUTO:标识符生成工作由hibernate自动处理。实际项目开发不建议使用。 
注意:当主键为int,而数据库中又不是自动增长型时,使用@GeneratedValue是无法正常工作的。 
我们也可以使用下面的方式来自己指定我们的主键值: 
           
Java代码 
@GeneratedValue(generator = "c-assigned")    
    @GenericGenerator(name = "c-assigned", strategy = "assigned")    
     private String employeeId;   

@GeneratedValue(generator = "c-assigned")   @GenericGenerator(name = "c-assigned", strategy = "assigned")       private String employeeId;


或者直接不要定义@GeneratedValue,只定义@Id效果也是一样的。

Hibernate 注解的用法以及说明(二),古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。