gpt4 book ai didi

java - 实体继承: central table with common values + extended father entity

转载 作者:太空宇宙 更新时间:2023-11-04 08:22:39 25 4
gpt4 key购买 nike

我的架构如下所示:

Schema

我的 NOMINATION 表保存了我所有 4 项提名之间的通用数据。

我有 4 种类型的提名:

  1. 同事提名
  2. 团队提名
  3. 创意提名
  4. 成功提名

现在,如您所见,我只对 4 个表中的 3 个进行了建模,这是因为在此旧数据模型上,同事提名所需的唯一数据位于提名表中。

所以基本上我在 CATEGORY_CODE 列上使用 DiscriminatorColumn 来区分并创建了 5 个实体,其中 4 个继承自主 Nomination 实体。

提名实体 -> CoworkerNomTeamNomIdeaNomSuccessNom

我有两个问题:

<强>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 的公共(public)列上,而不是获取特定列。

<强>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 子类。

关于java - 实体继承: central table with common values + extended father entity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9229776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com