不需要,只要加上对应annotation就行或是xml配置,如果通过hibernate自动建表会在数据库中生成对应的一张中间表
解决方案 »
- 各位大神怎么通过heritrix抓取自己想要的文本和图片呢?~~~~ 急啊
- 请问jsp如何获取客户端的mac地址?
- Jsp中Servlet编译问题
- myeclipse8+struts2.1+spring2.5+hibernate3.2+weblogic10配置二级缓存时提示错误
- 请大家看一下程序,帮忙解决一下问题
- session对象的管理,先谢谢!!!!
- 在已经查询的结果循环中再进行一个新的查询不能实现出现这样的错误!
- HttpClient如何接收有ModelAndView返回的数据
- 请教各位朋友,系统提示:package javax.servlet does not exit 该怎么解决?
- jsp入门的几个问题,
- 请问spring怎么同时配置jdbc和hibernate链接???
- 如何控制1min只允许访问某个方法100次?如何保存一个长期数据?
每个课程只能有一个学生的时候才叫一对多。
配置成多对多就不需要。
是不是写成这样就可以
Student类:
@Entity
@Table(name = "student", catalog = "db")
public class Student implements java.io.Serializable { private Integer id;
private String name;
private String password;
private Boolean isDelete;
private Set<Course> Courses= new HashSet<Course>(0); @Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
} @Column(name = "name")
public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} @Column(name = "password")
public String getPassword() {
return this.password;
} public void setPassword(String password) {
this.password = password;
} @Column(name = "is_delete")
public Boolean getIsDelete() {
return this.isDelete;
} public void setIsDelete(Boolean isDelete) {
this.isDelete = isDelete;
} @OneToMany(fetch = FetchType.LAZY, mappedBy = "student")
public Set<Course> getCourses() {
return this.Courses;
} public void setTCourses(Set<Course> Coursea) {
this.Courses= Courses;
}}
Course类:
@Entity
@Table(name = "course", catalog = "db")
public class Course implements java.io.Serializable { private Integer id;
private String name;
private Boolean isDelete; @Id
@GeneratedValue(strategy = IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
} public void setId(Integer id) {
this.id = id;
} @Column(name = "name")
public String getName() {
return this.name;
} public void setName(String name) {
this.name = name;
} @Column(name = "is_delete")
public Boolean getIsDelete() {
return this.isDelete;
} public void setIsDelete(Boolean isDelete) {
this.isDelete = isDelete;
}}我写的对吗?
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;@Entity
@Table(name = "STUDENT", schema = "TEST")
public class Student implements java.io.Serializable { // Fields private String stid;
private String stname;
private Set<StudentCourse> studentCourses = new HashSet<StudentCourse>(0);
@Id
@Column(name = "STID", unique = true, nullable = false, length = 40)
public String getStid() {
return this.stid;
} public void setStid(String stid) {
this.stid = stid;
} @Column(name = "STNAME", length = 40)
public String getStname() {
return this.stname;
} public void setStname(String stname) {
this.stname = stname;
} @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "student")
public Set<StudentCourse> getStudentCourses() {
return this.studentCourses;
} public void setStudentCourses(Set<StudentCourse> studentCourses) {
this.studentCourses = studentCourses;
}}
package com.hibernate;import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "COURSE", schema = "TEST")
public class Course implements java.io.Serializable { // Fields private String coid;
private String coname;
private Set<StudentCourse> studentCourses = new HashSet<StudentCourse>(0);
@Id
@Column(name = "COID", unique = true, nullable = false, length = 40)
public String getCoid() {
return this.coid;
} public void setCoid(String coid) {
this.coid = coid;
} @Column(name = "CONAME", length = 40)
public String getConame() {
return this.coname;
} public void setConame(String coname) {
this.coname = coname;
} @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "course")
public Set<StudentCourse> getStudentCourses() {
return this.studentCourses;
} public void setStudentCourses(Set<StudentCourse> studentCourses) {
this.studentCourses = studentCourses;
}}
package com.hibernate;import java.math.BigDecimal;
import javax.persistence.AttributeOverride;
import javax.persistence.AttributeOverrides;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name = "STUDENT_COURSE", schema = "TEST")
public class StudentCourse implements java.io.Serializable { // Fields private StudentCourseId id;
private Course course;
private Student student;
private BigDecimal score;
@EmbeddedId
@AttributeOverrides( {
@AttributeOverride(name = "stid", column = @Column(name = "STID", nullable = false, length = 40)),
@AttributeOverride(name = "coid", column = @Column(name = "COID", nullable = false, length = 40)) })
public StudentCourseId getId() {
return this.id;
} public void setId(StudentCourseId id) {
this.id = id;
} @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "COID", nullable = false, insertable = false, updatable = false)
public Course getCourse() {
return this.course;
} public void setCourse(Course course) {
this.course = course;
} @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "STID", nullable = false, insertable = false, updatable = false)
public Student getStudent() {
return this.student;
} public void setStudent(Student student) {
this.student = student;
} @Column(name = "SCORE", precision = 22, scale = 0)
public BigDecimal getScore() {
return this.score;
} public void setScore(BigDecimal score) {
this.score = score;
}}
package com.hibernate;import javax.persistence.Column;
import javax.persistence.Embeddable;@Embeddable
public class StudentCourseId implements java.io.Serializable { // Fields private String stid;
private String coid;
public StudentCourseId() {
} /** full constructor */
public StudentCourseId(String stid, String coid) {
this.stid = stid;
this.coid = coid;
} @Column(name = "STID", nullable = false, length = 40)
public String getStid() {
return this.stid;
} public void setStid(String stid) {
this.stid = stid;
} @Column(name = "COID", nullable = false, length = 40)
public String getCoid() {
return this.coid;
} public void setCoid(String coid) {
this.coid = coid;
} public boolean equals(Object other) {
if ((this == other))
return true;
if ((other == null))
return false;
if (!(other instanceof StudentCourseId))
return false;
StudentCourseId castOther = (StudentCourseId) other; return ((this.getStid() == castOther.getStid()) || (this.getStid() != null
&& castOther.getStid() != null && this.getStid().equals(
castOther.getStid())))
&& ((this.getCoid() == castOther.getCoid()) || (this.getCoid() != null
&& castOther.getCoid() != null && this.getCoid()
.equals(castOther.getCoid())));
} public int hashCode() {
int result = 17; result = 37 * result
+ (getStid() == null ? 0 : this.getStid().hashCode());
result = 37 * result
+ (getCoid() == null ? 0 : this.getCoid().hashCode());
return result;
}}