how to create a composite primary key hibernate JPA?

后端 未结 1 1706
逝去的感伤
逝去的感伤 2020-12-20 07:31

i try to create composite primary key in table from 2 foreign key using hibernate JPA,but gives me error.I find some solution on net but nothing

The relations betw

1条回答
  •  伪装坚强ぢ
    2020-12-20 07:57

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

    }

    0 讨论(0)
提交回复
热议问题