how to create a composite primary key hibernate JPA?

旧时模样 提交于 2019-11-29 12:28:05

Remove constructor from

@EmbeddedId
private ServiceDepartmentPK serviceDepartmentPK;

Remove @ManyToOne annotation from ServiceDepartmentPK class and set fields types to long or int

@Embeddable

public class ServiceDepartmentPK  implements Serializable {

private static final long serialVersionUID = 1L;
@Column(name="COLUMN_NAME")
private long clientId;

@Column(name="COLUMN_NAME")
private long carServiceId;

public ServiceDepartmentPK  () {
}
// getters setters for client and carserver ids-s

public boolean equals(Object other) {
    if (this == other) {
        return true;
    }
    if (!(other instanceof ServiceDepartmentPK  )) {
        return false;
    }
    ServiceDepartmentPK  castOther = (ServiceDepartmentPK  )other;
    return 
        (this.scopeId == castOther.clientId)
        && (this.scriptId == castOther.carServiceId);

}

public int hashCode() {
    final int prime = 31;
    int hash = 17;
    hash = hash * prime + ((int) (this.clientId ^ (this.clientId>>> 32)));
    hash = hash * prime + ((int) (this.carServiceId ^ (this.carServiceId>>> 32)));

    return hash;
}

}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!