Reputation: 401
I'm trying to create an abstract service in my springMVC application. My data flows* perfect through my layers, but my persistence (Abstraction), could not find a way to save/delete/update my database, these are the classes:
abstract class BaseServiceImpl<T, ID extends Serializable>
implements BaseService<T, ID> {
protected abstract BaseRepository<T, ID> getRepository();
private BaseRepository<T, ID> repository;
public BaseServiceImpl(BaseRepository<T, ID> repository){
this.repository = repository;
}
public List<T> findAll(){
return getRepository().findAll();
}
public T find(ID id){
return getRepository().findOne(id);
}
public T save(T persisted){
return this.repository.save(persisted);
}
public void delete(ID id){
getRepository().delete(id);
}
public long count(){
return getRepository().count();
}
public boolean exists(ID id){
return getRepository().exists(id);
}
}
//---
@Service
public class MedicineServiceImpl
extends BaseServiceImpl<Medicine, Integer>
implements MedicineService {
@Autowired
private MedicineRepository repository;
@Autowired
public MedicineServiceImpl(MedicineRepository repository) {
super(repository);
}
@Override
public BaseRepository<Medicine, Integer> getRepository() {
return repository;
}
}
//--
@MappedSuperclass
abstract class BaseDomain {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
}
//--
@Entity
@Table(name = "medicines")
public class Medicine
extends BaseDomain implements Serializable{
@NotNull
private String name;
private String description;
private String purpose;
@Column(name = "active_principle")
private String activePrinciple;
@Column(name = "therapeutic_class")
private String therapeuticClass;
@Column(name = "ean_code")
private String eanCode;
@Column(name = "ms_code")
private String msCode;
@Column(name = "avisa_warning")
private String anvisaWarning;
@Column(name = "lab_name")
private String labName;
@Column(name = "lab_phone")
private String labPhone;
@NotNull
private String contraindication;
public Medicine() {
}
}
//--
@NoRepositoryBean
public interface BaseRepository<T,ID extends Serializable>
extends Repository<T,ID> {
List<T> findAll();
long count();
void delete(ID id);
T save(T s);
T findOne(ID id);
boolean exists(ID id);
void delete(T deleted);
}
//--
@Repository
public interface MedicineRepository
extends BaseRepository<Medicine, Integer>{
}
I got 500 status error and my tocatlog shows the following message:
SEVERE: Servlet.service() for servlet [TavalendoDispatcherServlet] in context with path [/tavalendo] threw exception [Request processing failed; nested exception is org.springframework.beans.NotReadablePropertyException: Invalid property 'id' of bean class [java.util.LinkedHashMap]: Could not find field for property during fallback access!] with root cause org.springframework.beans.NotReadablePropertyException: Invalid property 'id' of bean class [java.util.LinkedHashMap]: Could not find field for property during fallback access! at org.springframework.data.util.DirectFieldAccessFallbackBeanWrapper.getPropertyValue(DirectFieldAccessFallbackBeanWrapper.java:56) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:956) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:423) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1079) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:625) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745)
Does anyone can explain what happend?
Upvotes: 2
Views: 12004
Reputation: 71
I had ran with same issue while using Projection in JPA.
Error Code:
Optional<List<AbcProj>> getData(--);
Resolved after making below changes:
List<AbcProj> getData(--);
Upvotes: 2
Reputation: 401
I ran into a similar issue recently and I was getting a similar exception when trying to create/update my resources:
Invalid property 'id' of bean class [org.springframework.hateoas.CollectionModel]: Could not find field for property during fallback access!
After looking closely I seem to used an incorrect HTTP request header (Content-Type: text/uri-list instead of Content-Type: application/json) when making the call to the API due to which it wasn't able to deserialize the JSON payload.
Just sharing in case somebody runs into something similar, it may save them some time when debugging
Upvotes: 1
Reputation: 1
1) Converter Object to String
private String jsonObjectToString( T value ) throws AppException
{
try
{
ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
String strJson = ow.writeValueAsString( value );
return strJson;
}
catch ( JsonProcessingException e )
{
LOG.error( e.getMessage(), e );
throw new AppException( e );
}
}
2) Converter String to Object
@SuppressWarnings( "unchecked" )
private T jsonStringToObject( String strJson, Class<?> clazz ) throws AppException
{
try
{
ObjectMapper mapper = new ObjectMapper();
T object = ( T ) mapper.readValue( strJson, clazz );
return object;
}
catch ( IOException e )
{
LOG.error( e.getMessage(), e );
throw new AppException( e );
}
}
3) Transforms the generic T to reset the Object Reference
private void yourMethod( CommonRq<T> rq ) throws AppException {
String newStrJson = jsonObjectToString( rq.getData() );
T newEntity = jsonStringToObject( newStrJson, getEntityClass() );
T result = this.mantenimientoService.create( newEntity );
}
This has worked for me....
Upvotes: 0
Reputation: 4137
It seems Spring cannot find the id field of Medicine:
Invalid property 'id' of bean class [java.util.LinkedHashMap]: Could not find field for property during fallback access!]
Actually, Medicine does not seem to have an id
field, so you need to add it.
Upvotes: 0