实体映射中的重复列 [英] Repeated column in mapping for entity

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

问题描述

当我坚持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;
}


更新

我复制了与您相同的案例.我认为这是某种错误,因为isUpdatableisInsertable返回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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