Spring-data JPA存储库通过在结果中丢失空值来排序 [英] Spring-data JPA repository Order by losing null values in results
问题描述
我正在使用spring-data和一个jpa存储库进行查询.我有一个问题,我有一个具有ManyToOne字段的实体,如果我按查询中的该字段排序,那么该字段中没有Null的任何值都不会返回到我的列表中.这似乎不是适当的行为.
I am using spring-data and a jpa repository for my queries. I am having a problem where, I have an entity with a ManyToOne field, if I order by this field in a query, then any values that have a Null for this field are not returned in my list. This doesn't seem like proper behaviour.
这是我的实体的样子:
@Entity
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@NotNull
@Column(name = "id")
private Integer id;
@Size(max = 255)
@Column(name = "name")
private String name;
@JoinColumn(name = "owner_user_id", referencedColumnName = "id")
@ManyToOne(fetch = FetchType.LAZY)
private User ownerUserId;
}
然后是ManyToOne用户实体
Then the ManyToOne user entity
@Entity
public class User {
@Size(max = 100)
@Column(name = "email")
private String email;
@Size(max = 256)
@Column(name = "first_name")
private String firstName;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@NotNull
@Column(name = "id")
private Integer id;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "ownerUserId", fetch = FetchType.LAZY)
private Collection<Item> itemCollection;
}
我的JPA存储库如下:
I have my JPA repository like so:
@Transactional
public interface ItemRepository extends JpaRepository<Item, Integer> {
@Query("FROM Item i where name = ?1");
Page<Item> findItemWithName(String name, Pageable pageable);
}
我简化了很多代码,只是为了让您有个主意.所有查询都工作得很好,当我将Pageable对象中的Sort设置为对owner_user_id列进行排序时,就会出现问题.如果项目表中的任何条目的owner_user_id为null,则它们不会在列表中返回.
I have simplified a lot of the code just so you can get an idea. All the queries are working great, the problem arises when I set the Sort in my Pageable object to sort on the owner_user_id column. If any of the entries in the item table have a null for owner_user_id they are not returned in the list.
我可以添加某种注释来解决此问题吗?还是我可以做的其他事情?我真的很想继续使用存储库,但是如果我不能解决这个问题,我认为我不会.我正在使用hibernate和MYSQL,不确定是否是问题的一部分.
Is there some sort of annotation I can add to get around this? Or something else I can do? I really want to keep using the repository but don't think I will if I can't get around this. I am using hibernate and MYSQL, not sure if that is part of the issue.
谢谢.
推荐答案
这是Spring Data/JPA Spec中的一个已知问题,在1.2.1版本中已解决.参见 https://jira.springsource.org/browse/DATAJPA-252 和 https://jira.spring.io/browse/DATAJPA-277 .
This was a known issue in Spring Data / JPA Spec which is solved in 1.2.1 Version. See https://jira.springsource.org/browse/DATAJPA-252 and https://jira.spring.io/browse/DATAJPA-277.
这篇关于Spring-data JPA存储库通过在结果中丢失空值来排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!