0

首先,我要感谢所有阅读本文的人。

我的应用程序中的 Hibernate fw 有问题。下图显示了导致错误的数据库部分的示意图。

图表
(来源:akamaihd.net

我的代码是由逆向工程自动生成的,为了使其工作,我必须“显示”代码生成器 module_application 表不是多对多表(使用自定义逆向工程策略)。现在,当我运行我的代码时,我收到以下错误:

Error creating Session: org.hibernate.MappingException: Repeated column in mapping for entity: DAL.module_application.ModuleApplication column: application_id (should be mapped with insert="false" update="false")

ModuleApplication 类如下图所示:

@Entity
@Table(name = "module_application", catalog = "domotics")
public class ModuleApplication implements java.io.Serializable
{

    /**
     * 
     */
    private static final long serialVersionUID = 4725933797587110677L;
    private ModuleApplicationId id;
    private Application application;
    private Module module;
    private Set<ModuleApplicationStatus> moduleApplicationStatuses = new HashSet<ModuleApplicationStatus>(0);

    public ModuleApplication()
    {
    }

    public ModuleApplication(ModuleApplicationId id, Application application, Module module)
    {
        this.id = id;
        this.application = application;
        this.module = module;
    }

    public ModuleApplication(ModuleApplicationId id, Application application, Module module, Set<ModuleApplicationStatus> moduleApplicationStatuses)
    {
        this.id = id;
        this.application = application;
        this.module = module;
        this.moduleApplicationStatuses = moduleApplicationStatuses;
    }

    @EmbeddedId
    @AttributeOverrides(
    {
        @AttributeOverride(name = "networkAddress", column = @Column(name = "network_address", nullable = false, length = 45)),
        @AttributeOverride(name = "applicationId", column = @Column(name = "application_id", nullable = false)) 
    })
    public ModuleApplicationId getId()
    {
        return this.id;
    }

    public void setId(ModuleApplicationId id)
    {
        this.id = id;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "application_id", nullable = false, insertable = false, updatable = false)
    public Application getApplication()
    {
        return this.application;
    }

    public void setApplication(Application application)
    {
        this.application = application;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "network_address", nullable = false, insertable = false, updatable = false)
    public Module getModule()
    {
        return this.module;
    }

    public void setModule(Module module)
    {
        this.module = module;
    }

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "moduleApplication")
    public Set<ModuleApplicationStatus> getModuleApplicationStatuses()
    {
        return this.moduleApplicationStatuses;
    }

    public void setModuleApplicationStatuses(Set<ModuleApplicationStatus> moduleApplicationStatuses)
    {
        this.moduleApplicationStatuses = moduleApplicationStatuses;
    }
}

正如我们所见,insert 和 update 都设置为 false。如果有人知道此错误的确切原因和/或如何解决它,请分享知识!

4

1 回答 1

1

我发现您在 [this post] 中可能有同样的问题(Hibernate Mapping Exception : Repeated column in mapping for entity)。

也许您可以使用代理键作为 id 或使用查询来检索 Application。

您现在正在使用对象图解决方案,或者您可以使用查询解决方案:

仅将 Application 和 Module 的标识保留为 ModuleApplication 中的字段。您可以使用查询来检索给定 ModuleApplication 的 Application 或 Module

public class ModuleApplication {

    private ModuleApplicationId id;

    private Set<ModuleApplicationStatus> moduleApplicationStatuses = new HashSet<ModuleApplicationStatus>(0);
}

Application application = applicationRepository.findBy(moduleApplication.getApplicationId());
于 2013-07-30T03:46:06.967 回答