Could not resolve target entity

JPA: Could not resolve target entity

Fixed BugsJava

Problem: When you are trying to use @Query and returns next error:

Could not resolve target entity

Solution: You have to use as table name, the name of the Java class. Check if it is correct in the @Query and check that you are not using the database name of the table. YOU HAVE TO USE THE JAVA’S CLASS NAME OF THE ENTITY.

Example:

//This is the repository class
@Entity
@Table(name = "CAR_TABLE")
class CarDto {
 String colour;
 String numberOfDoors;
}
@Repository
public interface CarRepository extends JpaRepository<CarDto, String> {

//Wrong, the name 'Cor' is not correct
@Modifying
@Query("update Cor c set c.color = :color where c.numberOfDoors = 5")
void wrongMethod1(@Param("color") String color);

//Wrong, the name 'CAR_TABLE' could be the name of the table in database, but you don't have to use it
@Modifying
@Query("update CAR_TABLE c set c.color = :color where c.numberOfDoors = 5")
void wrongMethod2(@Param("color") String color);

//Correct
@Modifying
@Query("update CarDto c set c.color = :color where c.numberOfDoors = 5")
void correctMethod(@Param("color") String color);
}

Source: documentation

For more interesting tutorials & guides just check them HERE.

Leave a Reply

Your email address will not be published. Required fields are marked *