I have an issue with a search query.
My model:
Project:
@Entity
@Table(name = "project")
public class Project {
@Id
@Column(name = "id")
@GeneratedValue
private Integer id;
@ManyToMany(fetch=FetchType.EAGER)
@JoinTable(
name="project_tag",
joinColumns = @JoinColumn(name="project_id"),
inverseJoinColumns = @JoinColumn(name="tag_id")
)
private Set<Tag> requiredSkills;
@Column(name="name")
private String name;
@Column(name = "short_description", columnDefinition="TEXT")
private String shortdescription;
@Column(name = "extended_description", columnDefinition="TEXT")
private String extendedDescription;
}
Tag:
@Entity
@Table(name="tag", uniqueConstraints = {@UniqueConstraint(columnNames={"name"})})
public class Tag {
@Id
@Column(name = "id")
@GeneratedValue
private Integer id;
@Column(name = "name")
private String name;
public int hashCode() {
return getName().hashCode();
}
}
I want to search projects with some search string, and I want to find projects with this string in the name, short description, extended description, AND tags. Searching for tags is the problem.
Here is my query:
public List<Project> search(String search) {
search = "%" + search + "%";
Query query = sessionFactory.getCurrentSession().createQuery(
"from Project p" +
" left join fetch p.requiredSkills r" +
" where p.name like :search" +
" or p.shortdescription like :search" +
" or p.extendedDescription like :search" +
" or r.name like :search"
);
query.setParameter("search", search);
query.setMaxResults(30);
return (List<Project>) query.list();
}
The line "or r.name like :search" is giving me an error (everything works fine without it):
java.lang.NullPointerException
myProject.model.Tag.hashCode(Tag.java:53)
I get this error only when some projects are found (no error when no results returned). Any ideas? Thanks!
Edit: forgot to tell that projects may have 0 to n tags...
So, I just deleted the "fetch" keyword from JB Nizet's proposed query, and it worked...
Final query:
Might be related to https://community.jboss.org/wiki/HibernateFAQ-AdvancedProblems#I_have_a_nonlazy_set_of_entities_that_override_equals_and_Hibernate_throws_a_NullPointerException saying that this problem will not be corrected...
Edit: tried again with fetch, it worked... Don't understand why!