2015-01-15 353 views
4

我们有一个名为Location的类的项目,并成功实现了jparepositories,定义了几种搜索方法。我们希望有一个额外的搜索方法,可以返回一组街道名(从位置表中提取)而不是一组位置 - 所以我们可以在用户输入街道时实现自动填写。首先,我们尝试了@Query注释:如何在jparepository中创建自定义查询,但返回实体以外的对象?

@RepositoryRestResource(collectionResourceRel = "locations", path = "locations") 
public interface LocationRepository extends JpaRepository<Location, Integer>, LocationRepositoryCustom { 
    List<Location> findByStreetNameStartingWithIgnoreCase(@Param("street") String streetName); 

    @Modifying 
    @Query("select x.streetName from Location x where x.streetName like :street%") 
    List<String> findStreetNameStartingWith(@Param("street") String streetName); 
} 

如果我查询的程序并不存在一个街道(街道没有开始,X,例如),我得到了一个空集{}返回。如果我问的是确实存在一个街道(街道= BR,因为百老汇在数据库中存在),我得到

{"cause":null,"message":"PersistentEntity must not be null!"} 

然后我们试图执行它作为一个自定义查询,使用:

public interface LocationRepositoryCustom { 
    @Query("select x.streetName from Location x where x.streetName like :streetName") 
    public List<String> collectStreetNames(@Param("streetName") String streetName); 
} 

class LocationRepositoryImpl implements LocationRepositoryCustom { 

    @PersistenceContext 
    private EntityManager em; 

    @Override 
    public List<String> collectStreetNames(String streetName) { 
    List<String> retList = new ArrayList<String>(); 
    retList.add("start"); 
    retList.add("end"); 
    return retList; 
    } 
} 

这也给我们“PersistentEntity must not null”的错误。实现中的代码被用来返回一个硬编码的结果,所以我们不想弄清楚我们的SQL是错误的还是我们的体系结构。我们在调试下运行它,并确认返回了两个项目的列表。

该问题似乎是从存储库中返回除列表以外的任何内容。这是对这个架构的限制吗?还是有什么我们做错了,而且如果我们学会了秘密的握手,一切都将是疯狂的?

回答

0

我错过的线索是'PersistentEntity must not null'。存储库框架想要返回一个注册实体的列表 - 不是任何旧的POJO或原语。解决的办法是定义的查询可以返回一个实体:

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

    @Column(name="Name", length=40) 
    @Id 
    private String value; 

    public PString() { } 
    public PString(String name) { 
    this.value = name; 
    } 

    public String getValue() { 
    return value; 
    } 
    public void setValue(String value) { 
    this.value = value; 
    } 
} 

伴随着这一点,需要一个标准PStringRepository:

@RepositoryRestResource(collectionResourceRel = "strings", path = "strings") 
public interface PStringRepository extends JpaRepository<PString, String> { 
} 

然后,在LocationRepositoryCustom我的自定义函数变为:

@Override 
public List<PString> collectStreetNames(String streetName) 
{ 
    Query query = em.createNativeQuery("select distinct streetName from Location where streetName like ?"); 
    query.setParameter(1, streetName + "%"); 
    List<PString> returned = new ArrayList<PString>(); 

    @SuppressWarnings("unchecked") 
    List<String> list = query.getResultList(); 

    for (String string : list) 
    { 
     returned.add(new PString(string)); 
    } 

    return returned; 
} 

现在返回一个StreetNames列表。 (它们被格式化为字符串项目的hrefs,因此所有空格都被替换为%20,但是我可以处理它们。)有趣的是,PString表不需要存在于数据库模式中 - 返回的hrefs实际上并不涉及到数据库中的实际数据项目。

请注意,这不会回答如何使用@Query注释执行此操作的问题。我通过返回列表再次尝试,但仍然有相同的错误。

+0

这种方法对我来说效果不好,你找到了解决方案吗?如果是的话,你可以在这里回答:http://stackoverflow.com/questions/33538426/spring-data-rest-sdr-bug-persistent-entity-must-not-be-null – 2015-11-05 07:10:46

相关问题