我的架构如下所示:
我的NOMINATION
表保存了我所有四个提名之间的共同数据。
我有4种提名类型:
同事提名
团队提名
创意提名
成功提名
现在,如您所见,我仅对4个表中的3个进行了建模,这是因为在此旧数据模型上,“同事提名”所需的唯一数据在“提名”表中。
因此,基本上我在列DiscriminatorColumn
上使用了CATEGORY_CODE
进行区分,并制成5个实体,其中4个是从主要Nomination
实体继承的。Nomination
实体-> CoworkerNom
,TeamNom
,IdeaNom
,SuccessNom
。
我有两个问题:
1.为什么我的特定DAO无法从其特定表中获取列?
例如:
提名.java:
@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;
//So on
IdeaNom.java:
@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;
//so on
AuditableEntity.java:
@MappedSuperclass
public abstract class AuditableEntity {
/**
* The user who last updated this object. This object is lazy loaded, so
* make sure a session is open
*/
@ManyToOne(cascade = {CascadeType.PERSIST,
CascadeType.MERGE}, fetch = FetchType.LAZY /*optional = true*/)
@JoinColumn(name = "UPDATED_BY", referencedColumnName = "EMP_ID",
nullable = true)
@Cascade(value = {org.hibernate.annotations.CascadeType.PERSIST,
org.hibernate.annotations.CascadeType.MERGE,
org.hibernate.annotations.CascadeType.SAVE_UPDATE})
private Employee modifiedBy = null;
/**
* The last system update timestamp for this entity
*/
@Column(name = "UPDATED_DATE", nullable = true)
@Temporal(value = TemporalType.TIMESTAMP)
private Date modifiedOn = null;
//getters and setters
我的findByAll方法看起来像这样:
public List<T> findAll() {
if (logger.isDebugEnabled()) {
logger.debug("findAll");
}
return currentSession().createCriteria(getPersistentClass()).list();
}
和我的DAO,基本上是从此方法所在的
AbstractHibernateDAO
继承的。它类似于AbstractHibernateDAO
-> NominationHibernateDAO
-> IdeaNomHibernateDAO
。我收到以下错误:
Caused by: java.sql.SQLException: Invalid column name 'ACTUAL_SITUATION'.
at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:368)
at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2820)
at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2258)
at net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:632)
at net.sourceforge.jtds.jdbc.JtdsStatement.executeSQLQuery(JtdsStatement.java:477)
at net.sourceforge.jtds.jdbc.JtdsPreparedStatement.executeQuery(JtdsPreparedStatement.java:778)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:96)
at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
at org.hibernate.loader.Loader.getResultSet(Loader.java:1953)
at org.hibernate.loader.Loader.doQuery(Loader.java:802)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
at org.hibernate.loader.Loader.doList(Loader.java:2533)
... 38 more
ACTUAL_SITUATION
是Idea_Nom列,因此我猜想它会停在NOMINATION
的公用列上,而不是获取特定的列。2.我如何能够获得所有提名的列表,而不管其类型如何?我可以单独对提名实体运行通用DAO吗?正如我提到的那样,这可能是一个愚蠢的问题,但我已经有了一个
NominationHibernateDAO
,但我只是好奇,因为到目前为止,我的count方法仅基于DiscriminatorColumn返回特定的计数... 最佳答案
在Nomination
中,您没有指定继承策略,我认为您应该使用JOINED策略:
@Entity(name = "Nomination")
@Table(name = "NOMINATION")
@Inheritance(strategy=InheritanceType.JOINED)
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;
...
}
@Entity
@PrimaryKeyJoinColumn(name="NOM_ID")
public class IdeaNom extends Nomination { ... }
您应该查看Mapping Entities
(2)如果您对层次结构的根目录
Nomination
进行查询,请使用JPA,例如:select n from Nomination n
它是一个多态查询,它也加载
Nomination
子类。