Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 409 Vote(s) - 3.42 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Best way of handling entities inheritance in Spring Data JPA

#1
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.
Reply

#2
I used the solution also described in [this post][1] from Netgloo's blog.

The idea is to create a *generic* repository class like the following:

@NoRepositoryBean
public interface ABaseRepository<T extends A>
extends CrudRepository<T, Long> {
// All methods in this repository will be available in the ARepository,
// in the BRepository and in the CRepository.
// ...
}

then I can write the three repositories in this way:

@Transactional
public interface ARepository extends ABaseRepository<A> { /* ... */ }

@Transactional
public interface BRepository extends ABaseRepository<B> { /* ... */ }

@Transactional
public interface CRepository extends ABaseRepository<C> { /* ... */ }

Moreover, to obtain a read-only repository for `ARepository` I can define the `ABaseRepository` as read-only:

@NoRepositoryBean
public interface ABaseRepository<T>
extends Repository<T, Long> {
T findOne(Long id);
Iterable<T> findAll();
Iterable<T> findAll(Sort sort);
Page<T> findAll(Pageable pageable);
}

and from `BRepository` extend also the Spring Data JPA's `CrudRepository` to achieve a read/write repository:

@Transactional
public interface BRepository
extends ABaseRepository<B>, CrudRepository<B, Long>
{ /* ... */ }


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through