Spring数据JPA:如何使用抽象实体

时间:2019-03-19 17:45:42

标签: java spring hibernate spring-boot spring-data-jpa

我有一些名为MEMBER,PROVIDER,EMPLOYER等的表,它们有一些常见的列,如ID,ADDRESS等。我正在使用hibernate使用Spring Data JPA从这些表中获取数据。由于我有许多相似的表,因此我创建了一个超级实体类并编写了一个通用方法来获取数据。这是我的超级班。

    @Entity
    @Getter
    @Setter
    @NoArgsConstructor
    @ToString
    public abstract class Recipient implements Serializable {

      @Id
      @Column(name = "RECIPIENT_ID", unique = true, nullable = false)
      private Long id;
}

这是扩展此超类的示例实体。

@Entity
@Getter
@Setter
@NoArgsConstructor
@ToString
@Table(name = "MEMBER")
public class Member extends Recipient {

  @Column(name = "Member_NAME")
  private String membername;

}

这是服务

public class RecipientServiceImpl extends AbstractService<Recipient>
    implements RecipientService {
@Override
  public Recipient findByIdAndType(long id, String type) {
    Recipient recipient = null;
    switch (RecipientType.get(type)) {
      case MEMBER:
        recipient = recipientRepository.findMemberById(id);
      case PROVIDER:
        recipient = recipientRepository.findProviderById(id);
      case EMPLOYER:
        recipient = recipientRepository.findEmployerById(id);

    }
}

这是存储库

 public interface RecipientRepository
        extends JpaRepository<Recipient, Long>, JpaSpecificationExecutor<Recipient> {

        public Member findMemberById(long id);
        public Provider findProviderById(long id);
        public Employer findEmployerById(long id);
    }

显然,由于收件人抽象实体中没有映射任何表,因此无法正常工作。有没有一种方法可以实现我希望作为通用获取方法的方式,而不必为每个实体创建存储库?

0 个答案:

没有答案