1

I have three entities. (I am using xxx as a place holder for this example)

I have set up all their @Entities with

@Entity
@Table(name = "xxx")
public class xxx {

@Id
@Column(name = "xxx_id")
int xxx_id;

However, I am able to do:

findById(int ...) for only ONE of the entities in their respective Repository. When I try to do that for the other two entities, I get this problem:

"Invalid derived query! No property id found for type xxx!"

I am able to run it and get the proper results. But why am I getting this error for two entities but not the other?

I set up the entities and their repositories exactly the same way. The ONLY difference is that in the entity whose repository does not return any errors, I am joining that entity with another entity whose repository fails, but I used "mappedBy" in the @JoinColumns section.

Could that have something to do with why that one has no problems?

Romil Patel
  • 12,879
  • 7
  • 47
  • 76
justTryin
  • 15
  • 5
  • 3
    `findById` method needs to have a field named `id` in the entity class. However in your `xxx` class, you have `xxx_id`, not `id`. – Johna Jul 09 '19 at 22:32

1 Answers1

3

How findBy... works?

Entity:
private int clientid;
private String firstname;
private String lastname;

Query:

findBy<Name_as_per_entity>
findByClientid(int clientid);
findByFirstnameAndLastname(String firstname, String lastname)

Solution

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).

Doc

The underscore _ is a reserved character in Spring Data query derivation to potentially allow manual property path description.

Stick to the Java naming conventions of using camel-case for member variable names and everything will work as expected.

Also Refer this

Romil Patel
  • 12,879
  • 7
  • 47
  • 76
  • 1
    I agree with this answer. Use CamelCase and write findByxxxId(int xxxId) – Neeraj Gupta Jul 10 '19 at 05:11
  • 1
    Thank you. That link you provided was very helpful in determining how SQL breaks down the name. I made my variables like: "xxxId" the @Column like "xxx_id" And the repository functions like "findByxxxId" – justTryin Jul 10 '19 at 15:12