首先,我要感谢所有阅读本文的人。
我的应用程序中的 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。如果有人知道此错误的确切原因和/或如何解决它,请分享知识!