Home > Software design >  How do I return a list of entity's instance with either jpql or jpa
How do I return a list of entity's instance with either jpql or jpa

Time:11-26

I have a Subject class which references Category class. The Subject class has this simplified signature:

@Entity
@Table(name="Subject")
public class Subject{

//Omitted codes
@Column(name = "date")
public Date date;

@ManyToOne
@JoinColumn(name="course_category", nullable=false)
private Category category;
//Omitted codes

}

The Category is like this:

@Entity
@Table(name="Category")
public class Category {
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
//omitted codes

@OneToMany(cascade=CascadeType.ALL, mappedBy="category")
private Set<Subject> subject=new HashSet<>();

//Omitted codes
}

Now I want to return a list of date from my SubjectDao whenever I pass id that matches a particular Category id, but my method doesn't work.

This is SubjectDao

public interface SubjectDao extends JpaRepository<Subject, Integer>{

@Query("Select s.date from Subject s Where s.category.id=:id")
Page<Date> findDates(@RequestParam("id") int id, Pageable pegeable);
}

This is the error I keep getting

Couldn't find persistentEntity for type class java.sql.Timestamp...

Please how should I write the jpa query?

I've tried to adopt jpa projection technique so I added the following interface

public interface DatesOnly{
public Date getDate();
}

Then reimplemented my SubjectDaolike this

public interface SubjectDao extends JpaRepository<Subject, Integer>{

@Query("Select s.date from Subject s where s.category.id =:id")
List<DatesOnly>findDates(@RequestParam("id")int id);

}

But when I run the code I get another confusing error like


org.springframework.data.mapping.MappingException: Couldn't find PersistentEntity for type class jdk.proxy4.$Proxy133
    at org.springframework.data.mapping.context.MappingContext.getRequiredPersistentEntity(MappingContext.java:80) ~[spring-data-commons-2.7.5.jar:2.7.5]
    at org.springframework.data.mapping.context.PersistentEntities.getRequiredPersistentEntity(PersistentEntities.java:116) ~[spring-data-commons-2.7.5.jar:2.7.5]
    at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.wrap(PersistentEntityResourceAssembler.java:90) ~[spring-data-rest-webmvc-3.7.5.jar:3.7.5]
    at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.toModel(PersistentEntityResourceAssembler.java:73) ~[spring-data-rest-webmvc-3.7.5.jar:3.7.5]
    at org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.toModel(PersistentEntityResourceAssembler.java:34) ~[spring-data-rest-webmvc-3.7.5.jar:3.7.5]
    at org.springframework.data.web.PagedResourcesAssembler.createModel(PagedResourcesAssembler.java:210) ~[spring-data-commons-2.7.5.jar:2.7.5]
    at org.springframework.data.web.PagedResourcesAssembler.toModel(PagedResourcesAssembler.java:122) ~[spring-data-commons-2.7.5.jar:2.7.5]
    at org.springframework.data.rest.webmvc.AbstractRepositoryRestController.lambda$entitiesToResources$3(AbstractRepositoryRestController.java:95) ~[spring-data-rest-webmvc-3.7.5.jar:3.7.5]
    at java.base/java.util.Optional.orElseGet(Optional.java:364) ~[na:na]
    at org.springframework.data.rest.webmvc.AbstractRepositoryRestController.entitiesToResources(AbstractRepositoryRestController.java:95) ~[spring-data-rest-webmvc-3.7.5.jar:3.7.5]
    at org.springframework.data.rest.webmvc.AbstractRepositoryRestController.toCollectionModel(AbstractRepositoryRestController.java:78) ~[spring-data-rest-webmvc-3.7.5.jar:3.7.5]
    at org.springframework.data.rest.webmvc.RepositorySearchController.lambda$toModel$1(RepositorySearchController.java:204) ~[spring-data-rest-webmvc-3.7.5.jar:3.7.5]
    at java.base/java.util.Optional.map(Optional.java:260) ~[na:na]
    at org.springframework.data.rest.webmvc.RepositorySearchController.toModel(RepositorySearchController.java:201) ~[spring-data-rest-webmvc-3.7.5.jar:3.7.5]
    at org.springframework.data.rest.webmvc.RepositorySearchController.executeSearch(RepositorySearchController.java:185) ~[spring-data-rest-webmvc-3.7.5.jar:3.7.5]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:na]
    at java.base/java.lang.reflect.Method.invoke(Method.java:567) ~[na:na]
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-5.3.23.jar:5.3.23]
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) ~[spring-web-5.3.23.jar:5.3.23]
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) ~[spring-webmvc-5.3.23.jar:5.3.23]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) ~[spring-webmvc-5.3.23.jar:5.3.23]
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808) ~[spring-webmvc-5.3.23.jar:5.3.23]
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.3.23.jar:5.3.23]
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1071) ~[spring-webmvc-5.3.23.jar:5.3.23]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:964) ~[spring-webmvc-5.3.23.jar:5.3.23]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006) ~[spring-webmvc-5.3.23.jar:5.3.23]
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898) ~[spring-webmvc-5.3.23.jar:5.3.23]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:670) ~[tomcat-embed-core-9.0.68.jar:4.0.FR]
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883) ~[spring-webmvc-5.3.23.jar:5.3.23]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:779) ~[tomcat-embed-core-9.0.68.jar:4.0.FR]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53) ~[tomcat-embed-websocket-9.0.68.jar:9.0.68]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100) ~[spring-web-5.3.23.jar:5.3.23]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.23.jar:5.3.23]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93) ~[spring-web-5.3.23.jar:5.3.23]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.23.jar:5.3.23]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201) ~[spring-web-5.3.23.jar:5.3.23]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:117) ~[spring-web-5.3.23.jar:5.3.23]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:197) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:541) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:360) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1789) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.68.jar:9.0.68]
    at java.base/java.lang.Thread.run(Thread.java:831) ~[na:na]

