0

I have created a POC with Spring Boot and JPA using spring-boot-starter-data-jpa and my solution works pretty well. My issue arises when i try to add the same solution to an old project which uses Spring 4 and hibernate JPA 2.1 version. When I deployed my war in weblogic, the first error i got was could not autowire the repository so i added the the @EnableJpaRepositories. I even tried to add spring.datasource.jndi-name=EXAMPLE_Data_Source where EXAMPLE_Data_Source is my weblogic datasource but no luck.

The application class:

@Configuration
@EnableWs
@ComponentScan("com.example.package")
@EnableJpaRepositories(basePackages = {"com.example.repository","com.example.entity"})
public class AppConfig extends WsConfigurerAdapter { //WsConfigurerAdapter  is spring-ws-core 2.2.0 Release

@Autowire
MyRepository  repository; 

The Repository class:

public interface MyRepository extends JpaRepository<EntityClass, EntityPKey> {

    @Query("select c from EntityClass c where ID = :param1 and  TYPE = :param2 and :param3 between FROM_DATE and TO_DATE ")
    EntityClass entityClass(@Param("param1") int param1, @Param("param2")
            char param2, @Param("param3") Date param3);
}

Now i get the get the error below:

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean  named 'entityManagerFactory' is defined" weblogic.application.ModuleException: 
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean   named 'entityManagerFactory' is defined
    at weblogic.application.internal.ExtensibleModuleWrapper.start(ExtensibleModuleWrapper.java:140)
    at weblogic.application.internal.flow.ModuleListenerInvoker.start(ModuleListenerInvoker.java:124)
    at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:233)
    at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:228)
    at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:45)
    Truncated. see log file for complete stacktrace
Caused By: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'entityManagerFactory' is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBeanDefinition(DefaultListableBeanFactory.java:641)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getMergedLocalBeanDefinition(AbstractBeanFactory.java:1157)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:280)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
    Truncated. see log file for complete stacktrace

I have to get it working on the old spring project which is deployed in weblogic.

Questions 1: Will this way of solving my issue work for the Spring framework or only with Springboot?

Question 2: What configurations I am missing?

Question 3: Please point me to a working example. I am struggling to make this work.

Spring webmvc and spring-tx is version 4.0.6

Please be a bit more detailed in your explanation. I am still very junior.

Thanks in advance.

Shafin Mahmud
  • 3,831
  • 1
  • 23
  • 35
Carol
  • 61
  • 1
  • 3
  • 11
  • Have you configured your entityManagerFactory properly. this might help https://stackoverflow.com/questions/24520602/spring-data-jpa-no-bean-named-entitymanagerfactory-is-defined-injection-of-a – Harsha Jayamanna Sep 16 '18 at 17:14
  • @Harshakj89 How do i configure it? In the example of the link you gave me they make changes to ApplicationContext.xml. I do not have that file in my project. Also do i have to make changes when i am deploying to weblogic? The datasource is configured in weblogic – Carol Sep 16 '18 at 17:26
  • this link explains how to configure hibernate properly with java configuration. check if this is helpful https://www.baeldung.com/hibernate-4-spring – Harsha Jayamanna Sep 16 '18 at 17:35
  • There are multiple possible ways of handling it. Is Weblogic supposed to be injecting the JPA persistence context, just the JDBC DataSource, just configuration information, or none of these? – chrylis -cautiouslyoptimistic- Sep 16 '18 at 17:35
  • @chrylis weblogic has the datasource . What other configurations are missing and how and were should i make them. It seems spring boot does a lot of this for us but when i use spring i have to make these in the application. Please show me examples of the different scenerios – Carol Sep 16 '18 at 17:45
  • @Harshakj89 in the example in https://www.baeldung.com/hibernate-4-spring, They configure the datasource with Bean annotation in the PersistenceConfig class. I am using weblogic for the datasource. How do configure mine if i am using weblogic. They also do configurations in properties file but aren't those the same as the ones in the datasource? which is in weblogic – Carol Sep 16 '18 at 17:53
  • In that case you will have to configure hibernate to connect to your data-source. https://www.journaldev.com/2905/hibernate-tomcat-jndi-datasource-example-tutorial. this should help – Harsha Jayamanna Sep 16 '18 at 18:04
  • If you can't use Boot, then you'll basically just need to replicate what's in `HibernateAutoConfiguration`. You should be able to copy it over cleanly. – chrylis -cautiouslyoptimistic- Sep 16 '18 at 18:41

2 Answers2

0

You have to configure Below Beans

DriverManagerDataSource
LocalContainerEntityManagerFactoryBean
JpaTransactionManager

Please refer this example

Tejas Garde
  • 337
  • 2
  • 13
  • i see the example you referred me is an xml version of 3/4 of what i did. I would tick for that if i had voting rights. Thanks – Carol Sep 19 '18 at 06:20
0

I was using annotation based configuration so i solved it after 2 long days using the following configuration:

@Bean
public DataSource jndiDataSource() {
    final JndiDataSourceLookup dsLookup = new JndiDataSourceLookup();
    dsLookup.setResourceRef(true);

    return dsLookup.getDataSource(dataSourceName); // where dataSourceName is your weblogic data source
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws NamingException {
    LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
    factoryBean.setDataSource(jndiDataSource());
    factoryBean.setJpaVendorAdapter(jpaVendorAdapter());
    factoryBean.setPackagesToScan("com.example.entity");
    return factoryBean;
}

@Bean
public JpaVendorAdapter jpaVendorAdapter() {
    HibernateJpaVendorAdapter hibernateJpaVendorAdapter = new HibernateJpaVendorAdapter();
    hibernateJpaVendorAdapter.setShowSql(false);
    hibernateJpaVendorAdapter.setGenerateDdl(true);
    hibernateJpaVendorAdapter.setDatabase(Database.ORACLE);
    return hibernateJpaVendorAdapter;
}

@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
    JpaTransactionManager txManager = new JpaTransactionManager();
    txManager.setEntityManagerFactory(entityManagerFactory);
    return txManager;
}
Carol
  • 61
  • 1
  • 3
  • 11