我正在尝试测试一些DAO。
我有一个名为Nomination
的实体(和表),它具有一些属性:
@Entity(name = "Nomination")
@Table(name = "NOMINATION")
@DiscriminatorColumn(name="CATEGORY_CODE", discriminatorType = DiscriminatorType.STRING, length = 1)
public class Nomination extends AuditableEntity {
@Id
@Column(name = "NOM_ID", insertable = true, updatable = true,
nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Transient
protected NominationType type = null;
@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "PERIOD_ID", referencedColumnName = "PERIOD_ID")
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.MERGE})
private NomPeriod period = null;
@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "CATEGORY_CODE", referencedColumnName = "CATEGORY_CODE")
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.MERGE})
private Category category = null;
@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "NOMINATOR_ID", referencedColumnName = "EMP_ID")
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.MERGE})
private Employee nominator = null;
@ManyToOne(fetch = FetchType.LAZY, cascade = {CascadeType.PERSIST, CascadeType.MERGE})
@JoinColumn(name = "STATUS_ID", referencedColumnName = "STATUS_ID")
@Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
org.hibernate.annotations.CascadeType.MERGE})
private Status status = null;
//more after this line
现在,我有4种不同的提名类型(想法,团队,成功和同事提名)和4种不同的表格(提名,team_nom,idea_nom,success_nom)。一个(NOMINATION)具有公共列。这与我的第一种提名类型
CoworkerNom
相吻合。该表没有单独的表,因为它只需要NOMINATION表中列出的内容。其他3个提名是:TeamNom
,SuccessNom
和IdeaNom
。它们具有自己的表,因为它们具有其他数据,因此它们也具有自己的实体。例如:@Entity(name = "IdeaNom")
@Table(name = "IDEA_NOM")
@DiscriminatorValue("I")
public class IdeaNom extends Nomination {
@Column(name = "PURPOSE_INC", insertable = true, updatable = true,
nullable = true)
private Boolean purposeIncrease;
@Column(name = "PURPOSE_SIMPLIFY", insertable = true, updatable = true,
nullable = true)
private Boolean purposeSimplify;
//more after this line
此结构适用于
SuccessNom
和TeamNom
。我在了解如何执行此操作时遇到了问题,但是到目前为止,我已经完成了一些DAO并试图对其进行测试。但是我收到以下错误:
Caused by: org.hibernate.MappingException: Repeated column in mapping for entity:
com.dev.test.data.entity.SuccessNom column: CATEGORY_CODE (should be mapped with insert="false" update="false")
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:676)
at org.hibernate.mapping.PersistentClass.checkPropertyColumnDuplication(PersistentClass.java:698)
at org.hibernate.mapping.PersistentClass.checkColumnDuplication(PersistentClass.java:720)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:474)
at org.hibernate.mapping.SingleTableSubclass.validate(SingleTableSubclass.java:65)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1362)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1865)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.newSessionFactory(LocalSessionFactoryBean.java:860)
at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:779)
at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1479)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
... 54 more
首先,CATEGORY_CODE是区分符...但是它应该是可写的。希望你们能够再次帮助我。
编辑:这是我的架构
最佳答案
如果需要映射鉴别符列,请使用insert="false" update="false"
映射它。 Hibernate认为它已声明一次,并且不可写-只能由hibernate管理。另外,加入该列有点奇怪。