How to use createNativeQuery using jpatemplate, because jpatemplate.getEntityManager() is returning null:
public class GenericDAO<T, PK extends Serializable> {
@Autowired
public void setEntityManagerFactory(EntityManagerFactory emf) {
this.jpaTemplate = new JpaTemplate(emf);
}
}
or can we use both entitymanager and jpaTemplate? like:
public class GenericDAO<T, PK extends Serializable> {
@Autowired
public void setEntityManagerFactory(EntityManagerFactory emf) {
this.jpaTemplate = new JpaTemplate(emf);
}
@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}
public void save(obj){
this.jpaTemplate.presist(Object obj)
}
public List<?> createNativeQuery(String query){
entityManager.createNativeQuery(query) ;
}
}
To access the entity manager, you’re supposed to use the execute method of JpaTemplate, and put your code in the callback. JpaTemplate gives you the EM. You don’t get it from the JpaTemplate:
Note that this class is deprecated.