实体映射中的重复列 [英] Repeated column in mapping for entity
问题描述
当我坚持WorkAction
时,我需要存储WorkflowInstance
的FK ID.
When I persist a WorkAction
I need to store the FK Id for WorkflowInstance
WorkAction wa = new WorkAction();
wa.setWorkflowInstance(wfi);
waDao.persist(wa);
但是在WorkAction
表的fk列中,workflow_instance_id
最后显示为空.
However in the table for WorkAction
the fk column, workflow_instance_id
winds up being null.
在实体中找到WorkAction
我已经像这样使用, insertable = false, updatable = false
映射WorkflowInstance
Turns out in the Entity for WorkAction
I had mapped WorkflowInstance
using , insertable = false, updatable = false
like this
@ManyToOne
@JoinColumn(name = "workflow_instance_id", referencedColumnName = "workflow_instance_id", insertable = false, updatable = false)
private WorkflowInstance workflowInstance;
当我删除它时,我得到了Repeated column in mapping for entity: WorkAction column: workflow_instance_id
,但是在WorkAction
实体中,我没有看到workflow_instance_id
再次映射,即我没有private Long workflow_instance_id;
When I removed it, I got the Repeated column in mapping for entity: WorkAction column: workflow_instance_id
But in the WorkAction
entity I didn't see workflow_instance_id
mapped again , i.e. I did not have private Long workflow_instance_id;
但是后来我注意到在我的WorkAction
实体中,我已经使用引用workflow_instamce_id
But then I noticed in my WorkAction
entity, I had mapped a WorkflowInstancePlayer
using a formula which referenced workflow_instamce_id
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumnsOrFormulas({ @JoinColumnOrFormula(formula = @JoinFormula(value = "(SELECT a.role_class_id FROM WF_WORK_ACTION_CLASS a WHERE a.work_action_class_id = work_action_class_id)", referencedColumnName = "role_class_id")),
@JoinColumnOrFormula(column = @JoinColumn(name = "workflow_instance_id", referencedColumnName = "workflow_instance_id", insertable=false, updatable=false)) })
private WorkflowInstancePlayer player;
即使将insertable=false, updatable=false
添加到该映射后,我仍然遇到问题.当我注释掉@JoinColumnsOrFormulas
时,可以将WorkflowInstance
上的insertable=false, updatable=false
删除,然后工作流实例ID不为null,但随后我丢失了WorkflowInstancePlayer
Even after adding insertable=false, updatable=false
to that mapping I still have the issue. When I comment out the @JoinColumnsOrFormulas
I can remove the insertable=false, updatable=false
on the WorkflowInstance
and the workflow_instance_id is then not null but I then lose my WorkflowInstancePlayer
我该如何解决?
推荐答案
在@JoinColumn
批注中使用insertable = false, updatable = false
.
public class WorkAction {
@Column(name="workflow_instance_id")
private Long workflow_instance_id;
@ManyToOne
@JoinColumn(name = "workflow_instance_id", referencedColumnName = "workflow_instance_id", insertable = false, updatable = false)
private WorkflowInstance workflowInstance;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumnsOrFormulas({
@JoinColumnOrFormula(formula = @JoinFormula(value = "(SELECT a.role_class_id FROM WF_WORK_ACTION_CLASS a WHERE a.work_action_class_id = work_action_class_id)", referencedColumnName = "role_class_id")),
@JoinColumnOrFormula(column = @JoinColumn(name = "workflow_instance_id", referencedColumnName = "workflow_instance_id", insertable=false, updatable=false)) })
private WorkflowInstancePlayer player;
}
更新
我复制了与您相同的案例.我认为这是某种错误,因为isUpdatable
和isInsertable
返回true.
I reproduced the same case as you did. I think this is some kind of bug because isUpdatable
and isInsertable
returns true.
protected void checkColumnDuplication(Set distinctColumns, Iterator columns)
throws MappingException {
while ( columns.hasNext() ) {
Selectable columnOrFormula = (Selectable) columns.next();
if ( !columnOrFormula.isFormula() ) {
Column col = (Column) columnOrFormula;
if ( !distinctColumns.add( col.getName() ) ) {
throw new MappingException(
"Repeated column in mapping for entity: " +
getEntityName() +
" column: " +
col.getName() +
" (should be mapped with insert=\"false\" update=\"false\")"
);
}
}
}
}
protected void checkPropertyColumnDuplication(Set distinctColumns, Iterator properties)
throws MappingException {
while ( properties.hasNext() ) {
Property prop = (Property) properties.next();
if ( prop.getValue() instanceof Component ) { //TODO: remove use of instanceof!
Component component = (Component) prop.getValue();
checkPropertyColumnDuplication( distinctColumns, component.getPropertyIterator() );
}
else {
if ( prop.isUpdateable() || prop.isInsertable() ) {
checkColumnDuplication( distinctColumns, prop.getColumnIterator() );
}
}
}
}
源代码来自org.hibernate.mapping.PersistentClass
这篇关于实体映射中的重复列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!