不需要,只要加上对应annotation就行或是xml配置,如果通过hibernate自动建表会在数据库中生成对应的一张中间表

解决方案 »

  1.   


    每个课程只能有一个学生的时候才叫一对多。
    配置成多对多就不需要。
    是不是写成这样就可以
    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;
    }}我写的对吗?
      

  2.   

    具体细节不记的,很久不用了,不过你这上没有看见oneToMany之类的,你去看看hibernate一对多,多对多这些注解是怎么用的吧
      

  3.   

    我是设计好数据库了,反射生成。但是,Hibernate自动生成会生成中间表的实体类,所以我想请教,这张中间表,应该是怎么写的?需要手动修改吗?
      

  4.   

    看你喜好  反正hibernate支持不写就是多对多写就是两个一对一
      

  5.   

    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 = "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;
    }}
      

  6.   

    解决了http://blog.csdn.net/xiaobiaobiao521/article/details/9123303