Hibernate with annotations:“实体映射中的重复列” - 复合PK

时间:2013-07-30 01:29:46

标签: java hibernate annotations composite-primary-key

首先,我要感谢大家甚至读这篇文章。

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

Diagram

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

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。如果有人知道这个错误的确切原因和/或如何解决它,请分享知识!

1 个答案:

答案 0 :(得分:1)

我发现您在[此帖子](Hibernate Mapping Exception : Repeated column in mapping for entity)中可能遇到同样的问题。

也许你可以使用代理键作为id或使用查询来检索应用程序。

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

仅将Application和Module的标识保留为ModuleApplication中的字段。在给定ModuleApplication

的情况下,您可以使用查询来检索应用程序或模块
public class ModuleApplication {

    private ModuleApplicationId id;

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

Application application = applicationRepository.findBy(moduleApplication.getApplicationId());