Reputation: 1177
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) ;
}
}
Upvotes: 0
Views: 1909
Reputation: 692073
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:
return jpaTemplate.execute(new JpaCalback<List<?>>() {
@Override
public List<?> doInJpa(EntityManager em) {
// your code here.
}
});
Note that this class is deprecated.
Upvotes: 2