Spring Data Recovery Error (SDR)? Persistent entity must not be null

I am currently working on a POC for Spring Data Rest. Trying to get workable JSONout from the repository.

I have an Entity class (NewTask)

@Entity @Table(name="newtable") public class NewTask { @Id @Column(name="newid") private int id; @Column(name="newage") private int age; @Column(name="newaddress") private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } } 

and corresponding repository.

  @RepositoryRestResource @PreAuthorize("hasRole('ROLE_ADMIN')") public interface NewTaskRepository extends CrudRepository<NewTask, Serializable>{ @Query("SELECT t.address FROM NewTask t where t.id = :id") String findByMyId(@Param("id") int id); } 

whenever i click

 http://localhost:8080/POCDB/newTasks/search/findByMyId?id=1 

I get the following error: {"cause": null, "message": "PersistentEntity must not be null!" }

Now here's what my repository looks like: read the comments for each method

  //Gives- PersistentEntity must not be null!!! @Query("SELECT t.address FROM NewTask t where t.id = :id") String findByMyId(@Param("id") int id); //WORKS FINE @Query("SELECT t.id FROM NewTask t where t.id = :id") int findId(@Param("id") int id); //WORKS FINE @Query("SELECT t.id FROM NewTask t where t.id = :id") Integer findIdTwo(@Param("id") int id); //Gives- PersistentEntity must not be null!!! @Query("SELECT t.id FROM NewTask t") List<Integer> findIds(); 

I'm not sure what the problems are with return data types. I have provided the link below for some solution:

How to create a custom query in jparepository but return an object other than the object?

and added 2 more methods to my repository that don't work for me

  // returns in some weird format @Query("SELECT t.address FROM NewTask t where t.id = :id") PString findByMyId(@Param("id") int id); //Gives- PersistentEntity must not be null!!! @Query("SELECT t.address FROM NewTask t") List<PString> findAddress(); 

Do I have a suspicion that this is an error in the SDR, or am I missing something?

+6
source share
3 answers

Spring Data REST can only return data for which a repository is registered. In the other question you refer, you will notice that they created a special repository specifically for the type that they need. This is not an error in the SDR, it is just how it functions. He also keeps it RESTful.

So, in your case, the SDR can only return NewTask or NewTask collections.

+8
source

In Spring Download, if the main Spring application cannot be verified for your domain (POJO) classes, this error will be thrown if you do not put all the classes in the same package. Then you add the component check annotation on top of Spring of the main application class

 @ComponentScan(basePackages = "com.aaa.bbbb.ccccc") 
0
source

I use Spring Data Mongo, not JPA, but it helped me to include the _class field in the select select Mongo statement. Spring uses this field to map the database results back to the entity / document.

Also pay attention to @TypeAlias so that you have a constant _class value when moving between domain classes.

I also ran into the same problem when pre-projecting database results using Spring projections . Later, when I converted the results to paginated resources, the environment was not able to define the projection interface as a constant entity and, therefore, could not find the necessary metadata necessary to perform the conversion for me. The solution for this use case was a custom resource assembler.

0
source

Source: https://habr.com/ru/post/985992/


All Articles