Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

528
Visualizações
Spring Data JPA inheritance in repositories

I have an abstract class annotated with @MappedSuperClass. There are around 15 entities that extend this abstract class (having 15 corresponding tables in the database). The 15 entities all have the same attributes that are inherited from the abstract super class.

I have created a repository as below for the abstract class:

@NoRepositoryBean
public interface AbstractRepository <T extends AbstractClass, E extends Serializable>
                                    extends PagingAndSortingRepository<T, Serializable> {
  .....some methods here
}

The 15 entities/tables store some data (data pertaining to 15 separate equipment). Based on the equipment selected, the data from that table is to be retrieved. Will I have to create 15 separate repositories for the 15 concrete entities or is there any way to get the specific entity for the equipment selected using only the abstract repository? If repositories need to be created for each concrete entity, how to get the right repository for the specific equipment call? (store the table name and repository class in a map that gets created on startup of the application perhaps?)

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Ideally, the following set up should work:

Entity classes

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class Equipment { ... }

@Entity
public class Crane extends Equipment {}

@Entity
public class Excavator extends Equipment {}

Repository interface

public interface EquipmentRepository<T extends Equipment> extends CrudRepository<T> {}

Repository consumer

@Service
public class SomeService {
  @Autowired
  private EquipmentRepository<Crane> craneRepository;

  @Autowired
  private EquipmentRepository<Excavator> excavatorRepository;
}

I have set up a sample project on Github to demonstrate this in action. Running Maven tests as any of the following will show passing tests to demonstrate that the set up works as expected.

mvn test -Dspring.profiles.active="default,eclipselink"

mvn test -Dspring.profiles.active="default,openjpa"

That said, this set up may not work with Hibernate, which is what I suspect you are using. Hibernate does not support auto-incremented identifier columns with TABLE_PER_CLASS inheritance. Therefore, attempting to run the set up above with Hibernate will throw an exception. This can be verified by running the following on the sample linked above.

mvn test -Dspring.profiles.active="default,hibernate"
about 4 years ago · Santiago Trujillo Relatório

0

You will have to create repository for each class. However you can keep your methods in the abstract. You'll need to provide @Query on each of the methods and use SpeL(Spring Expression Language) to add the type to the queries.

@NoRepositoryBean
public interface AbstractRepository<T extends AbstractEquipment> 
        extends CrudRepository<T, Long>{

 @Query("select e from #{#entityName} as e from equipment where e.name = equipmentName")
 T findEquipmentByName(String equipmentName);

}

Then extend like the following

@Transactional
public interface SpecialEquipmentRepo extends AbstractRepository<SpecialEquipment,Long>{

}
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda