Joined: Sat Dec 25, 2010 9:58 am Posts: 11
|
Hi Hardy, this is my detailed configuration.... Customer bean:- package com.libms.domain;
// Generated Oct 2, 2010 12:15:42 PM by Hibernate Tools 3.3.0.GA
import com.libms.custom.validations.EmailIdExist; import com.libms.custom.validations.NotEmpty; import com.libms.custom.validations.NotNull; import com.libms.custom.validations.Size; import com.libms.custom.validations.UserIdExist;
import java.math.BigDecimal; import java.util.HashSet; import java.util.Set;
import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; import javax.persistence.Transient; import javax.persistence.UniqueConstraint;
import org.hibernate.validator.constraints.Email;
/** * Customer generated by hbm2java */ @Entity @Table(name = "CUSTOMER", uniqueConstraints = @UniqueConstraint(columnNames = { "USERNAME", "EMAIL" })) @Matches(field = "password", verifyField = "retypepassword") public class Customer implements java.io.Serializable { /** * */ private static final long serialVersionUID = 8344606008234672532L;
@NotNull @UserIdExist(message = "userid already exists. try with different userid") private BigDecimal id;
@NotEmpty @Size(min = 1, max = 64) private String name;
@Size(min = 1, max = 256) private String addr;
@NotEmpty @Size(min = 1, max = 10) private String dob;
@NotEmpty @Size(min = 1, max = 30) private String phone;
@NotEmpty @Size(min = 1, max = 16) @UserIdExist(message = "username already exists. try with different username") private String username;
@NotEmpty @Size(min = 1, max = 32) private String password; @Transient private String retypepassword;
@NotEmpty @Email @EmailIdExist(message = "email ID already exists. try with different email id") private String email;
private Set<Card> cards = new HashSet<Card>(0); private Set<Librarian> librarians = new HashSet<Librarian>(0);
public Customer() { }
public Customer(BigDecimal id) { this.id = id; }
public Customer(BigDecimal id, String name, String addr, String dob, String phone, String username, String password, Set<Card> cards, Set<Librarian> librarians) { this.id = id; this.name = name; this.addr = addr; this.dob = dob; this.phone = phone; this.username = username; this.password = password; this.cards = cards; this.librarians = librarians; }
@Id @Column(name = "ID", unique = true, nullable = false, precision = 22, scale = 0) public BigDecimal getId() { return this.id; }
public void setId(BigDecimal id) { this.id = id; }
@Column(name = "NAME", length = 64) public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
@Column(name = "ADDR", length = 256) public String getAddr() { return this.addr; }
public void setAddr(String addr) { this.addr = addr; }
@Column(name = "DOB", length = 10) public String getDob() { return this.dob; }
public void setDob(String dob) { this.dob = dob; }
@Column(name = "PHONE", length = 30) public String getPhone() { return this.phone; }
public void setPhone(String phone) { this.phone = phone; }
@Column(name = "USERNAME", unique = true, length = 16) public String getUsername() { return this.username; }
public void setUsername(String username) { this.username = username; }
@Column(name = "PASSWORD", length = 32) public String getPassword() { return this.password; }
public void setPassword(String password) { this.password = password; }
@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer") public Set<Card> getCards() { return this.cards; }
public void setCards(Set<Card> cards) { this.cards = cards; }
@OneToMany(fetch = FetchType.EAGER, mappedBy = "customer") public Set<Librarian> getLibrarians() { return this.librarians; }
public void setLibrarians(Set<Librarian> librarians) { this.librarians = librarians; }
/** * @return the email */ @Column(name = "EMAIL", unique = true, length = 50) public String getEmail() { return email; }
/** * @param email * the email to set */ public void setEmail(String email) { this.email = email; }
/** * @return the retypepassword */ public String getRetypepassword() { return retypepassword; }
/** * @param retypepassword the retypepassword to set */ public void setRetypepassword(String retypepassword) { this.retypepassword = retypepassword; }
}
Now my Spring MVC controller is :- @Controller @RequestMapping("/admin") public class CustomerController {
@Autowired private CustomerService customerService;
@RequestMapping("/customer/save") public String saveCustomer(Customer customer) { customerService.save(customer); } }My SpringService IMPL is :- @Service public class CustomerServiceImpl implements CustomerService { @Autowired private CustomerDAO customerDAO;
@Override @Transactional public void save(Customer customer) { customerDAO.saveCustomer(customer); } }My springDAO is:- @Repository public class CustomerDAOImpl extends LibmsHibernateTemplate implements CustomerDAO {
@Override public void saveCustomer(Customer customer) { getLibmsHibernateTemplate().save(customer); //getLibmsHibernateTemplate is nothing but hibernateTempalte } }My jsp for customer is:- <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> <div class="header"> <h2>Customer Create</h2> <c:if test="${not empty message}"> <div id="message" class="success">${message}</div> <input type="hidden" id="hidSuccess" name="hidSuccess" /> </c:if> <c:if test="${error}"> <div id="message" class="error">Form has errors</div> </c:if> </div> <form:form name="createForm" id="createForm" modelAttribute="customer"> <fieldset>
<p> <form:label path="id">Id <form:errors path="id" cssClass="error_ui"></form:errors> </form:label> <form:input path="id" />
</p> <p> <form:label path="username">Login Name <form:errors path="username" cssClass="error_ui"></form:errors> </form:label> <form:input path="username" /> </p> <p> <form:label path="email">Email <form:errors path="email" cssClass="error_ui"></form:errors> </form:label> <form:input path="email" /> </p> <p> <form:label path="password">Password <form:errors path="password" cssClass="error_ui"></form:errors> </form:label> <form:password path="password" /> </p> <p> <!-- <label for="retypepwd">Re Type Password</label><input type="password" id="rpwd"> --> <form:label path="retypepassword">Re Type Password <form:errors path="retypepassword" cssClass="error_ui"></form:errors> </form:label> <form:password path="retypepassword" /> </p> <p> <form:label path="phone">Phone <form:errors path="phone" cssClass="error_ui"></form:errors> </form:label> <form:input path="phone" /> </p> </fieldset> <fieldset> <p> <form:label path="addr">Address <form:errors path="addr" cssClass="error_ui"></form:errors> </form:label> <form:textarea path="addr" /> </p> <p> <form:label path="name">Name <form:errors path="name" cssClass="error_ui"></form:errors> </form:label> <form:input path="name" /> </p> <p> <form:label path="dob">Date Of Birth <form:errors path="dob" cssClass="error_ui"></form:errors> </form:label> <form:input path="dob" readonly="true" size="8" /> </p> </fieldset>
<p class="submit"> <c:if test="${empty message}"> <a href="javascript:saveAndValidateCustomer('divId','validate','createForm');" class="button pink serif skew glass">Save</a> </c:if> </p>
<div id="divId"></div> <input type="hidden" id="pageName" name="pageName" value="${page}" /> </form:form> <script> loadCalendar('dob'); </script> Hibernate Config xml is :- <?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/b ... ns-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/c ... xt-3.0.xsd">
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/libms_schema" /> <property name="username" value="root" /> <property name="password" value="password" /> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="annotatedClasses"> <list> <value>com.libms.domain.Customer</value> </list> </property> <property name="hibernateProperties"> <props> <!-- <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> --> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">true</prop> <!-- <prop key="hibernate.format_sql">true</prop> <prop key="hibernate.hbm2ddl.auto">create</prop> --> </props> </property> </bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> </beans> Please check the retypepassword field. This I declared @Transient but it still persists in to hiebranteTempalate save. Foolowing is the error occuring Exception!: org.springframework.dao.InvalidDataAccessResourceUsageException: Could not execute JDBC batch update; SQL [insert into CUSTOMER (ADDR, DOB, EMAIL, NAME, PASSWORD, PHONE, retypepassword, USERNAME, ID) values (?, ?, ?, ?, ?, ?, ?, ?, ?)]; nested exception is org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
One more thing I'm serializing the form using JQuery Ajax call jQuery.ajax({ type : 'post', url : 'save', onCreate : showLoader(divId, 'Please wait...'), data : jQuery("#formName").serialize(), success : function(html) { jQuery("#formcontainer").val(''); jQuery("#formcontainer").html(html); }, error : function(xhr, ajaxOptions, thrownError) { // alert(xhr.status); // alert(thrownError); alert("failed: " + xhr.status + " " + thrownError); } });
|
|