Hayi
Hayi

Reputation: 6236

Getting the entity manager

i'm using spring data jpa but i want execute some costume query so how can i get the entity manager in my java classes to make entityManager.createQuery(..)

   <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      <property name="persistenceUnitName" value="persistenceYous" />
      <property name="dataSource" ref="dataSource" />
      <property name="packagesToScan" value="persistence" />
      <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />

   </bean>

Upvotes: 0

Views: 801

Answers (3)

Martin Baumgartner
Martin Baumgartner

Reputation: 3652

You can obtain your EntityManager as in any other spring application:

public class ProductDaoImpl implements ProductDao {
    private EntityManager em;

    @PersistenceContext
    public void setEntityManager(EntityManager em) {
        this.em = em;
    }

    public Collection loadProductsByCategory(String category) {
        em. .... 
        ....
    }
}

See: http://docs.spring.io/spring/docs/4.0.0.RELEASE/spring-framework-reference/html/orm.html#orm-jpa-straight

Upvotes: 2

gpeche
gpeche

Reputation: 22504

In your applicationContext.xml, check that you have the following:

<context:annotation-config />

That will add support for several annotations, such as @PersistenceContext, that injects an EntityManager. So in your Spring-managed beans, you can do:

public class MyClass {
    private EntityManager entityManager;

    @PersistenceContext
    public void setEntityManager(EntityManager em) {
        this.entityManager = em
    }

    public void myMethod() {
        Query q = entityManager.createQuery(...);
        // ...
    }
}

If you want to add suport for just @PersistenceContext, and not the other annotations that <context:annotation-config /> supports, you would delete that from the applicationContext.xml and add the specific BeanPostProcessor:

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" scope="singleton">
   <!-- Optional configuration of PersistenceAnnotationBeanPostProcessor, for advanced use cases -->
</bean>

Upvotes: 2

Mr.Chowdary
Mr.Chowdary

Reputation: 3407

Firstly follow this link to get the steps required to implement..
check this link

In second step he uses @Autowired private JdbcOperations operations;
But that doesn't work for me, so i used the following code.. Which worked for me.

@Component
public class MayorRepositoryImpl extends JdbcDaoSupport implements MayorRepositoryCustom {

    @Override
    public List<Employee> getUsers(String role) {
        return getJdbcTemplate().query("Write your custom query",new RowMapper<Employee>(){
            @Override 
            public Employee mapRow(ResultSet rs, int rownumber) throws SQLException {   
                // Mapping each row and adding to list and returning the list 
                Employee employeeBean=new Employee();  
                employeeBean.setId(rs.getInt("id"));  
                employeeBean.setEmployeeNumber(rs.getString("employeeNumber"));
                employeeBean.setName(rs.getString("name"));
                employeeBean.setWorkEmailAddress(rs.getString("workEmailAddress"));
                employeeBean.setPersonalEmailAddress(rs.getString("personalEmailAddress"));
                return employeeBean;  
            }  
        });
    }

}
This is perfectly working for me..
If you find any difficulty let me know to help you.

Cheers

Upvotes: -1

Related Questions