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?
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.