【问题标题】:The expected Spring JPA query is not being returned未返回预期的 Spring JPA 查询
【发布时间】:2018-11-23 02:16:50
【问题描述】:

我正在学习如何使用 Spring JPA,并且我已经配置了我的数据库以成功地将元素添加到我的数据库中。我只是无法制定一个查询来检索我的结果(员工列表,按姓氏过滤):

//员工实体:

@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) 方法时,我总是收到一个空集合——即使传递了我知道数据库中存在的姓氏:

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(带有下划线)。我不确定这是否有什么不同。

我哪里做错了?

【问题讨论】:

    标签: java mysql spring jpa


    【解决方案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”是数据库列的实际名称,如有必要,请更改。

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

    【讨论】:

    • 我相应地修改了我的Employee 类,但在调用findByLastName(...) 方法时仍然没有得到任何返回值。关于为什么会发生这种情况的任何其他想法?
    • ....你在课堂上添加@Table了吗?确保你处理的是正确的表和方案~
    • 我刚刚添加了它 - 仍然没有运气
    • 原来是一个与 RestController 无关的问题,它将查询参数传递给 findByLastName(...) 方法。尽管如此,为了成功查询数据库,还需要您建议的更改。
    • “@Column”注解在我们的例子中没有任何作用,因为提供者会自动将骆驼案例实体字段映射到蛇案例数据库列。
    猜你喜欢
    • 2016-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多