I was wondering if anyone can give feedback on a pattern that I am currently playing around with? It involves having an entity implementing a DTO interface that is also used (as a projection) in a JpaRepository interface – for the same entity – to return query results with specific columns. The DTO interface also has default methods to allow any instances of the entity and the DTO proxies to have similar behaviours.
The questions I am looking to have answered are whether this pattern has any drawbacks such as performance that might prevent one from using it in production. I am also interested to hear how others are querying specific data fields using JpaRepositories. Below I have a code sample that illustrates the pattern that I am playing with.
public interface InstructorDTO {
String getFirstName();
String getLastName();
default String getFullName() {
return getFirstName() + ' ' + getLastName();
}
}
@Entity
public class Instructor implements InstructorDTO {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
private int id;
@Column(name="first_name")
private String firstName;
@Column(name="last_name")
private String lastName;
@Column(unique = true)
private String email;
@Override
public String getFirstName() {
return this.firstName;
}
@Override
public String getLastName() {
return this.lastName;
}
...remaining getters and setters
}
@Repository
public interface InstructorRepository extends JpaRepository<Instructor, Integer> {
<S> S findById(int id, Class<S> type);
<T> Collection<T> findByEmail(String email, Class<T> type);
}
public class SomeClass {
@Autowired
InstructorRepository instructorRepository;
public void someMethod {
int id = 1;
// Returns proxy
InstructorDTO instructor1 = instructorRepository.findById(id, InstructorDTO.class);
// Returns Instructor Object
Instructor instructor2 = instructorRepository.findOne(id);
System.out.println(instructor1.getFullName()); // returns John Doe
System.out.println(instructor2.getFullName()); // returns John Doe
}
}