I have following Entity
@Entity
public class Employee{
private Integer id;
private String name;
private String address;
private Date created;
@OneToMany(mappedBy = "employee")
private Set<Language> languages = new HashSet<AlbumUser>()
}
and Language entity is
@Entity
public class Language{
private String name;
@ManyToOne
@JoinColumn(name = "employee_id")
private Employee employee;
}
my language table looks like following
I want to select all employee whose name starts with A and who knows java and C and to do so I am trying following
DetachedCriteria criteria = DetachedCriteria.forClass(Employee.class,"employee");
criteria.add( Restrictions.ilike("name", "A%") );
criteria.add(Restrictions.in("languages",languageSet));
return getHibernateTemplate().findByCriteria(criteria)
where languageSet is
Set<Language> languageSet = new HashSet();
languageSet.add(new Language("Java"));
languageSet.add(new Language("C"));
I can see my attempt is completely wrong...I am new to hibernate can some one please help me with it..
Caused by: java.sql.SQLException: No value specified for parameter 2
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2176)
at com.mysql.jdbc.PreparedStatement.fillSendPacket(PreparedStatement.java:2100
This should be achievable using Hibernate ASSOCIATIONs with CRITERIA. While you look close to correct (while am not sure why you are using a DetachedCriteria, rather than a simple Criteria), you should refer this - http://docs.jboss.org/hibernate/core/3.3/reference/en/html/querycriteria.html#querycriteria-associations - to figure out more on how should build such queries.