08-02-2023, 07:26 AM
I've three **JPA entity** classes `A`, `B` and `C` with the following hierarchy:
A
|
+---+---+
| |
C B
That is:
@Entity
@Inheritance
public abstract class A { /* ... */ }
@Entity
public class B extends A { /* ... */ }
@Entity
public class C extends A { /* ... */ }
Using **Spring Data JPA**, what is the best way to write **repositories** classes for such entities?
I know that I can write these:
public interface ARespository extends CrudRepository<A, Long> { }
public interface BRespository extends CrudRepository<B, Long> { }
public interface CRespository extends CrudRepository<C, Long> { }
but if in the class `A` there is a field `name` and I add this method in the `ARepository`:
public A findByName(String name);
I've to write such method also in the other two repositories, and this is a bit annoying.. Is there a better way to handle such situation?
Another point I would like to have is that `ARespository` should be a read-only repository (i.e. extend the `Repository` class) while the other two repositories should expose all the CRUD operations.
Let me know possible solutions.
A
|
+---+---+
| |
C B
That is:
@Entity
@Inheritance
public abstract class A { /* ... */ }
@Entity
public class B extends A { /* ... */ }
@Entity
public class C extends A { /* ... */ }
Using **Spring Data JPA**, what is the best way to write **repositories** classes for such entities?
I know that I can write these:
public interface ARespository extends CrudRepository<A, Long> { }
public interface BRespository extends CrudRepository<B, Long> { }
public interface CRespository extends CrudRepository<C, Long> { }
but if in the class `A` there is a field `name` and I add this method in the `ARepository`:
public A findByName(String name);
I've to write such method also in the other two repositories, and this is a bit annoying.. Is there a better way to handle such situation?
Another point I would like to have is that `ARespository` should be a read-only repository (i.e. extend the `Repository` class) while the other two repositories should expose all the CRUD operations.
Let me know possible solutions.