Spring Repository自定义查询Spel评估始终为空

时间:2016-04-17 08:09:20

标签: spring jpa spring-data spring-data-jpa

每次我在我的存储库中调用findByFirstNameContainingOrLastNameContaining函数时都会收到错误:

org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Property or field 'firstName' cannot be found on null

我的查询错了吗?我完全不知道错误

以下是我的文件:

Person.java

package com.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Person {

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    long id;

    String firstName;

    String lastName;

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
}

PersonRepository.java

package com.repositories;

import com.model.Person;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;

public interface PersonRepository extends CrudRepository<Person, Long> {
    @Query("SELECT p FROM Person p WHERE p.firstName LIKE %:firstName% OR "
            + "p.firstName LIKE %:lastName% OR "
            + "p.lastName LIKE %:lastName% OR "
            + "p.lastName LIKE %:firstName%")
    public List<Person> findByFirstNameContainingOrLastNameContaining(
        @Param(value = "firstName") String firstName, 
        @Param(value = "lastName") String lastName
    );
}

1 个答案:

答案 0 :(得分:0)

将%xxx%替换为:{parameter},它将起作用。

latin1