本文介绍了Hibernate:映射中的重复列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在这个例子中,我有一个出勤DTO和一个工人DTO,在这种情况下,工人被部门分隔开来,而一个工人只能在一个部门内部工作。需要注意的是,

您应该移除 @Column private String workerId; 关系到 WorkerDto 。



如果你想创建关系,你应该使用 setWorkerDto在 AttendencetDto 中方法并保存。交易结束后,您将在DB中拥有您的关系。


So in this example scenario I have an attendance DTO, and a worker DTO, workers in this context are separated by department, and a worker can only ever be inside of one department. It is important to note that Worker {id='123-123-123', department='a'} is different to Worker {id='123-123-123', department='b'}, despite them both sharing the same Id.

I have the following class setup to try and separate functions by id and department

public class IdAndDepartmentPK implements Serializable {

    private String id;
    private String department;

    public IdAndDepartmentPK() {}
    ...
}

This key class is shared between DTOs that require both the Worker's id and department, below are the two DTOs that are causing a problem.

@Entity
@IdClass(IdAndDepartmentPK.class)
public class AttendencetDto {

    @Id private String id; // This is a departmentally unique attendenceId
    @Id private String department;
    @Column private String workerId;

    @JoinColumns({
        @JoinColumn(name = "workerId"),
        @JoinColumn(name = "department")
    })
    @ManyToOne(fetch = FetchType.EAGER)
    private WorkerDto workerDto;
    ....
 }

 @Entity
 @IdClass(IdAndDepartmentPK.class)
 public class WorkerDto {

    @Id
    private String id;

    @Id
    private String department;
    ...
 }

WorkerDto does not need to have knowledge of AttendencetDto, but AttendencetDto, does need to have access to WorkerDto and the other data it contains.

Hibernate complains that fields like workerId should be mapped with insert="false" update="false", but if I was to do this then I wouldn't be able to persist those values to the database.

I essentially want to have those fields available whilst also having the WorkerDto available, is this possible?

解决方案

You should remove @Column private String workerId; because you already map it by relation to WorkerDto.

If you want to create relation between that you should use setWorkerDto method in your AttendencetDto and just save. After transaction ends you will have your relation in DB.

这篇关于Hibernate:映射中的重复列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 08:34