mskwon
mskwon

Reputation: 73

how does ORM map to interface?

I am learning JPA Projections these days!
There is something strange that I can't understand.

the follow is my question

Spring Data JPA Repository

public interface PersonRepository extends JpaRepository<Person, Serializable> {

  Collection<NameAgeOnly> findByAgeGreaterThan(int age);
}

Interface(there is no Clazz which implement this interface)

public interface NameAgeOnly {
  String getName();

  String getAge();
}

RepositoryTest

public class RepositoryTest {
  List<CountByAddress> countByAddressList = personRepository.getCityNameAndPersonCount();
  debug.log(countByAddressList.get(0).getName());
}

How can countByAddressList.get(0).getName can work? there is no class which implements NameAgeOnly?

Is there any default class for Interface? Anyone who can explain How to ORM map to Interface Not Class?

thank you:)

Upvotes: 0

Views: 80

Answers (1)

JB Nizet
JB Nizet

Reputation: 691755

The documentation explains it:

The query execution engine creates proxy instances of that interface at runtime for each element returned and forwards calls to the exposed methods to the target object.

Upvotes: 2

Related Questions