Øystein Amundsen
Øystein Amundsen

Reputation: 4203

Spring JpaRepository find entities from attribute using unconventional naming

The following used to work in Spring 1.5.10.RELEASE, but does not work in Spring 2.0.7.RELEASE, and I do not know why:

Entity

@Entity
@Table(name = "locations")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Location {
  // ... unimportant stuff
  @Column(name = "c_locations_id")
  private String cLocationId;
  // ... more unimportant stuff
}

Repository (aka "The problem")

@Repository
public interface LocationRepository extends JpaRepository<Location, Long>, JpaSpecificationExecutor<Location> {
  Location findByCLocationId(String cLocationId);
  List<Location> findAllByOrderByCLocationIdAsc();
}

The error I'm getting under Spring 2.0.7.RELEASE for the above code, is

java.lang.IllegalArgumentException: Unable to locate Attribute with the the given name [CLocationId] on this ManagedType.

I cannot change the name of the attribute due to other circumstances, so I've tried different variations on the methods in the repository:

What does it want?! I just want to upgrade the framework... 😭

Upvotes: 4

Views: 1156

Answers (2)

Hakob Hakobyan
Hakob Hakobyan

Reputation: 1131

You can use method name like this:

Location findByC_Location_Id(String cLocationId);

this can be helpful with references

Upvotes: 5

David P&#233;rez Cabrera
David P&#233;rez Cabrera

Reputation: 5048

You can use @Query annotation in your methods official documentation.

@Query("select l from Location l where l.cLocationId = ?1")
Location findByCLocationId(String cLocationId);

@Query("select l from Location l")
List<Location> findAllByOrderByCLocationIdAsc();

Upvotes: 2

Related Questions