Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
254 views
in Technique[技术] by (71.8m points)

java - Spring Data Rest (SDR ) bug? Persistent Entity Must not be null

Currently I am working on a POC for Spring Data Rest. Trying to get workable JSONout of a 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 a 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 hit

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

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

Now here is how my repository looks: Please read the comments on 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 am not sure what are the issues with return types.I referred the link below for some solution:

How does one create a custom query in jparepository but return an object other than the entity?

and added 2 more methods to my Repository, which 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();

I have a hunch this is a bug in SDR, or am I missing something?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Spring Data REST can only return data for which the repository is registered for. In the other question that you reference, you'll notice that they created a custom repository specifically for the type that they needed. This isn't a bug in SDR, it's just how it functions. It also keeps it RESTful.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...