Hi,
I am using struts,spring and hibernate.
I have a users table where the administrator can add the new users in it through the application.
If the user is already present in the db then i want to display the user a message that "user already exist" else save the user in the db.
However whenever i am trying to add the duplicate entry i get the following exception:
Hibernate: select nextval ('users_id_seq')
Hibernate: insert into users (username, password, id) values (?, ?, ?)
WARN org.hibernate.util.JDBCExceptionReporter::logExcep tions() - SQL Error: 0, SQLState: null
ERROR org.hibernate.util.JDBCExceptionReporter::logExcep tions() - Batch entry 0 insert into users (username, password, id) values ('steve', 'sdf', 3) was aborted.
ERROR org.hibernate.event.def.AbstractFlushingEventListe ner:
erformExecutions() - Could not synchronize database state with session
org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.handledN onSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert( SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.conver t(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(Ab stractBatcher.java:249)
at org.hibernate.engine.ActionQueue.executeActions(Ac tionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(Ac tionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListe ner.performExecutions(AbstractFlushingEventListene r.java:298)
at org.hibernate.event.def.DefaultFlushEventListener. onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.j ava:1000)
at org.springframework.orm.hibernate3.HibernateAccess or.flushIfNecessary(HibernateAccessor.java:394)
at org.springframework.orm.hibernate3.HibernateTempla te.execute(HibernateTemplate.java:366)
at org.springframework.orm.hibernate3.HibernateTempla te.save(HibernateTemplate.java:612)
at com.comp.model.dao.hibernateImpl.LoginDaoHibernate Impl.saveUser(LoginDaoHibernateImpl.java:58)
at com.comp.model.serviceImpl.LoginServiceImpl.saveUs er(LoginServiceImpl.java:43)
at com.comp.view.UserAddAction.execute(UserAddAction. java:57)
at org.apache.struts.action.RequestProcessor.processA ctionPerform(RequestProcessor.java:419)
at org.apache.struts.action.RequestProcessor.process( RequestProcessor.java:224)
at org.apache.struts.action.ActionServlet.process(Act ionServlet.java:1196)
at org.apache.struts.action.ActionServlet.doPost(Acti onServlet.java:432)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:709)
at javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:252)
at
My query:
1) Why i dont get proper error message such as "Duplicate entry or some violation error"
2) Also i am not able to catch the exception.
Code as follows:
User.hbm.xml
-------------
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="com.comp.model.businessobject">
<class name="User" table="users">
<id name="id" column="id" type="java.lang.Long">
<generator class="sequence">
<param name="sequence">users_id_seq</param>
</generator>
</id>
<property name="username" column="username" not-null="true" />
<property name="password" column="password" not-null="true" />
</class>
</hibernate-mapping>
applicationContext.xml
---------------------
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<!--
- Root application context for the Countries application.
- Web-specific beans are defined in "countries-servlet.xml".
-->
<beans>
<!--
- The message source for this context, loaded from localized "messages_xx" files
- in the classpath, i.e. "/WEB-INF/classes/messages.properties" or
- "/WEB-INF/classes/messages_fr.properties".
-
- "getMessage" calls to this context will use this source.
- Child contexts can have their own message sources, inheriting all messages from this
- source, being able to define new messages and override ones defined in this source.
-
- We have no need for application messages in this tiny application, so this
- definition will simply be used by the next level (countries-servlet.xml).
-->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="messages" />
</bean>
<!-- ========================= Start of PERSISTENCE DEFINITIONS ========================= -->
<!-- DataSource Definition -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>org.postgresql.Driver</value>
</property>
<property name="url">
<value>jdbc:postgresql://127.0.0.1:5432/somedb</value>
</property>
<property name="username">
<value>postgres</value>
</property>
<property name="password">
<value>postgres</value>
</property>
</bean>
<!-- Hibernate SessionFactory Definition -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>
com/comp/model/businessobject/User.hbm.xml
</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.PostgreSQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<!--<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>-->
<!--<prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>-->
</props>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!-- Spring Data Access Exception Translator Defintion -->
<bean id="jdbcExceptionTranslator"
class="org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator">
<property name="dataSource">
<ref bean="dataSource" />
</property>
</bean>
<!-- Hibernate Template Defintion -->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
<property name="jdbcExceptionTranslator">
<ref bean="jdbcExceptionTranslator" />
</property>
</bean>
<!-- Login DAO object: Hibernate implementation -->
<bean id="loginDao"
class="com.comp.model.dao.hibernateImpl.LoginDaoHibernateImpl">
<property name="hibernateTemplate">
<ref bean="hibernateTemplate" />
</property>
</bean>
<!-- ========================= Start of SERVICE DEFINITIONS ========================= -->
<!-- User Service Defintion -->
<bean id="loginService"
class="com.comp.model.serviceImpl.LoginServiceImpl">
<property name="loginDao">
<ref bean="loginDao" />
</property>
</bean>
<!-- Hibernate Transaction Manager Definition -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
</beans>
DAO class:
------------
Code:
public User saveUser(User user, HttpServletRequest request)
throws UserAlreadyExistClass {
User receiveduser = null;
try {
getHibernateTemplate().save(user);
} catch (Exception x) {
throw new UserAlreadyExistClass("User already exist");
}
return receiveduser;
}
I also tried adding getHibernateTemplate().flush(); after save still it didnt worked.
Thanks