Goal: In order to make my Spring-Boot-Database code compatible with Android-Room-Database all my Spring-Boot-Repositoy classes
should have a method insert(T entity) and update(T entity).
Following the guide from https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.single-repository-behavior
part Example 34. Fragments overriding and part Example 35. Customized repository interfaces I tried to implement the new insert
method by calling the existing save method:
public class CustomRoomMethodsImpl<T,ID> ... {
public void insert(T entity) {
parent.save(entity);
}
}
My Question: where does the Java-Fragment code CustomRoomMethodsImpl get parent from (that has already implemented the save method)?
When I tried to add a parameter to the Java-Fragment code constructor
public CustomRoomMethodsImpl(SimpleJpaRepository<T, ID> parent) {
this.parent = parent;
}
I get the spring-boot error
Parameter 0 of constructor in org.fdroid.jpa.db.CustomRoomMethodsImpl required
a bean of type 'org.springframework.data.jpa.repository.support.SimpleJpaRepository'
that could not be found.
Action:
Consider defining a bean of type
'org.springframework.data.jpa.repository.support.SimpleJpaRepository'
in your configuration.
Since all my jpa repository classes should get their insert(T entity) and update(T entity) methods: how
could i provide access to the genrated class (CrudRepository<T, ID> or SimpleJpaRepository<T, ID>) ?
Here is my implementation that caused the spring-boot-error:
// CustomRoomMethods.java: Interface for Spring-Data-repository-fragment methods
@NoRepositoryBean
public interface CustomRoomMethods<T,ID> {
void insert(T entity);
void update(T entity);
}
// CustomRoomMethodsImpl.java: Implementatoin of Interface for Spring-Data-repository-fragment methods
public class CustomRoomMethodsImpl<T,ID> implements CustomRoomMethods<T,ID> {
private SimpleJpaRepository<T, ID> parent;
public CustomRoomMethodsImpl(SimpleJpaRepository<T, ID> parent) {
this.parent = parent;
}
@Override
public void insert(T entity) {
parent.save(entity);
}
@Override
public void update(T entity) {
parent.save(entity);
}
}
// UserRepositoryJpa.java Example JPA Repository that save User-Pojo-entity to Database
@Repository
public interface UserRepositoryJpa extends CrudRepository<User, Integer>, CustomRoomMethods<User,Integer> {
}
// User.java Example User-Pojo-entity that should be saved to Database
@Entity
public class Test {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public int id;
public String name;
}
Background: I try to implement a database compatibility layer for Spring-Boot-JPA/Android-Room
- In Jpa pojo-entities are ẁritten to the database using
repository.save(entity) - In Android-Room pojo-entities are written to the database using
dao.insert(entity)ordao.update(entity)
"compatibility layer" means my Service-layer-code is pure-non-android-code that can be used in android and in spring-boot. The Service-layer-code uses a common java-repository-interface that is either implemented in android-room or in JPA.