• / 4
  • 下载费用:10 金币  

Hibernate 注解方式配置多对多.doc

关 键 词:
Hibernate 注解方式配置多对多.doc
资源描述:
Hibernate 注解方式配置多对多,学生,课程,学生与课程的关联,配置后会自动生成三张表。恩根据经验应该是这样的,代码是转的,哪个敢扔我。要转就转全吧,方便大家学习,呵呵。学生:package com.hibernate.many2many;import java.util.Set;import javax.persistence.CascadeType;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.JoinColumn;import javax.persistence.JoinTable;import javax.persistence.ManyToMany;import javax.persistence.Table;@Entity@Table(name=“t_student“)public class Student {@Id@GeneratedValueprivate Integer id;private String name;@ManyToMany(targetEntity=com.hibernate.many2many.Course.class,cascade={CascadeType.MERGE,CascadeType.PERSIST})@JoinTable(name=“student_course“,joinColumns={@JoinColumn(name=“student_id“)},inverseJoinColumns={@JoinColumn(name=“course_id“)})private Set course;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Set getCourse() {return course;}public void setCourse(Set course) {this.course = course;}}课程package com.hibernate.many2many;import java.util.Set;import javax.persistence.CascadeType;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;import javax.persistence.ManyToMany;import javax.persistence.Table;@Entity@Table(name=“t_course“)public class Course {@Id@GeneratedValueprivate Integer id;private String name;@ManyToMany(cascade={CascadeType.PERSIST,CascadeType.MERGE},mappedBy=“course“,targetEntity=com.hibernate.many2many.Student.class)private Set student;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Set getStudent() {return student;}public void setStudent(Set student) {this.student = student;}}测试类:public void testsave2(){Session session = null;try{session = HibernateUtil.getSession();session.beginTransaction();Student s = new Student();Student s2
展开阅读全文
  微传网所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
0条评论

还可以输入200字符

暂无评论,赶快抢占沙发吧。

关于本文
本文标题:Hibernate 注解方式配置多对多.doc
链接地址:https://www.weizhuannet.com/p-9474308.html
微传网是一个办公文档、学习资料下载的在线文档分享平台!

微传网博客

网站资源均来自网络,如有侵权,请联系客服删除!

 网站客服QQ:80879498  会员QQ群:727456886

copyright@ 2018-2028 微传网络工作室版权所有

     经营许可证编号:冀ICP备18006529号-1 ,公安局备案号:13028102000124

收起
展开