hibernate多对多注解配置
上一篇文章主要讲解了Hibernate多对多配置文件的配置方法,接下来继续讲解注解方式配置多对多关系的方法。注解功能已经加入hibernate4之中了,但hibernate3要使用注解配置还需要另外引入jar包。
需要的jar包可见附件:
接下来开始进入正题:
一、第一种直接配置多对多关系,以Role角色-Right权限为例。
import java.util.Date; import java.util.List; import javax.persistence.*; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name="SYS_ROLE") public class Role{ private String role_id; private String role_name; private Date cre_time; private String parent_id; private String describe; private int sort; private Listrights; @Id @GeneratedValue(generator = "paymentableGenerator") @GenericGenerator(name = "paymentableGenerator", strategy = "uuid") public String getRole_id() { return role_id; } public void setRole_id(String roleId) { role_id = roleId; } @Column(name="role_name") public String getRole_name() { return role_name; } public void setRole_name(String roleName) { role_name = roleName; } @Column(name="cre_time") public Date getCre_time() { return cre_time; } public void setCre_time(Date creTime) { cre_time = creTime; } @Column(name="parent_id") public String getParent_id() { return parent_id; } public void setParent_id(String parentId) { parent_id = parentId; } @Column(name="describe") public String getDescribe() { return describe; } public void setDescribe(String describe) { this.describe = describe; } @Column(name="sort") public int getSort() { return sort; } public void setSort(int sort) { this.sort = sort; } //多对多映射 @ManyToMany(targetEntity = Right.class, fetch = FetchType.LAZY) @JoinTable(name = "ROLE_RIGHT", joinColumns = @JoinColumn(name = "ROLE_ID"), inverseJoinColumns = @JoinColumn(name = "RIGHT_ID")) public List getRights() { return rights; } public void setRights(List rights) { this.rights = rights; } @Override public String toString() { return "Role [cre_time=" + cre_time + ", describe=" + describe + ", parent_id=" + parent_id + ", role_id=" + role_id + ", role_name=" + role_name + ", sort=" + sort + "]"; } }
import java.util.List; import javax.persistence.*; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name="SYS_RIGHTS") public class Right{ private String right_id; private String right_name; private String url; private String parent_id; private int lev; private int sort; private Listroles; @Id @GeneratedValue(generator = "paymentableGenerator") @GenericGenerator(name = "paymentableGenerator", strategy = "uuid") public String getRight_id() { return right_id; } public void setRight_id(String rightId) { right_id = rightId; } @Column(name="right_name") public String getRight_name() { return right_name; } public void setRight_name(String rightName) { right_name = rightName; } @Column(name="url") public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } @Column(name="parent_id") public String getParent_id() { return parent_id; } public void setParent_id(String parentId) { parent_id = parentId; } @Column(name="lev") public int getLev() { return lev; } public void setLev(int lev) { this.lev = lev; } @Column(name="sort") public int getSort() { return sort; } public void setSort(int sort) { this.sort = sort; } //多对多映射 @ManyToMany(mappedBy="rights") public List getRoles() { return roles; } public void setRoles(List roles) { this.roles = roles; } @Override public String toString() { return "Right [lev=" + lev + ", parent_id=" + parent_id + ", right_id=" + right_id + ", right_name=" + right_name + ", sort=" + sort + ", url=" + url + "]"; } }
最后,在hibernate-cfg.xml文件中配置对应的Role和Right实体即可。
到这里一个多对多配置就完成了。
常用的注解:
1.@Entity 声明实体
2.@Table(name="SYS_ROLE") 声明实体类对应的表名
3.@Column(name="role_name") 声明属性对应的列
4.@Id @GeneratedValue(generator = "paymentableGenerator") @GenericGenerator(name = "paymentableGenerator", strategy = "uuid")
声明主键及生成策略,在这里uid的生成策略
5. @OneToOne(mappedBy="card") 一对一映射
6.@OneToMany(fetch = FetchType.LAZY, mappedBy = "student") 一对多映射
7.@ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "student")
多对一映射
8.@ManyToMany(targetEntity = Right.class, fetch = FetchType.LAZY)
@JoinTable(name = "ROLE_RIGHT", joinColumns = @JoinColumn(name = "ROLE_ID"), inverseJoinColumns = @JoinColumn(name = "RIGHT_ID"))
多对多映射
9.@Transient 表示该属性并非一个到数据库表的字段的映射,ORM框架将忽略该属性
二、第二种情况,就是在中间实体中配置两个多对一关系,以学生Student-成绩Score-课程Course为例。
import java.util.List; import javax.persistence.*; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name="student") public class Student { private String id; private String name; private ListscoreList; @Id @GeneratedValue(generator = "paymentableGenerator") @GenericGenerator(name = "paymentableGenerator", strategy = "uuid") public String getId() { return id; } public void setId(String id) { this.id = id; } @Column(name="name") public String getName() { return name; } public void setName(String name) { this.name = name; } //学生端一对多,学生的成绩列表,也可以选择不配置 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "student") public List getScoreList() { return scoreList; } public void setScoreList(List scoreList) { this.scoreList = scoreList; } @Override public String toString() { return "Student [id=" + id + ", name=" + name + "]"; } }
import javax.persistence.*; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name="score") public class Score { private String id;//主键id private Student student;//学生id private Course course;//课程id private int score;//成绩 @Id @GeneratedValue(generator = "paymentableGenerator") @GenericGenerator(name = "paymentableGenerator", strategy = "uuid") public String getId() { return id; } public void setId(String id) { this.id = id; } //多对一映射 @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "student") public Student getStudent() { return student; } public void setStudent(Student student) { this.student = student; } //多对一映射 @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "course") public Course getCourse() { return course; } public void setCourse(Course course) { this.course = course; } @Column(name="score") public int getScore() { return score; } public void setScore(int score) { this.score = score; } @Override public String toString() { return "Score [course=" + course.getName() + ", score=" + score + ", student=" + student.getName() + "]"; } }
import java.util.List; import javax.persistence.*; import org.hibernate.annotations.GenericGenerator; @Entity @Table(name="course") public class Course { private String id; private String name; private ListscoreList; @Id @GeneratedValue(generator = "paymentableGenerator") @GenericGenerator(name = "paymentableGenerator", strategy = "uuid") public String getId() { return id; } public void setId(String id) { this.id = id; } @Column(name="name") public String getName() { return name; } public void setName(String name) { this.name = name; } //课程端一对多,课程的成绩列表,也可以选择不配置 @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "course") public List getScoreList() { return scoreList; } public void setScoreList(List scoreList) { this.scoreList = scoreList; } @Override public String toString() { return "Course [id=" + id + ", name=" + name + "]"; } }
以上就是多对多两种情况的注解配置方法了,注解方式真的是很优秀的配置方法,很直观很简洁一目了然,也不需要另外配置xml文件,直接在实体类文件中注明数据库关系映射的情况即可,开发效率提高很明显,我用过注解配置之后都不想用配置文件来配置了,哈哈~