标签:com http blog style class div img code c log t
Hibernate中注解注解比较多,常用的也就那么几个,在这里把Hibernate中的注解汇总总结一下。
@Entity:将一个类声明为一个实体bean,即一个持久化POJO;
@Id:声明bean的标识属性,即和表中的主键对应的属性;
@Table:声明bean映射数据库中指定的表;
@Column:声明bean的属性到表的列的映射,该注解还有以下属性:
@GenerateValue:声明主键的生成策略,该注解还有以下属性:
@SequenceGenerator:声明一个数据库序列,该注解还有以下属性:
@GenericGenerator:声明一个hibernate的主键生成策略,有十三种生成策略。该注解还有以下属性:
@OneToOne:设置一对一关联。
示例:
1 @Entity 2 public class Hansband { 3 private int id; 4 private String username; 5 private Wife wife; 6 7 @Id 8 @GeneratedValue(strategy = GenerationType.IDENTITY) 9 public int getId() { 10 return id; 11 } 12 13 @Column 14 public String getUsername() { 15 return username; 16 } 17 /** 18 * @OneToOne:一对一关联 19 * cascade:级联,它可以有有五个值可选,分别是: 20 * CascadeType.PERSIST:级联新建 21 * CascadeType.REMOVE : 级联删除 22 * CascadeType.REFRESH:级联刷新 23 * CascadeType.MERGE : 级联更新 24 * CascadeType.ALL : 以上全部四项 25 * @JoinColumn:主表外键字段 26 * wifeId:Hansband所映射的表中的一个字段 27 */ 28 @OneToOne 29 @JoinColumn(name="wifeId") 30 public Wife getWife() { 31 return wife; 32 } 33 public void setId(int id) { 34 this.id = id; 35 } 36 public void setUsername(String username) { 37 this.username = username; 38 } 39 public void setWife(Wife wife) { 40 this.wife = wife; 41 } 42 } 43 44 45 @Entity 46 public class Wife { 47 private int id ; 48 private String username; 49 private Hansband hansband; 50 51 @Id 52 @GeneratedValue(strategy = GenerationType.AUTO) 53 public int getId() { 54 return id; 55 } 56 public String getUsername() { 57 return username; 58 } 59 public void setId(int id) { 60 this.id = id; 61 } 62 public void setUsername(String username) { 63 this.username = username; 64 } 65 /** 66 * @OneToOne:一对一关联 67 * mappedBy = "wife":意思是说这里的一对一配置参考了wife 68 * wife又是什么呢?wife是Husband类中的getWife(),注意不是Husband类中的 69 * wife属性,Husband类中的OneToOne配置就是在getWife()方法上面配的. 70 * 如果Husband类中的getWife()方法改成getNewHusband(),其他不变的话, 71 * 这里就要写成:mappedBy = "new Husband" 72 */ 73 @OneToOne(mappedBy="wife") 74 public Hansband getHansband() { 75 return hansband; 76 } 77 public void setHansband(Hansband hansband) { 78 this.hansband = hansband; 79 } 80 }
@OneToMany和@ManyToOne:设置一对多和多对一关联。
示例:
@Entity @Table(name = "t_employee") public class Employee { private Integer employeeId; private String employeeName; private Company company; @Id @GeneratedValue(strategy = GenerationType.AUTO) public Integer getEmployeeId() { return employeeId; } /** * @ManyToOne:多对一关联 * cascade:级联,它可以有有五个值可选,分别是: * CascadeType.PERSIST:级联新建 * CascadeType.REMOVE : 级联删除 * CascadeType.REFRESH:级联刷新 * CascadeType.MERGE : 级联更新 * CascadeType.ALL : 以上全部四项 * fetch = FetchType.LAZY,延迟加载策略,如果不想延迟加载可以用FetchType.EAGER * @JoinColumn:主表外键字段 * cid:Employee所映射的表中的一个字段 */ @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH},fetch = FetchType.LAZY) @JoinColumn(name = "cid") public Company getCompany() { return company; } public String getEmployeeName() { return employeeName; } public void setEmployeeId(Integer employeeId) { this.employeeId = employeeId; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } public void setCompany(Company company) { this.company = company; } } @Entity @Table(name = "t_company") public class Company { private Integer companyId; private String companyName; private Set<Employee> employees; @Id @GeneratedValue(strategy = GenerationType.AUTO) public Integer getCompanyId() { return companyId; } /** * @OneToMany:一对多关联 * mappedBy = "company":意思是说这里的一对一配置参考了company * company又是什么呢?company是Employ类中的getCompany(),注意不是Employ类中的 * company属性,Employ类中的OneToMany配置就是在getCompany()方法上面配的. * 如果Employ类中的getCompany()方法改成getNewCompany(),其他不变的话, * 这里就要写成:mappedBy = "newCompany" */ @OneToMany(mappedBy = "company") public Set<Employee> getEmployees() { return employees; } public String getCompanyName() { return companyName; } public void setCompanyId(Integer companyId) { this.companyId = companyId; } public void setCompanyName(String companyName) { this.companyName = companyName; } public void setEmployees(Set<Employee> employees) { this.employees = employees; } }
@ManyToMany:设置多对多关联。
@Entity @Table(name="rong_user") public class User{ //省略其它内容 private Set<Role> roles = new LinkedHashSet<Role>();//角色集合 @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}) //name是数据库中间表名 @JoinTable(name = "rong_user_role", joinColumns = { @JoinColumn(name ="user_id" )}, inverseJoinColumns = { @JoinColumn(name = "role_id") }) @OrderBy("id") public Set<Role> getRoles() { return roles; } public void setRoles(Set<Role> roles) { this.roles = roles; } } @Entity @Table(name="rong_role") public class Role{ //省略其它内容 private Set<User> user = new LinkedHashSet<User>();//用户集合 @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, mappedBy = "roles", fetch = FetchType.LAZY) public Set<User> getUser() { return user; } public void setUser(Set<User> user) { this.user = user; } }
Hibernate中的注解说明,码迷,mamicode.com
标签:com http blog style class div img code c log t
原文地址:http://www.cnblogs.com/blackckat-hm/p/3700376.html