0

I am working on one enhancement in my application. This application is Spring Hibernate implementation.But the enhancement now I am working is Spring-JPA. Without disturbing the existing implementation I need to include JPA Below is my DBconfiguration class.

@Configuration
@EnableJpaRepositories(basePackages = {"packagename","package name" })
@EnableTransactionManagement
public class DataBaseConfiguration {

    @Value("${postgres.driver}")
    private String postgresDriver;
    @Value("${postgres.url}")
    private String postgresUrl;
    @Value("${postgres.username}")
    private String postgresUsername;
    @Value("${postgres.password}")
    private String postgresPassword;

    @Value("${hibernate.dialect}")
    private String hibernateDialect;
    @Value("${hibernate.dialect.property}")
    private String dialectProperty;
    @Value("${hibernate.show-sql}")
    private String hibernateShowSql;
    @Value("${boolean.true}")
    private String booleanTrue;

    **\\ existing hibernate flow**
    @Autowired
    @Bean(name = "name")
    public SessionFactory getSessionFactory(DataSource dataSource) {

        LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(
                dataSource);

        sessionBuilder.addAnnotatedClasses(entity.class);
        sessionBuilder.setProperty(hibernateDialect, dialectProperty);
        sessionBuilder.setProperty(postgresDriver, postgresUrl);
        sessionBuilder.setProperty(hibernateShowSql, booleanTrue);

        return sessionBuilder.buildSessionFactory();
    }

        **\\ existing hibernate flow**
    @Bean(name = "name")
    public DataSource getDataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(postgresDriver);
        dataSource.setUrl(postgresUrl);
        dataSource.setUsername(postgresUsername);
        dataSource.setPassword(postgresPassword);
        return dataSource;
    }

    **\\ existing hibernate flow**
    @Autowired
    @Bean(name = "manager")
    public HibernateTransactionManager getTransactionManager(
            SessionFactory sessionFactory) {
        HibernateTransactionManager transactionManager = null;
        transactionManager = new HibernateTransactionManager(sessionFactory);
        return transactionManager;
    }
    **\\included newly for JPA**
    @Bean
    public org.springframework.orm.jpa.JpaTransactionManager transactionManager() {
        JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
        jpaTransactionManager.setDataSource(getDataSource());
        jpaTransactionManager
                .setJpaDialect(new org.springframework.orm.jpa.vendor.HibernateJpaDialect());
        return jpaTransactionManager;
    }
        **\\included newly for JPA**
    @Bean
    public EntityManager entityManger() {
        return entityManagerFactory().getObject().createEntityManager();
    }
        \\included newly for JPA
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactory.setDataSource(getDataSource());

        entityManagerFactory.setPackagesToScan(
                "some package",
                "some package");
        Map<String, Object> jpaProperty = entityManagerFactory
                .getJpaPropertyMap();
        jpaProperty.put("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");

        HibernateJpaVendorAdapter va = new HibernateJpaVendorAdapter();
        va.setShowSql(Boolean.valueOf("true"));
        entityManagerFactory.setJpaVendorAdapter(va);
        return entityManagerFactory;
    }
}

When I deployed this application i am getting below exception

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class ------
Caused by: java.lang.NullPointerException
    at org.hibernate.hql.internal.antlr.HqlBaseParser.identPrimary(HqlBaseParser.java:4285)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.primaryExpression(HqlBaseParser.java:980)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.atom(HqlBaseParser.java:3609)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.unaryExpression(HqlBaseParser.java:3387)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.multiplyExpression(HqlBaseParser.java:3259)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.additiveExpression(HqlBaseParser.java:2964)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.concatenation(HqlBaseParser.java:597)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.relationalExpression(HqlBaseParser.java:2730)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.equalityExpression(HqlBaseParser.java:2591)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.negatedExpression(HqlBaseParser.java:2555)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.logicalAndExpression(HqlBaseParser.java:2471)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.logicalOrExpression(HqlBaseParser.java:2436)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.expression(HqlBaseParser.java:2146)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.exprList(HqlBaseParser.java:3994)
    at org.hibernate.hql.internal.antlr.HqlBaseParser.identPrimary(HqlBaseParser.java:4278)
5
  • How are you creating your DataBaseConfiguration bean? Commented Feb 10, 2015 at 17:23
  • In my servlet configuration in Mvc config class I am returing protected Class[] getServletConfigClasses() { retung databaseconfiguration.class} Commented Feb 10, 2015 at 17:27
  • I guess you have syntax error in some Query which might be OK as HQL but not as JPAQL, see e.g. what-when-how.com/hibernate/… By the way somewhere in your stacktrace might be error message stating which query is not OK. You might also try removing all queries (and/or one by one) to see if that's really the problem. Commented Feb 10, 2015 at 17:28
  • @Michal Thank you for pointed.It shows error in my named query.I annotated the named query in my entity class but help me how to check the syntax error in that query bcas I ran the query in SQL editior and pasted in above the entity class Commented Feb 10, 2015 at 17:33
  • I do not see the entity with the query. Commented Feb 10, 2015 at 17:38

1 Answer 1

1

I would suggest another approach, which could be simpler. Make JPA leading and use the LocalContainerEntityManagerFactoryBean to configure everything (including what you use now for hibernate). Next use the HibernateJpaSessionFactoryBean to expose the underlying SessionFactory.

This would save you duplicate configuration and would allow you to use a single JpaTransactionManager for all your transactions, however you still would be able to use a SessionFactory or HibernateTemplate if you would.

@Configuration
@EnableJpaRepositories(basePackages = {"packagename","package name" })
@EnableTransactionManagement
public class DataBaseConfiguration {

    @Autowired
    private Environment env;

    @Autowired
    @Bean(name = "name")
    public FactoryBean<SessionFactory> getSessionFactory(EntityManagerFactory emf) {
        HibernateJpaSessionFactoryBean factory = new HibernateJpaSessionFactoryBean();
        factory.setEntityManagerFactory(emf);
        return factory;
    }

        **\\ existing hibernate flow**
    @Bean(name = "name")
    public DataSource getDataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(env.getRequiredProperty("postgres.driver"));
        dataSource.setUrl(env.getRequiredProperty("postgres.url);
        dataSource.setUsername(env.getRequiredProperty("postgres.username");
        dataSource.setPassword(env.getRequiredProperty("postgres.password");
        return dataSource;
    }

    @Bean
    public JpaTransactionManager transactionManager() {
        JpaTransactionManager jpaTransactionManager = new JpaTransactionManager();
        jpaTransactionManager.setDataSource(getDataSource());
        jpaTransactionManager.setEntityManagerFactory(entityManagerFactory());
        return jpaTransactionManager;
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactory.setDataSource(getDataSource());
        Map<String, String> properties = new HashMap<String, String>();
        properties.put("hibernate.current_session_context_class", SpringSessionContext.class.getName()); 

        entitytManagerFactory.setJpaPropertyMap(properties);
        entityManagerFactory.setPackagesToScan(
                "some package",
                "some package");

        HibernateJpaVendorAdapter va = new HibernateJpaVendorAdapter();
        va.setShowSql(env.getProperty("hibernate.show-sql", Boolean.class, true);
        va.setDatabasePlatform(env.getProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect");
        entityManagerFactory.setJpaVendorAdapter(va);
        return entityManagerFactory;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.