JPA从主键更改为复合主键错误类必须使用一致的访问类型

时间:2017-08-06 14:16:22

标签: java hibernate jpa

所有这一切都是新的,所以如果有任何遗漏或不正确,请告诉我。 最初我创建了这个类,它只使用表的主键,如下所示:

@Entity
@Table(name="serviceRequestActivities"
,catalog="Maven_new"
, uniqueConstraints = @UniqueConstraint(columnNames="serviceReqNr") 
)

public class ServiceRequestActivities  implements java.io.Serializable,DbEntity {

private int activityNr;
private ServiceRequest serviceRequest;
...

@Id 
@Column(name="activityNr", unique=true, nullable=false)
public int getActivityNr() {
    return this.activityNr;
}

public void setActivityNr(int activityNr) {
    this.activityNr = activityNr;
}

@Override
@Transient
public Object getId(){
    return getActivityNr();
}

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="serviceReqNr", nullable=false)
public ServiceRequest getServiceRequest() {
    return this.serviceRequest;
}

public void setServiceRequest(ServiceRequest serviceRequest) {
    this.serviceRequest = serviceRequest;
}

... the other setters and getters

}

然后是需求的一些变化,我必须使用复合主键来获取要更新的对象。我改了它,现在看起来像这样:

@Entity
@IdClass(MyKey.class)
@Table(name="serviceRequestActivities"
,catalog="Maven_new"
, uniqueConstraints = @UniqueConstraint(columnNames="serviceReqNr") 
)

public class ServiceRequestActivities  implements java.io.Serializable,DbEntity {

@Id private int activityNr;
@Id private ServiceRequest serviceRequest;
...

public class MyKey implements java.io.Serializable {
    @Column(name="activityNr", nullable=false)
    private int activityNr;
    private ServiceRequest serviceRequest;

    public int getActivityNr() {
        return this.activityNr;
    }

    public void setActivityNr(int activityNr) {
        this.activityNr = activityNr;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof ServiceRequestActivities)) return false;
        ServiceRequestActivities activity = (ServiceRequestActivities) o;
        return getId() != null && Objects.equals(getId(), activity.getId());
    }

    @Override
    public int hashCode() {
        return 31;
    }

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name="serviceReqNr", nullable=false)
    public ServiceRequest getServiceRequest() {
        return this.serviceRequest;
    }

    public void setServiceRequest(ServiceRequest serviceRequest) {
        this.serviceRequest = serviceRequest;
    }
}
@Override
@Transient
public Object getId(){

    return getActivityNr();

}
...
the other setters and getters

}
由于抱怨错误,

尚无法完全实现更改 "该类必须使用一致的访问类型(字段或属性) 没有为此实体层次结构定义ID" 不确定什么不正确或缺少什么。

0 个答案:

没有答案