2 penweizgx002 penweizgx002 于 2015.07.24 16:01 提问

Hibernate4 复合主键没法使用缓存啊

代码如下,在执行查询的时候,缓存无效果,非复合主键的都正常进入缓存

 @Embeddable 
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class StudentClassesPK implements Serializable{
    private static final long serialVersionUID = 8086967036014159916L;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "student_id",referencedColumnName="id",nullable=true)  
    private Student student;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "classes_id",referencedColumnName="id",nullable=true )  
    private Classes classes;
    /**
     * 进入某个班级的时间
     */
    private Date startDate = new Date();

    public StudentClassesPK(){

    }

    public Student getStudent() {
        return student;
    }
    public void setStudent(Student student) {
        this.student = student;
    }
    public Classes getClasses() {
        return classes;
    }
    public void setClasses(Classes classes) {
        this.classes = classes;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((classes == null) ? 0 : classes.hashCode());
        result = prime * result + ((startDate == null) ? 0 : startDate.hashCode());
        result = prime * result + ((student == null) ? 0 : student.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;
        StudentClassesPK other = (StudentClassesPK) obj;
        if (classes == null) {
            if (other.classes != null)
                return false;
        } else if (!classes.equals(other.classes))
            return false;
        if (startDate == null) {
            if (other.startDate != null)
                return false;
        } else if (!startDate.equals(other.startDate))
            return false;
        if (student == null) {
            if (other.student != null)
                return false;
        } else if (!student.equals(other.student))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "StudentClassesPK [student=" + student + ", classes=" + classes + ", startDate=" + startDate + "]";
    }
}
 @Entity
@Table(name="T_ClassesStudent")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE,region="com.kmp.business.entity.StudentClasses")
public class StudentClasses extends BaseEntity{
    private static final long serialVersionUID = -6300584354851171783L;

    @EmbeddedId
    StudentClassesPK pk = new StudentClassesPK();

    /**
     * 离开某个班级的时间
     */
    private Date endDate;

    public StudentClassesPK getPk() {
        return pk;
    }
    public void setPk(StudentClassesPK pk) {
        this.pk = pk;
    }

    public Date getEndDate() {
        return endDate;
    }
    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }
    @Override
    public String toString() {
        return "StudentClasses [pk=" + pk + ", endDate=" + endDate + "]";
    }

1个回答

CSDNXIAOD
CSDNXIAOD   2015.07.24 16:06

Hibernate 使用复合主键
----------------------biu~biu~biu~~~在下问答机器人小D,这是我依靠自己的聪明才智给出的答案,如果不正确,你来咬我啊!

Csdn user default icon
上传中...
上传图片
插入图片