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.
Here are what my entities look like:
@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;
}
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;
}
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);
}
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.
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.
Thanks.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With