正在更新子实体



这有点冗长,但却是一个简单的概念问题。请花些时间阅读:(

我有两个列完全相同的表,只是它们的主键不同。因此,我创建了一个具有公共列的@MappedSuperclass,并用@Entity注释了子类

@MappedSuperclass
public abstract class AbstractCorporateAction {
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "ticker_serial", nullable = false, insertable = false, updatable = false)
@ToString.Exclude
private Ticker ticker;
@Column(name = "corp_act_type", nullable = false, precision = 4)
private BigInteger corpActType;
@Column(name = "number_of_shares", precision = 20)
private BigInteger numberOfShares;
}

以下是儿童课程:

@Entity
@Table(name = "corporate_actions", schema = "dfn_content_data")
public class ContentDataCorporateAction extends AbstractCorporateAction {
@Id
@Column(name = "action_id", precision = 10)
private BigInteger id;
}
@Entity
@Table(name = "corporate_actions", schema = "dfn_ms_history")
public class MsHistoryCorporateAction extends AbstractCorporateAction {
@Id
@Column(name = "id")
private BigInteger id;
@Column(name = "source_action_id", length = 20, unique = true)
private String sourceActionId;
}

您可以看到一个子类的主键是action_id,而另一个子类只是id。我的任务是从两者中检索行,进行一些计算,更新相应的字段并返回。

我做了所有的,除了最后一部分,那就是推回到DB。我现在有一个数据列表:List<AbstractCorporateAction>,我想向后推。

  1. 我尝试通过扩展extends JpaRepository<AbstractCorporateAction, BigInteger>来创建一个DAO。但由于AbstractCorporateAction中没有@Id属性而失败

    org.springframework.beans.factory.BeanCreationException:创建名称为"abstractCorporateActionDao"的bean时出错,该名称在com.gtn.ca_ajustments.repository.cacalculation.abstractCorporateActionDao中定义,在CalculationPersistenceConfiguration上声明的@EnableJpaRepositorys中定义:调用init方法失败;嵌套异常为java.lang.IllegalArgumentException:此类[class com.gtn.ca_adjustments.entity.caculation.AbstractCorporateAction]未定义IdClass

  2. 然后我尝试将其转换为继承表

    @实体@继承(策略=继承类型.TABLE_PER_CLASS(公共抽象类AbstractCorporateAction{}

由于没有通用Id,再次失败。

在这一点上我能做什么?

我试过:

interface ContentDataCorporateActionDao extends JpaRepository<ContentDataCorporateAction, BigInteger> {
}
interface MsHistoryCorporateActionDao extends JpaRepository<MsHistoryCorporateAction, BigInteger> {
}
@Repository
public class AbstractCorporateActionDao {
// Autowire both
private final ContentDataCorporateActionDao contentDataCorporateActionDao;
private final MsHistoryCorporateActionDao msHistoryCorporateActionDao;
@Transactional
public void saveAll(List<AbstractCorporateAction> abstractCorporateActions) {

// 1. filter all actions belong to first child class into a list
// 2. filter all actions belong to second child class into another list

// 3. for each list -> saveALl() to corresponding dao

}
}

这种方法是唯一的吗?这对任务来说似乎太过分了。

您可以通过以下方式稍微简化它:

iterate over the list.
foreach:
do an instanceof check
save with the matching repository.

因此,无需创建中间列表。由于您的JPA实现无论如何都会延迟更新,因此使用saveAll不会获得任何好处。

相关内容

  • 没有找到相关文章

最新更新