I need to use the following query but i am not able to use spring injection so i need to make the query in simple sql.
How can i make the command that don’t uses sessionfactory and any other beans
where registration is my entity class with username and password
public Registration findUserByEmail(String email) {
try {
Session session = sessionFactory.getCurrentSession();
String query = "FROM Registration WHERE email =:email";
Query query2 = session.createSQLQuery(query);
query2.setString("email",email);
return (Registration) query2.uniqueResult();
} catch (Exception e) {
System.out.println(e);
throw new RuntimeException("DAO failed", e);
}
}
First of all there is no need to make sql query since you have an entity. Hibernate is not dependent on Spring to get the SessionFactory so you don’t have to think about “beans”
Depending on your configuration (I assume its annotation based), you can create a session factory like –
and if it is not annotation based then you can use
this link may help.
As a side note – if you’re still willing to fire sql (will not recommend though) then do it in plain JDBC.