jpa实体 - getter没有带来正确的结果

时间:2012-08-26 13:41:44

标签: hibernate jpa

我正在与Jpa,Hibernate合作 我的项目中发生了一件非常奇怪的事情 我有两个实体:学生和课程,多对多的关系 很长一段时间一切都很完美,突然间我遇到了这个问题:
当我找到一个特定的学生(em.find),然后返回其课程列表,使用getter(student.getCourses)我只收到列表的一部分!如果我用一个查询(“从学生的选择s.courses中找到id = ...”),它会给出正确的结果。
我试图解决它很长一段时间 - 什么都没有 任何人都知道问题可能是什么? 任何帮助将非常感谢!
这是我的代码:

/ **
 *实体的实体实施类:课程
 *
 * /
@Entity(name =“Courses”)

公共课课程延伸加盖{

private static final long serialVersionUID = 1L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String courseName;
@Version
@Column(name = "Version", nullable = false)
private Long version;

@ManyToMany(mappedBy = "courses",fetch = FetchType.LAZY)
private Set<Student> students = new HashSet<Student>();

@ManyToOne
@JoinColumn(name = "LecturerID")
private Lecturer lecturer;


public Course() {
    super();
    this.courseName = " ";

}

public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}

public String getCourseName() {
    return courseName;
}

public void setCourseName(String courseName) {
    this.courseName = courseName;
}

public Lecturer getLecturer() {
    return lecturer;
}

public void setLecturer(Lecturer lecturer) {
    this.lecturer = lecturer;
}

public Set<Student> getStudents() {
    return students;
}

public void setStudents(Set<Student> students) {
    this.students = students;
}


public Long getVersion() {
    return version;
}

public void setVersion(Long version) {
    this.version = version;
}

}

/ **
 *实体的实体实施类:学生
 *
 * /
@Entity

公共类学生扩展Person / ,Person类扩展Stamping / {

private static final long serialVersionUID = 1L;

@Enumerated(EnumType.STRING)
@Column(length = 2)
private E_Year studentYear;
private Boolean inCondition;

@ManyToMany(cascade = PERSIST, fetch = FetchType.LAZY)
private Set<Course> courses = new HashSet<Course>();

public Student() {
    super();
    this.inCondition = true;
}

public E_Year getStudentYear() {
    return studentYear;
}

public void setStudentYear(E_Year studentYear) {
    this.studentYear = studentYear;
}

public Boolean getInCondition() {
    return inCondition;
}

public void setInCondition(Boolean inCondition) {
    this.inCondition = inCondition;
}


public Set<Course> getCourses() {
    return courses;
}

public void setCourses(Set<Course> courses) {
    this.courses = courses;
}

}

/ **
 *实体的实体实施类:印章
 *
 * /
@MappedSuperclass

public class Stamping实现Serializable {

private static final long serialVersionUID = 1L;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created", nullable = false)
private Date created;

@Temporal(TemporalType.TIMESTAMP)
@Column(name = "updated", nullable = false)
private Date updated;

private String lastUser;

public Date getCreated() {
    return created;
}

public Date getUpdated() {
    return updated;
}

public String getLastUser() {
    return lastUser;
}

@PrePersist
protected void onCreate() {
    updated = created = new Date();
    lastUser = System.getProperty("user.name"); 
}

@PreUpdate
protected void onUpdate() {
    updated = new Date();
    lastUser = System.getProperty("user.name"); 
}

public Stamping() {
    super();
}

@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((created == null) ? 0 : created.hashCode());
    result = prime * result
            + ((lastUser == null) ? 0 : lastUser.hashCode());
    result = prime * result + ((updated == null) ? 0 : updated.hashCode());
    return result;
}

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Stamping other = (Stamping) obj;
    if (created == null) {
        if (other.created != null)
            return false;
    } else if (!created.equals(other.created))
        return false;
    if (lastUser == null) {
        if (other.lastUser != null)
            return false;
    } else if (!lastUser.equals(other.lastUser))
        return false;
    if (updated == null) {
        if (other.updated != null)
            return false;
    } else if (!updated.equals(other.updated))
        return false;
    return true;
}

}

1 个答案:

答案 0 :(得分:0)

问题解决了......
我发现这一切都发生了,因为我有一个学生和课程的基础类,它有从Eclipse生成的HashCode和Equals方法。
删除这些方法后,一切正常 任何人都可以解释为什么会发生吗? 感谢。