My pom.xml file

<modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.edugreat</groupId>
    <artifactId>akademiks-resource</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>akademiks-resource</name>
    <description>Akademiks Resource</description>
<properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

CodePudding user response:

I was able to use your models almost without modification, I converted your primitives to wrapped versions. I'm not sure which Date you're using, but this should work with either java.util.Date or java.sql.Date. I used java.util.Date for this example.

On your DAO, I used the declarative property-driven approach as opposed to writing JPQL. I only use JPQL if the queries are complex.

public interface SubjectRepository extends JpaRepository<Subject, Integer> {
    List<DatesOnly> findAllByCategoryId(Integer categoryId);
}

I threw together a simple controller just to set up some test data.

@RestController
@RequestMapping("/subject")
public class SubjectController {
    private final SubjectRepository subjectRepository;

    public SubjectController(SubjectRepository subjectRepository) {
        this.subjectRepository = subjectRepository;
    }

    @PostMapping
    public Subject createSubject(@RequestBody Subject subject) {
        return subjectRepository.save(subject);
    }

    @GetMapping("/{id}")
    public List<DatesOnly> getDates(@PathVariable Integer id) {
        return subjectRepository.findAllByCategoryId(id);
    }
}

I initialized the database Category table with a single record (I added the name field just to have some data in the table).

insert into category(name) values ('test')

I posted the following to /subject (see controller). I posted this five times to have some sample data.

{
    "category": {
        "id": 1
    },
    "date": "2022-11-24T19:07:19.097303"
}

Next I hit the GET endpoint (localhost:8080/subject/1) with the id of the Category to get back the List<DatesOnly>. You can see the projection works just as we'd expect.

[
  {
    "date": "2022-11-24T19:07:19.097 00:00"
  },
  {
    "date": "2022-11-24T19:07:19.097 00:00"
  },
  {
    "date": "2022-11-24T19:07:19.097 00:00"
  },
  {
    "date": "2022-11-24T19:07:19.097 00:00"
  },
  {
    "date": "2022-11-24T19:07:19.097 00:00"
  }
]

If you're absolutely deadset on using JPQL, you can, here is a very slight modification to your query to make this work. You'll find that I dropped the s.date because that query just returns a collection of Dates, but your projection calls for the response to have a getDate() method, which is lacking when you just return a collection of Dates. So, return from the query a collection of Subject, this will have the getDate() method the projection requires.

@Query("Select s from Subject s Where s.category.id=:id")
List<DatesOnly> findDatesProjectedBySomeId(Integer id);

Here are a few things you should consider:

  1. You have @RequestParam added to your repository method; drop this as it does nothing.
  2. Don't mix primitives and wrapped types. It will work, but it causes unnecessary boxing and unboxing. If you're working with JPA entities, I prefer always to use the wrapped type because they are nullable, whereas the primitives are not.
  3. In Subject, consider not naming your field date; this is a reserved word in almost every DBMS and I'm pretty shocked that I was even allowed to create a table with this as the name of a column.
  4. On your OneToMany/ManyToOne, you should annotate them with JsonManagedReference and JsonBackReference or you're going to probably end up with a StackOverflow when these data are serialized.
  • Related