简体   繁体   English

Spring Data JPA + Hibernate:如何使用抽象超类上的存储库进行对象检索?

[英]Spring Data JPA + Hibernate: How do I use a repository on abstract super class for object retrieval?

I am asking as a follow up to this question . 我要求对此问题采取后续行动。 I am doing the exact same thing and it does not work, meaning it still gives an Exception indicating attempting to instantiate an abstract class: 我正在做完全相同的事情,它不起作用,这意味着它仍然给出一个异常,指示试图实例化一个抽象类:

org.springframework.orm.jpa.JpaSystemException: org.hibernate.InstantiationException: Cannot instantiate abstract class or interface org.springframework.orm.jpa.JpaSystemException:org.hibernate.InstantiationException:无法实例化抽象类或接口

The only difference is that I do not set my classes as implementing the Serializable interface, but I figured that is probably not the reason, so I suspect the Spring and Hibernate version. 唯一的区别是我没有将我的类设置为实现Serializable接口,但是我认为这可能不是原因,因此我怀疑是Spring和Hibernate版本。

so basically the abstract class looks something like this: 所以基本上抽象类看起来像这样:

@Entity
@Inheritance(strategy = InheritanceType.JOINED)
@Table(name="activities")
@DiscriminatorColumn(name="type", discriminatorType=DiscriminatorType.STRING)
public abstract class Activity
{
    @Id
    @Column(length=Fields.ID_FIELD_SIZE)
    protected String id;
    ....

and two sub-classes 和两个子类

@Entity
@Table(name="a_activities")
@DiscriminatorValue("A")
public class AActivity extends Activity
{
...

and

@Entity
@Table(name="b_activities")
@DiscriminatorValue("B")
public class BActivity extends Activity
{
...

When using the ActivityRepository to fetch an Activity by Id it gives the mentioned exception. 当使用ActivityRepository通过ID提取活动时,它会给出上述异常。

@Qualifier("activity-repository")
public interface ActivityRepository extends JpaRepository<Activity, String>
{
}

I am using Spring Data 1.2.0 and Hibernate 4.1.7. 我正在使用Spring Data 1.2.0和Hibernate 4.1.7。

Sorry for asking as a separate question but I could not comment on the original one 很抱歉提出一个单独的问题,但我无法评论原始问题

I managed to resolve this issue. 我设法解决了这个问题。 The main reason this was happening is that this hierarchy was introduced into our existing system which means we had data in the database before this change. 发生这种情况的主要原因是,将这种层次结构引入了我们现有的系统中,这意味着在进行此更改之前,我们已经在数据库中存储了数据。 The Activity class was a concrete class before and the tables for the sub-classes did not exist. Activity类以前是一个具体的类,并且子类的表不存在。 When adding this hierarchy we created the tables for the sub-classes; 当添加此层次结构时,我们为子类创建了表。 however, what I missed was the need to insert an entry for each Activity object in the proper table for the sub-class which it instantiates. 但是,我错过的是需要在实例化的子类的正确表中为每个Activity对象插入一个条目。

I tried this on one entry and I managed to successfully fetch that Activity by Id. 我在一个条目上尝试了此操作,然后成功通过ID成功获取了该Activity。 So we should run scripts to properly insert sub-class entries for all existing activity entries in the proper tables. 因此,我们应该运行脚本以在适当的表中为所有现有活动条目正确插入子类条目。

It turns out this answer already notes this point but somehow I noted the check on the Discriminator values but missed the need to check that each entry has a row in the corresponding sub-class table. 事实证明, 这个答案已经说明了这一点,但是以某种方式我注意到对Discriminator值的检查,但是错过了检查每个条目在相应的子类表中是否有一行的需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM