Nov 2nd, 2009, 02:30 PM
#1
Unable to persist data using Spring & Hibernate
Hi Everyone,
I am trying to insert a record into a MySQL 5.1 DB Server using Spring 3.0, Hibernate Annotations 3.4.0. I am trying to run this as a JUnit4 TestCase but I keep getting the following trace, and there is no record inserted into the database:
Hibernate: insert into customer (loginid, password, firstname, lastname, email, address, phno, key) values (?, ?, ?, ?, ?, ?, ?, ?)
org.hibernate.exception.SQLGrammarException: could not insert: [com.nlp.registration.entities.Customer]
at org.hibernate.exception.SQLStateConverter.convert( SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.conver t(JDBCExceptionHelper.java:43)
at org.hibernate.id.insert.AbstractReturningDelegate. performInsert(AbstractReturningDelegate.java:40)
at org.hibernate.persister.entity.AbstractEntityPersi ster.insert(AbstractEntityPersister.java:2158)
at org.hibernate.persister.entity.AbstractEntityPersi ster.insert(AbstractEntityPersister.java:2638)
....
....
My hibernate.cfg.xml reads as follows:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.current_session_context_class">thr ead</property>
<property name="hibernate.connection.driver_class">com.mysql .jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">pass123</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="hibernate.jdbc.batch_size">30</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.dialect">org.hibernate.dialect.MyS QLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- Mapping files -->
<mapping resource="hibernate/customer.hbm.xml"/>
</session-factory>
</hibernate-configuration>
The test-spring-config.xml reads as follows:
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotati on.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
<property name="mappingResources">
<list>
<value>hibernate/Customer.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory
</prop>
<prop key="hibernate.query.factory_class">
org.hibernate.hql.classic.ClassicQueryTranslatorFa ctory
</prop>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.jdbc.batch_size">30
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.generate_statistics">true</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>com.nlp.registration.entities.Customer</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverM anagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test?autoReconnect=true&characterEncoding=cp12 50" />
<property name="username" value="root" />
<property name="password" value="pass123" />
</bean>
<bean id="hibernateInterceptor"
class="org.springframework.orm.hibernate3.Hibernat eInterceptor"
autowire="byName" />
<bean id="customerDaoTarget"
class="com.nlp.hibernate.dao.HibernateCustomerDao"
autowire="byName">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<bean id="customerDAO" class="org.springframework.aop.framework.ProxyFact oryBean">
<property name="proxyInterfaces">
<value>com.nlp.dao.CustomerDao</value>
</property>
<property name="interceptorNames">
<list>
<value>hibernateInterceptor</value>
<value>customerDaoTarget</value>
</list>
</property>
<property name="proxyTargetClass" value="true"/>
</bean>
</beans>
Customer.hbm.xml reads as follows:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class
name="com.nlp.registration.entities.Customer"
table="customer">
<id name="id" type="int" column="id">
<generator class="native" />
</id>
<property
name="loginId"
type="java.lang.String"
column="loginid"
not-null="true"
unique="true"
length="20"
/>
<property
name="password"
type="java.lang.String"
column="password"
not-null="true"
length="20"
/>
<property
name="firstName"
type="java.lang.String"
column="firstname"
not-null="false"
length="20"
/>
<property
name="lastName"
type="java.lang.String"
column="lastname"
not-null="false"
length="20"
/>
<property
name="emailAddress"
type="java.lang.String"
column="email"
not-null="false"
length="30"
/>
<property
name="address"
type="java.lang.String"
column="address"
not-null="false"
length="100"
/>
<property
name="phoneNumber"
type="java.lang.String"
column="phno"
not-null="false"
length="11"
/>
<property
name="licenseKey"
type="java.lang.String"
column="key"
not-null="false"
length="30"
/>
</class>
</hibernate-mapping>
The SQL for the DB Table is as follows:
CREATE TABLE `customer` (
`id` int(20) NOT NULL default '0',
`loginid` varchar(20) NOT NULL default '',
`password` varchar(20) NOT NULL default '',
`firstname` varchar(20) default NULL,
`lastname` varchar(20) default NULL,
`email` varchar(40) default NULL,
`address` varchar(100) default NULL,
`phno` varchar(11) default NULL,
`key` varchar(30) default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `loginid` (`loginid`)
)
Any idea anyone?
Please find attached the complete source and configuration files.
Regards,
Joseph
Attached Files
Last edited by joseph george; Nov 2nd, 2009 at 11:43 PM .
Reason: Minor Corrections
Tags for this Thread
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules