预期的Spring JPA查询未返回

时间:2018-11-23 02:16:51

标签: java mysql spring jpa

我正在学习如何使用Spring JPA,并且已经将数据库配置为成功向数据库中添加元素。我只是在编写查询以检索我的结果时遇到麻烦(按姓氏过滤的Employee列表):

//雇员实体:

@Entity
public class Employee {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    long id;
    String firstName;
    String lastName;

    public Employee() { }

    public Employee(long id, String firstName, String lastName) {
        super();
        this.id = id;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    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;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((firstName == null) ? 0 : firstName.hashCode());
        result = prime * result + (int) (id ^ (id >>> 32));
        result = prime * result + ((lastName == null) ? 0 : lastName.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Employee other = (Employee) obj;
        if (firstName == null) {
            if (other.firstName != null)
                return false;
        } else if (!firstName.equals(other.firstName))
            return false;
        if (id != other.id)
            return false;
        if (lastName == null) {
            if (other.lastName != null)
                return false;
        } else if (!lastName.equals(other.lastName))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
    }

}

我的问题在下面我的存储库界面中的查询中。调用findByLastName(String lastName)方法时,我总是收到一个空的Collection-即使传递我知道在数据库中存在的lastName

public interface EmployeeRepository
    extends CrudRepository<Employee, Long> {

    @Query("select u from Employee u where u.lastName = ?1")
    public Collection<Employee> findByLastName(String lastName);
}

还值得一提的是,我正在查询的列在我的数据库列(带有下划线)中被命名为last_name。我不确定这是否有作用。

我哪里出错了?

1 个答案:

答案 0 :(得分:-1)

您需要在字段上添加@Column,以使jpa知道如何在数据库和Java类之间创建映射。

例如:

@Entity
@Table(name="target_table_name")
public class Employee {

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

    @Column(name="first_name")
    private String firstName;

    @Column(name="last_name")
    private String lastName;
    //getter and setter and something else
}

如果没有@Table,实体名称将被jpa识别为表名称。如果名称不相同,则应在类上添加@Table并指定表名称。

注释中的“ first_name”和“ last_name”是数据库列的实际名称,如有必要,应进行更改。

顺便说一句,最好将实体字段的访问权限设置为私有。