0

================ Original ISSUE (NOT duplicated) ==================

I am using Spring MVC, maven, Spring Data JPA, hsqldb... I made Spring Data JPA Entities, Repositories and Services(declare interfaces). (all related code is posted with comments [1],[2])

and generally, working ok. however....

one of custom repository function gets error even before compilation.

  • These two repository functions to search Task in db with different id (each task entity contains two different ids : TaskId, task_id)

  • 'Invalid derived query! No property find found for type Task!'

My code looks like:

=== TaskRepository.java ======

public interface TaskRepository extends JpaRepository<Task, Integer>{

    Task findByTaskId(String taskId); //[1] works find

    Task findOnebyTaskid(String task_id); // [2][Error message] Invalid derived query! No property find found for type Task!

========= TaskService.java ================

@Service
public class TaskService {  

    @Autowired
    private TaskRepository taskRepository;

    public Task findByTaskID(String taskId){    //[1] works find
        return taskRepository.findByTaskId(taskId);
    }


    public Task findOnebyTaskid(String task_id){  // [2]
        return taskRepository.findOnebyTaskid(task_id);
    }

========= Task.java =======================

@Entity
public class Task{

    //[1] works find
    private String taskId;

    public String getTaskId() {
        return taskId;
    }

    @Column(unique = true)  // [Q]necssary or not?
    public void setTaskId(String taskId) { 
        this.taskId = taskId;
    }

    // [2]
    private String task_id;

    public String getTask_id() {
        return task_id;
    }

    public void setTask_id(String task_id) {
        this.task_id = task_id;
    }

====== Test.java ================

Task findtaskbyID = taskService.findByTaskID(taskID); //[1] - works find

Task findbytaskid = taskService.findOnebyTaskid(task_id);  // [2]

I am using these versions (just in case, if need to check):

spring framework: ver 4.0.2.RELEASE
hsqldb: ver 2.3.2
spring-data-jpa: ver 1.7.2.RELEASE
hibernate-entitymanager: ver 4.3.8.Final

Thanks in advance. [Solution]

@Query("select t from Task t where t.task_id = ?1")
Task findOnebyTaskid(String task_id);

==== UPDATED ISSUE (Duplicated but Not the original issue) ===========

After apply this @Query("select t from Task t where t.task_id = ?1") by advice by @RC :

TaskRepository.java ...

@Query("select t from Task t where t.task_id = ?1")
Task findOnebyTaskid(String task_id);

it works for the first time, then it gets error from second time of editing other tasks.

Caused by: 
java.lang.NullPointerException
    at my.forecastAway.controller.TaskController.editTask(TaskController.java:352)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:749)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:690)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:945)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:876)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:711)
    at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1644)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
user1915570
  • 386
  • 2
  • 7
  • 27
  • Did you read the [manual](http://docs.spring.io/spring-data/jpa/docs/current/reference/html/)? You should tell us what you are trying to achieve here, what is the expected "SELECT" for "findOnebyTaskid", did you try "findByTask_id"? Also note that having two "Task ID" will probably be a great source of confusion in days to come –  May 04 '15 at 11:19
  • I try to find a Task entity which is saved in hsqldb to update changed value. I have two ids in each Task. because first id (task_id) is generated automatically from the client. so when I save a new Task , I save task_id in db as well. but somehow this automated id from client is duplicated (very rarely.). It is very rare but it happens and because it is automated from the client, I have no power to manuplate it. so I generated another id when I save new Task, that's TaskId. Therefore an saved entity in db has two ids. – user1915570 May 04 '15 at 11:26
  • Then, for my main purpose here, I need to update the exisiting entity with edited information, So, I need to find the entity in db. Therefore I made both repository functions, one with automated id (TaskId), another with custom generated id (task_id). So I can double check for the founded entity is really the right one or not and then I can update it with edited information. – user1915570 May 04 '15 at 11:29
  • and yes, I read the manual. but I am a newbie this kind of programming. so I don't understand everything in the manual yet. Some explanations are very general.. and I don't have ability to interprete exactly and apply on my case yet. so I appreciate if you mention What exactly you are suspicious about based on the manual? You could have something in mind? – user1915570 May 04 '15 at 11:30
  • I asked about the manual because of the name of your method. If "findByTask_id" doesn't work you could use `@Query` to define the query by hand and bypass spring "automagic" –  May 04 '15 at 11:40
  • I have added `@Query("select u from Task u")Task findOnebyTaskid(String task_id);` but it is not working as well.. I never knew about `@Query` and I don't know I am doing it right... . – user1915570 May 04 '15 at 12:33
  • 1
    You probably need `@Query("select t from Task t where t.task_id = ?1")`, see [here](http://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.at-query) –  May 04 '15 at 12:39
  • Thanks. it works for the first time. but it gets error from the second time..I updated question with the console message. – user1915570 May 04 '15 at 14:40
  • ok. Now I can see my the task_id randomly changes when the client leaves the page and return to page. so that's not same task_id anymore.. therefore it is null pointer.. Now it is another proglem... so task_id from the client is randomly changes when it leaves the page , that's why task_id duplicates sometimes when it needs to create new....I haven't observed that before.. but @RC Thanks a lot helping out. Thumbs up ! – user1915570 May 04 '15 at 15:06

0 Answers0