Announcement Announcement Module
Collapse
No announcement yet.
java.lang.IllegalStateException: Failed to load ApplicationContext Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • java.lang.IllegalStateException: Failed to load ApplicationContext

    Hello, i have a maven spring project in which i am using derby database, i have message entity class, with JpaMessageDao extending JpaDaoSupport

    public final class JpaMessageDao extends JpaDaoSupport {

    public long count() {
    return (Long) getJpaTemplate().execute(new JpaCallback() {

    public Object doInJpa(final EntityManager em) throws PersistenceException {
    Query q = em.createNamedQuery("MessageEntity.count");
    return q.getSingleResult();
    }
    });
    }

    public void create(final MessageEntity message) {
    getJpaTemplate().persist(message);
    }
    }

    and a persistenc.xml file .. Now in my context.xml i have added the beans of - org.springframework.jdbc.datasource.DriverManagerD ataSource - org.springframework.orm.jpa.LocalContainerEntityMa nagerFactoryBean -
    org.springframework.orm.jpa.vendor.HibernateJpaVen dorAdapter -
    org.springframework.orm.jpa.JpaTransactionManager -
    and my MssageEntity class - now here comes the issue is when i run junit test - my junit is compiling but i dont see any table with MessageEnitity name - but i do find a database in my project folder
    -<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
    <property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver" />
    <property name="url" value="jdbc:derby:MyData;create=true" />
    <property name="username" value="heyday" />
    <property name="password" value="heyday" />
    </bean>
    with the above code in context.xml - by embedded driver

    when i change it to ClientDriver and add url <property name="url" value="jdbc:derby://localhost:1527/MyDb;create=true"></property>

    i am getting "java.lang.IllegalStateException: Failed to load ApplicationContext"

    so can anyone please help me how to get this issue resolved, i want MessageEntity the table with values

    Any help is appreciated

    Thanks

  • #2
    Please use [ code][/code ] tags when posting code, that way it is actually readable ...

    Post the full stack trace (remember [ code][/code ] tags!!).

    Also don't use JpaDaoSupport or JpaTemplate simply inject the EntityManager and use that directly (both support classes should be considered deprecated).

    Comment


    • #3
      My sincere apologies for post such a crap code

      My JpaDao class is
      Code:
      public final class JpaMessageDao extends JpaDaoSupport {
      
      public long count() {
      return (Long) getJpaTemplate().execute(new JpaCallback() {
      
      public Object doInJpa(final EntityManager em) throws PersistenceException {
      Query q = em.createNamedQuery("MessageEntity.count");
      return q.getSingleResult();
      }
      });
      }
      
      public void create(final MessageEntity message) {
      getJpaTemplate().persist(message);
      }
      }
      My junit test case for aJpaDaoclass

      Code:
      @RunWith(SpringJUnit4ClassRunner.class)
      @ContextConfiguration(locations = { "classpath:context1.xml" })
      @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
      @Transactional
      public class JpaMessageDaoTest {
      
      	@Autowired
      	private JpaMessageDao jpaMessageDao;
      
      	@Test
      	public void daoCreate() throws Exception {
      
      		for (int i = 1; i <= 10; i++) {
      			jpaMessageDao.create(new MessageEntity(String.format("Message %s",
      					i)));
      		}
      
      		assertEquals(10, jpaMessageDao.count());
      	}
      
      	@Test
      	public void daoRead() throws Exception {
      		List<MessageEntity> messages = jpaMessageDao.read(1, 2);
      		assertNotNull(messages);
      		assertEquals(2, messages.size());
      	}
      }
      My context1.xml is

      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <beans xmlns="http://www.springframework.org/schema/beans"
      	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
      	xsi:schemaLocation="
          http://www.springframework.org/schema/beans
          http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
          http://www.springframework.org/schema/tx
          http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
      
      	<bean id="dataSource"
      		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      		<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />
      		<property name="url" value="jdbc:derby:MyData; create=true" />
      		<property name="url" value="jdbc:derby://localhost:1527/MyDb;create=true"></property>
      		<property name="username" value="heyday" />
      		<property name="password" value="heyday" />
      	</bean>
      
      	<bean id="entityManagerFactory"
      		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
      		<property name="dataSource" ref="dataSource"></property>
      		<property name="jpaVendorAdapter">
      			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
      				<property name="databasePlatform" value="org.hibernate.dialect.DerbyDialect" />
      			</bean>
      		</property>
      		<property name="jpaProperties">
      			<props>
      				<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
      			</props>
      		</property>
      	</bean>
      
      	<bean name="jpaMessageDao"
      		class="org.adrianwalker.maven.skeleton.spring.jpa.JpaMessageDao">
      		<property name="entityManagerFactory" ref="entityManagerFactory" />
      	</bean>
      <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
      		<property name="entityManagerFactory" ref="entityManagerFactory" />
      		<property name="dataSource" ref="dataSource" />
      	</bean>
      
      </beans>
      am am getting an error of

      "Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.Dependenc yInjectionTestExecutionListener@62ac70ab] to prepare test instance [org.adrianwalker.maven.skeleton.spring.jpa.JpaMess ageDaoTest@25ed7c33]
      java.lang.IllegalStateException: Failed to load ApplicationContext"

      when i use embedded driver its working properly

      thanks

      Comment


      • #4
        As stated please post the full stacktrace. If you don't use the embedded driver make sure that your database is up and running if it isn't it will fail to connect and thus fail to start.

        Comment


        • #5
          Hey i am sorry i fund the mistake, i did nt add the

          derby client dependency in the pom.xml
          Code:
          <dependency>
          			<groupId>org.apache.derby</groupId>
          			<artifactId>derbyclient</artifactId>
          			<version>10.8.2.2</version>
          		</dependency>
          once when i add its working - Tqqq

          Comment

          Working...
          X