Announcement Announcement Module
Collapse
No announcement yet.
container is searching for persistence.xml inspite of having the dataSource object Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • container is searching for persistence.xml inspite of having the dataSource object

    I am creating a JPAtransaction manager with dataSource created from a property file named database.properties. But while creating PlatformTransactionManager it throws exception as given below. Any ideas would be highly appreciated.


    @Bean
    public DataSource dataSource()
    {
    System.out.println("This method is called "+(count++)+" times ");
    SimpleDriverDataSource source = null;
    try
    {
    source = new SimpleDriverDataSource();
    Class driver = Class.forName(driverName);
    source.setDriverClass(driver);
    source.setUsername(this.userName);
    source.setPassword(this.password);
    source.setUrl(this.url);
    return source;

    }
    catch(Exception ee)
    {
    ee.printStackTrace();
    }
    return source;
    }

    @Bean
    public PlatformTransactionManager transactionManager()
    {
    System.out.println(" its starting well ");
    EntityManagerFactory factory = entityManagerFactory().getObject();
    System.out.println(" ending well =="+factory.getProperties());
    return new JpaTransactionManager(factory);
    }

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory()
    {
    LocalContainerEntityManagerFactoryBean containerFactory = new LocalContainerEntityManagerFactoryBean();
    containerFactory.setDataSource(this.dataSource());
    System.out.println(" container resulted sucessfully "+containerFactory.getDataSource());
    return containerFactory;
    }


    org.springframework.context.support.AbstractApplic ationContext prepareRefresh
    [java] INFO: Refreshing org.springframework.context.support.ClassPathXmlAp [email protected]: startup date [Thu May 05 18:21:18 IST 2011]; root of context hierarchy
    [java] 5 May, 2011 6:21:18 PM org.springframework.beans.factory.xml.XmlBeanDefin itionReader loadBeanDefinitions
    [java] INFO: Loading XML bean definitions from class path resource [spring.xml]
    [java] 5 May, 2011 6:21:18 PM org.springframework.context.annotation.ClassPathSc anningCandidateComponentProvider registerDefaultFilters
    [java] INFO: JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning
    [java] 5 May, 2011 6:21:18 PM org.springframework.context.annotation.ClassPathSc anningCandidateComponentProvider registerDefaultFilters
    [java] INFO: JSR-330 'javax.inject.Named' annotation found and supported for component scanning
    [java] 5 May, 2011 6:21:18 PM org.springframework.core.io.support.PropertiesLoad erSupport loadProperties
    [java] INFO: Loading properties file from class path resource [database.properties]
    [java] 5 May, 2011 6:21:18 PM org.springframework.beans.factory.annotation.Autow iredAnnotationBeanPostProcessor <init>
    [java] INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
    [java] 5 May, 2011 6:21:18 PM org.springframework.beans.factory.support.DefaultL istableBeanFactory preInstantiateSingletons
    [java] INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultL [email protected]: defining beans [smsService,smsServiceClient,springDataSource,sprin gScheduler,worker,org.springframework.context.anno tation.internalConfigurationAnnotationProcessor,or g.springframework.context.annotation.internalAutow iredAnnotationProcessor,org.springframework.contex t.annotation.internalRequiredAnnotationProcessor,o rg.springframework.context.annotation.internalComm onAnnotationProcessor,org.springframework.context. annotation.internalPersistenceAnnotationProcessor, org.springframework.beans.factory.config.PropertyP laceholderConfigurer#0,org.springframework.aop.con fig.internalAutoProxyCreator,org.springframework.t ransaction.annotation.AnnotationTransactionAttribu teSource#0,org.springframework.transaction.interce ptor.TransactionInterceptor#0,org.springframework. transaction.config.internalTransactionAdvisor,org. springframework.scheduling.annotation.internalAsyn cAnnotationProcessor,org.springframework.schedulin g.annotation.internalScheduledAnnotationProcessor, dataSource,transactionManager,entityManagerFactory]; root of factory hierarchy
    [java] This method is called 0 times
    [java] container resulted sucessfully org.springframework.jdbc.datasource.SimpleDriverDa [email protected]
    [java] 5 May, 2011 6:21:18 PM org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry destroySingletons
    [java] INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultL [email protected]: defining beans [smsService,smsServiceClient,springDataSource,sprin gScheduler,worker,org.springframework.context.anno tation.internalConfigurationAnnotationProcessor,or g.springframework.context.annotation.internalAutow iredAnnotationProcessor,org.springframework.contex t.annotation.internalRequiredAnnotationProcessor,o rg.springframework.context.annotation.internalComm onAnnotationProcessor,org.springframework.context. annotation.internalPersistenceAnnotationProcessor, org.springframework.beans.factory.config.PropertyP laceholderConfigurer#0,org.springframework.aop.con fig.internalAutoProxyCreator,org.springframework.t ransaction.annotation.AnnotationTransactionAttribu teSource#0,org.springframework.transaction.interce ptor.TransactionInterceptor#0,org.springframework. transaction.config.internalTransactionAdvisor,org. springframework.scheduling.annotation.internalAsyn cAnnotationProcessor,org.springframework.schedulin g.annotation.internalScheduledAnnotationProcessor, dataSource,transactionManager,entityManagerFactory]; root of factory hierarchy
    [java] Exception in thread "main" org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'smsService': Injection of persistence dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'entityManagerFactory' defined in class path resource [springservice/SpringDataSource.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}
    [java] at org.springframework.orm.jpa.support.PersistenceAnn otationBeanPostProcessor.postProcessPropertyValues (PersistenceAnnotationBeanPostProcessor.java:341)
    [java] at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.populateBean(AbstractAu towireCapableBeanFactory.java:1074)
    [java] at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:517)
    [java] at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
    [java] at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 91)
    [java] at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:222)
    [java] at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:288 )
    [java] at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:190)
    [java] at org.springframework.beans.factory.support.DefaultL istableBeanFactory.preInstantiateSingletons(Defaul tListableBeanFactory.java:580)
    [java] at org.springframework.context.support.AbstractApplic ationContext.finishBeanFactoryInitialization(Abstr actApplicationContext.java:895)
    [java] at org.springframework.context.support.AbstractApplic ationContext.refresh(AbstractApplicationContext.ja va:425)
    [java] at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:139)
    [java] at org.springframework.context.support.ClassPathXmlAp plicationContext.<init>(ClassPathXmlApplicationCon text.java:83)
    [java] at springservice.SpringScheduler.main(SpringScheduler .java:24)
    [java] Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'entityManagerFactory' defined in class path resource [springservice/SpringDataSource.class]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}
    [java] at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.initializeBean(Abstract AutowireCapableBeanFactory.java:1420)
    [java] at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:519)
    [java] at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
    [java] at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 91)
    [java] at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:222)
    [java] at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:288 )
    [java] at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:190)
    [java] at org.springframework.orm.jpa.support.PersistenceAnn otationBeanPostProcessor.findDefaultEntityManagerF actory(PersistenceAnnotationBeanPostProcessor.java :529)
    [java] at org.springframework.orm.jpa.support.PersistenceAnn otationBeanPostProcessor.findEntityManagerFactory( PersistenceAnnotationBeanPostProcessor.java:495)
    [java] at org.springframework.orm.jpa.support.PersistenceAnn otationBeanPostProcessor$PersistenceElement.resolv eEntityManager(PersistenceAnnotationBeanPostProces sor.java:656)
    [java] at

  • #2
    An EntityManagerFactory always needs a persistence.xml, it has nothing to with the fact that you are specifing the datasource localy or in the persistence.xml...

    Comment


    • #3
      I am able to inject EntityManager instance, but this blog does not talk about persistence.xml and even javadocs says spring might use given dataSource when persistence.xml is not there.

      http://blog.springsource.com/2011/01...-service-tier/

      in setDataSource() method of
      http://static.springsource.org/sprin...ctoryBean.html
      is explaining this.

      Comment


      • #4
        Reading and understanding are 2 different things. If you would have looked at the sample you point at you would have seen that even there is a persistence.xml

        You still need a persistence.xml file, you can override the location with the given factory bean but it still has to be there. If you don't override it has to be in the default location. The existence of a persistence.xml file is dictated by the jpa specification, spring simply delegates to the underlying technology (hibernate, openjpa, eclipselink etc.) to provide jpa capabilities, those technologies really need a persistence.xml file. No matter if you use spring or not to configure that technology.

        Also if you would look at the stacktrace you would see that you cannot inject your entitymanager(factory) as the creation of that bean fails... Your error has nothing at all to do with your datasource.

        Comment


        • #5
          Thanks for the reply. I have modified the code such that it takes persistence.xml. But now I have a new issue. When I call entityManager.persist(), the bean is not stored in the database. But entityManager.createNativeQuery() works fine. Attached are the logs.

          @Service
          public class SmsService {

          @PersistenceContext
          private EntityManager manager = null;

          public List findBySource(String source)
          {
          try
          {
          String queryString = "select * from SmsDetails where source = '"+source+"'";
          Query query = manager.createNativeQuery(queryString,SmsBean.clas s);
          List resultList = query.getResultList();
          System.out.println(resultList);
          return resultList;
          }
          catch(Exception ee)
          {
          ee.printStackTrace();
          }
          return null;
          }

          public void addSmsDetails(SmsBean bean)
          {
          try
          {
          //System.out.println(" i am not a b "+bean+ " manager.getClass().getName ()=="+manager.getClass().getName());
          //Session session = ((HibernateEntityManager)manager).getSession();
          //System.out.println(" properties "+session.getProperties());
          System.out.println(" calling persisting ===");
          manager.persist(bean);
          //session.save(bean);
          }
          catch(Exception ee)
          {
          System.out.println(" caught exception ");
          ee.printStackTrace();
          }
          }
          }



          @Service
          public class SmsService {

          @PersistenceContext
          private EntityManager manager = null;

          public List findBySource(String source)
          {
          try
          {
          String queryString = "select * from SmsDetails where source = '"+source+"'";
          Query query = manager.createNativeQuery(queryString,SmsBean.clas s);
          List resultList = query.getResultList();
          System.out.println(resultList);
          return resultList;
          }
          catch(Exception ee)
          {
          ee.printStackTrace();
          }
          return null;
          }

          public void addSmsDetails(SmsBean bean)
          {
          try
          {
          //System.out.println(" i am not a b "+bean+ " manager.getClass().getName ()=="+manager.getClass().getName());
          //Session session = ((HibernateEntityManager)manager).getSession();
          //System.out.println(" properties "+session.getProperties());
          System.out.println(" calling persisting ===");
          manager.persist(bean);
          //session.save(bean);
          }
          catch(Exception ee)
          {
          System.out.println(" caught exception ");
          ee.printStackTrace();
          }
          }
          }



          19:28:22,367 DEBUG DefaultListableBeanFactory:242 - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTr ansactionAdvisor'
          [java] 19:28:22,369 DEBUG DefaultListableBeanFactory:458 - Finished creating instance of bean 'smsService'
          [java] 19:28:22,370 DEBUG DefaultListableBeanFactory:214 - Creating shared instance of singleton bean 'smsServiceClient'
          [java] 19:28:22,370 DEBUG DefaultListableBeanFactory:430 - Creating instance of bean 'smsServiceClient'
          [java] 19:28:22,374 DEBUG DefaultListableBeanFactory:242 - Returning cached instance of singleton bean 'smsService'
          [java] 19:28:22,375 DEBUG DefaultListableBeanFactory:739 - Autowiring by type from bean name 'smsServiceClient' via constructor to bean named 'smsService'
          [java] Example code started
          [java] calling persisting ===
          [java] 19:28:22,385 DEBUG SharedEntityManagerCreator$SharedEntityManagerInvo cationHandler:231 - Creating new EntityManager for shared EntityManager invocation
          [java] 19:28:22,456 DEBUG EntityManagerFactoryUtils:328 - Closing JPA EntityManager
          [java] calling persisting ===
          [java] 19:28:22,459 DEBUG SharedEntityManagerCreator$SharedEntityManagerInvo cationHandler:231 - Creating new EntityManager for shared EntityManager invocation
          [java] 19:28:22,460 DEBUG EntityManagerFactoryUtils:328 - Closing JPA EntityManager
          [java] calling persisting ===
          [java] 19:28:22,460 DEBUG SharedEntityManagerCreator$SharedEntityManagerInvo cationHandler:231 - Creating new EntityManager for shared EntityManager invocation
          [java] 19:28:22,461 DEBUG EntityManagerFactoryUtils:328 - Closing JPA EntityManager
          [java] 19:28:22,461 DEBUG SharedEntityManagerCreator$SharedEntityManagerInvo cationHandler:231 - Creating new EntityManager for shared EntityManager invocation
          [java] 19:28:22,493 DEBUG SimpleDriverDataSource:138 - Creating new JDBC Driver Connection to [jdbc:mysql://localhost:3336/college]
          [java] Hibernate: select * from SmsDetails where source = 'saravana'
          [java] 19:28:22,519 DEBUG EntityManagerFactoryUtils:328 - Closing JPA EntityManager
          [java] []
          [java] []
          [java] 19:28:22,520 DEBUG DefaultListableBeanFactory:504 - Eagerly caching bean 'smsServiceClient' to allow for resolving potential circular references
          [java] 19:28:22,521 DEBUG DefaultListableBeanFactory:242 - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTr ansactionAdvisor'
          [java] 19:28:22,523 DEBUG DefaultListableBeanFactory:458 - Finished creating instance of bean 'smsServiceClient'
          [java] 19:28:22,523 DEBUG DefaultListableBeanFactory:242 - Returning cached instance of singleton bean 'springDataSource'

          Comment


          • #6
            Please use [ code][/code ] tags when posting code, that way it remains readable. For update/delete/insert to work you need transactions, trying to modify data without transactions is useless.

            Comment


            • #7
              Sorry, Never thought it could be the issue. Expected default code to work with out transactional attribute. Able to persist the data with the entity manager and thanks for the instructions.

              Comment

              Working...
              X