Hibernate:映射中的重复列 [英] Hibernate: Repeated Column in Mapping
问题描述
Worker {id ='123-123-123',department ='a'}
与工人{id = '123-123-123',department ='b'}
,尽管他们都分享相同的ID。 我有以下班级设置为按 id
和部门
public class IdAndDepartmentPK实现Serializable {
私有字符串id;
私人字符串部门;
public IdAndDepartmentPK(){}
...
}
这个关键类在需要 Worker
的 id
和<$ c的DTO之间共享$ c> department ,下面是导致问题的两个DTO。
@Entity
@IdClass(IdAndDepartmentPK.class)
public class AttendencetDto {
@Id private String id; //这是部门唯一的attendenceId
@Id私人字符串部门;
@Column private String workerId;
$ b @JoinColumn({
@JoinColumn(name =workerId),
@JoinColumn(name =department)
})
@ ManyToOne(fetch = FetchType.EAGER)
私人WorkerDto workerDto;
....
}
@实体
@IdClass(IdAndDepartmentPK.class)
公共类WorkerDto {
@I $
私人字符串ID;
@Id
私人字符串部门;
...
}
WorkerDto code>不需要知道
AttendencetDto
,但 AttendencetDto
,需要有权访问 WorkerDto
和它包含的其他数据。
Hibernate抱怨像 workerId
应该被映射为insert =falseupdate =false,但是如果我这样做了,那么我将无法将这些值保存到数据库中。
我基本上希望可以使用这些字段,同时也可以使用 WorkerDto
,这可能吗?
您应该移除 @Column private String workerId;
关系到 WorkerDto
。
如果你想创建关系,你应该使用 setWorkerDto在
方法并保存。交易结束后,您将在DB中拥有您的关系。 AttendencetDto
中
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:映射中的重复列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!