Hibernate – Spring-Data JPA repository is sorted by losing null values ​​in the results

I am using spring-data and jpa repository to query. I have a question, I have an entity with ManyToOne field, if I sort by this field in the query, then in My list will not return any value with the field Null. This does not seem to be the correct behavior.

The following is what my entity looks 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 itemCollection;


}

I have my JPA repository as shown below:

@ Transactional
public interface ItemRepository extends JpaRepository {

@Query("FROM Item i where name = ?1");
Page findItemWithName(String name, Pageable pageable);
}

I have simplified a lot of code, so you can get an idea. All queries work well, when I set Sort in the Pageable object to the owner_user_id column When sorting, there will be a problem. If any entries in the item table are null for owner_user_id, they will not be returned in the list.

Can I add some kind of comment to solve this problem? Or something else I can do? I really want to continue using the repository, but if I can’t solve this problem, I don’t think I will do it. I am using hibernate and MYSQL, not sure if this is part of the problem.

Thank you.

This is a known issue in Spring Data / JPA Spec, which is in version 1.2.1 It has been resolved in https://jira.springsource.org/browse/DATAJPA-252 and https://jira.spring.io/browse/DATAJPA-277.

Me I am using spring-data and jpa repository to query. I have a problem, I have an entity with ManyToOne field, if I sort by this field in the query, then nothing with that field Null will be returned in my list This does not seem to be the correct behavior.

Here is what my entity looks 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;< br />
@JoinColumn(name = "owner_user_id", referencedColumnName = "id")
@ManyToOne(fetch = FetchType.LAZY)
private User ownerUserId;

}

Then ManyToOne user entity

@Entity
public class User {

@Size(max = 10 0)
@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 itemCollection;


}

I have my JPA repository as shown below:

@Transactional 
public interface ItemRepository extends JpaRepository {

@Query("FROM Item i where name = ?1");
Page findItemWithName(String name , Pageable pageable);
}

I have simplified a lot of code, so you can get an idea. All queries work well, when I set Sort in the Pageable object to perform the owner_user_id column When sorting, there will be a problem. If any item in the item table is null for owner_user_id, they will not be returned in the list.

Can I add some kind of comment to solve this problem? Or something else I can do? I really want to continue using the repository, but if I can’t solve this problem, I don’t think I will do it. I am using hibernate and MYSQL, not sure if this is part of the problem.

Thank you.

This is a known issue in Spring Data / JPA Spec, and it has been resolved in version 1.2.1. See https://jira. springsource.org/browse/DATAJPA-252 and https://jira.spring.io/browse/DATAJPA-277.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 4456 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.