Oirampok
Oirampok

Reputation: 649

Spring JPA repository cannot resolve property of entity

Im trying to do a simple search for two columns for a table.

My customer entity class snipet:

@Entity
@Table(name = "CUSTOMER")
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "CUSTOMER_ID", nullable = false)
    private int customer_id;

    @Column(name = "FIRST_NAME", nullable = false)
    private String first_name;

    @Column(name = "LAST_NAME", nullable = false)
    private String last_name;

    @Column(name = "PHONE_NUMBER", nullable = false)
    private String phone_number;

    @Column(name = "EMAIL")
    private String email;

my customerRepository class snipet:

@Repository
public interface CustomerRepository extends JpaRepository<Customer, Integer> {

    List<Customer> findAllByFirst_nameAndLast_name(String firstName, String lastname);

}

enter image description here

When compiling and running springBoot i get this error:

nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.owl.owlserver.repositories.CustomerRepository.findAllByFirst_nameAndLast_name(java.lang.String,java.lang.String)! No property first found for type Customer!

So its not able to detect the fields in the customer object, should I be importing the customer class into the repository somehow?

Upvotes: 3

Views: 3414

Answers (3)

Abhinav Tripathi
Abhinav Tripathi

Reputation: 21

The whole point of Spring JPA is to simplify query building using the methods defined in Repository. The convention is to use the camelCase for naming field in Entity and the same while writing query methods. Just change the naming of entity fields from snake case to camel case and you're good to go.

Upvotes: 1

Ryuzaki L
Ryuzaki L

Reputation: 39978

I would recommend keeping the properties in a camel-case as per standards

Because we treat the underscore character as a reserved character, we strongly advise following standard Java naming conventions (that is, not using underscores in property names but using camel case instead).

    @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "CUSTOMER_ID", nullable = false)
private int customerId;

@Column(name = "FIRST_NAME", nullable = false)
private String firstName;

@Column(name = "LAST_NAME", nullable = false)
private String lastName;

@Column(name = "PHONE_NUMBER", nullable = false)
private String phoneNumber;

@Column(name = "EMAIL")
private String email;

And then query as well as per Property Expressions

List<Customer> findAllByFirstNameAndLastName(String firstName, String lastname);

Upvotes: 1

Patrycja Wegrzynowicz
Patrycja Wegrzynowicz

Reputation: 737

The problem lies in your snake_case naming convention, it's recommended to use camelCase naming convention.

As the Spring docs say:

Because we treat the underscore character as a reserved character, we strongly advise following standard Java naming conventions (that is, not using underscores in property names but using camel case instead).

You can see a related JIRA issue here -- underscores are not supported in property names.

Upvotes: 3

Related Questions