Announcement Announcement Module
Collapse
No announcement yet.
javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException

    i have a MessageEntity class
    Code:
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private long id;
    
      private String text;
    
      public MessageEntity() {
      }
    
      public MessageEntity(final String text) {
        this.text = text;
      }
    
      public long getId() {
        return id;
      }
    
      public void setId(final long id) {
        this.id = id;
      }
    
      public String getText() {
        return text;
      }
    
      public void setText(final String text) {
        this.text = text;
      }
    
      @Override
      public String toString() {
    	  if (logger.isDebugEnabled()) {
    			logger.debug("Debug Enabled");
    		}
    		logger.debug("id is " + id + "text " + text);
        return "MessageEntity [id=" + id + ", text=" + text + "]";
      }
    and a MessageContoller class
    Code:
    public class MessageJpaController {
    	
    	static Logger logger = LoggerFactory.getLogger(MessageJpaController.class);
    
      @PersistenceContext(unitName = "messagePersistenceUnit")
      private EntityManager em;
    
      public long count() {
        Query q = em.createNamedQuery("MessageEntity.count");
        return (Long) q.getSingleResult();
      }
    
      public void create(MessageEntity messageEntity) {
        em.persist(messageEntity);
      }
    
      public MessageEntity read(final long id) {
    
        return em.find(MessageEntity.class, id);
      }}
    and its JUnit test case

    Code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "classpath:context1.xml" })
    @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = false)
    @Transactional
    public class MessageJpaControllerTest {
    	
    	@Autowired
    	private MessageJpaController messageJpaController;
    
    
    	@Test
    	public void testCreate() {
    		for (int i = 11; i <= 20; i++) {
    			messageJpaController.create(new MessageEntity(String.format(
    					"Message %s", i)));
    
    		}
    
    		assertEquals(10, messageJpaController.count());
    	}
    
    }
    i do see all the code working properly .. have added all the dependencies in pom.xml

    Code:
    <properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    	</properties>
    	<repositories>
    		<repository>
    			<id>JBoss Repo</id>
    			<url>http://repository.jboss.com/maven2</url>
    		</repository>
    	</repositories>
    	<dependencies>
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>3.8.1</version>
    			<scope>test</scope>
    		</dependency>
    		
    
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-context</artifactId>
    			<version>3.1.0.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-core</artifactId>
    			<version>3.1.0.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-beans</artifactId>
    			<version>3.1.0.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-test</artifactId>
    			<version>3.1.0.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-tx</artifactId>
    			<version>3.1.0.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-aop</artifactId>
    			<version>3.1.0.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-context-support</artifactId>
    			<version>3.1.0.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-orm</artifactId>
    			<version>3.1.0.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework</groupId>
    			<artifactId>spring-web</artifactId>
    			<version>3.1.0.RELEASE</version>
    		</dependency>
    		<dependency>
    			<groupId>mysql</groupId>
    			<artifactId>mysql-connector-java</artifactId>
    			<version>5.1.6</version>
    		</dependency>
    		<dependency>
    			<groupId>org.slf4j</groupId>
    			<artifactId>slf4j-log4j12</artifactId>
    			<version>1.6.4</version>
    		</dependency>
    		<dependency>
    			<groupId>junit</groupId>
    			<artifactId>junit</artifactId>
    			<version>4.10</version>
    		</dependency>
    		<dependency>
    			<groupId>org.hibernate.javax.persistence</groupId>
    			<artifactId>hibernate-jpa-2.0-api</artifactId>
    			<version>1.0.1.Final</version>
    		</dependency>
    		<dependency>
    			<groupId>org.hibernate</groupId>
    			<artifactId>hibernate-entitymanager</artifactId>
    			<version>4.0.1.Final</version>
    		</dependency>
    	</dependencies>
    i am getting " javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException" and com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorEx ception: Table 'MyData.MessageEntity' doesn't exist

    tq

  • #2
    Can you double check that you have annoted MessageEntity and in your datasource configuration you are scanning the packages where entity resides?

    Comment


    • #3
      @abhijith_p i am afraid, i dint get u, wht exactly ur asking me t recheck, can u pls elaborate bit more

      Comment


      • #4
        Looking at the exception, it feels like your entity classes are not recognized by your datasource configuration. You usually annotate your entity class(domain classes) and scan those packages when you are declaring your sessionfactory. I would verify that these entity classes are visible for the sessionfactory.

        For ex in our project we have done this:
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotati on.AnnotationSessionFactoryBean" packagesToScan="com.xxx.domain" p:useTransactionAwareDataSource="true" p:configurationClass="org.hibernate.cfg.Annotation Configuration" p:dataSource-ref="dataSource">

        Hope I am clear this time.

        Comment


        • #5
          @abhijith_p yep i gotcha But u see my configuratin file, i dont see anything wrong, if can find juzz tell me
          here is my contex.xml

          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="com.mysql.jdbc.Driver" />
          		<property name="url" value="jdbc:mysql://localhost:3306/MyData" />
          		<property name="username" value="root" />
          		<property name="password" value="vekomy" />
          	</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="messageJpaController"
          		class="com.vekomy.etl.mediation.controller.MessageJpaController">
          	</bean>
          
          	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
          		<property name="entityManagerFactory" ref="entityManagerFactory" />
          		<property name="dataSource" ref="dataSource" />
          	</bean>
          
          </beans>

          Comment


          • #6
            Hey @abhijith_p tqq buddy, i gotacha i did small silly mistake in place of dialect, now my junit test is compiling and working but unfortunately its not creating the table in mysql

            Comment


            • #7
              Try to remove the @Transactional annotation from test class.

              Comment


              • #8
                @matthias.hubber - It dint work bro -
                but if i remove drop
                in this
                Code:
                <property name="jpaProperties">
                			<props>
                				<prop key="hibernate.hbm2ddl.auto">create-drop</prop>
                			</props>
                		</property>
                if i keep only create it creating -

                Comment

                Working...
                X