I am using JPA with Spring 3.0 and Struts 2.2.3. And I am very new to using Spring and JPA.
I have created a class named GenericDAO which extends JpaDaoSupport. This class is then extended by other application specific DAOs.
I can retrieve data using find method of JPATemplate. But I have not been able to save objects using persist method. I had previously thought that there might be problem in my annotations while configuring many to one relationship. But as I am able to get object in that relationship, i cannot understand why I am not able to save objects.
Following are the various files I am using.
ApplicationContext.xml
GenericDAO.javaCode:<?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:aop="http://www.springframework.org/schema/aop" 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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd" default-autowire="autodetect" > <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> <bean id="genericDAO" class="shareProj.util.GenericDAO" abstract="true" > <property name="entityManagerFactory" ref="entityManagerFactory" ></property> </bean> <bean id="loginDao" class="shareProj.authenticate.LoginDAOImplJPA" scope="singleton" parent="genericDAO" ></bean> <bean id="rolesDao" class="shareProj.authenticate.RolesDAOImpl" scope="singleton" parent="genericDAO" ></bean> <bean id="adminDao" class="shareProj.admin.AdminDaoImpl" /> <bean id="customeDao" class="shareProj.customer.CustomerDaoImpl" /> <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/shareProj" /> <property name="username" value="root" /> <property name="password" value="root" /> </bean> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> <property name="database" value="MYSQL" /> <property name="showSql" value="true" /> </bean> </property> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
Code:package shareProj.util; import java.io.Serializable; import java.util.List; import org.springframework.orm.jpa.support.JpaDaoSupport; import org.springframework.transaction.annotation.Transactional; public abstract class GenericDAO<T, ID extends Serializable> extends JpaDaoSupport{ private Class<T> persistentClass; public Class<T> getPersistentClass() { return persistentClass; } public void setPersistentClass(Class<T> persistentClass) { this.persistentClass = persistentClass; } @Transactional public void save(T entity) { getJpaTemplate().persist(entity); } @Transactional public void delete(T entity) { getJpaTemplate().remove(entity); } @Transactional public T getById(Object id) { System.out.println("Entity Manager: " + getJpaTemplate()); return getJpaTemplate().find(persistentClass, id); } @SuppressWarnings("unchecked") @Transactional public List<T> getList() { System.out.println(persistentClass); return (List<T>)getJpaTemplate().find("select t from "+ persistentClass.getName() +" t"); } }
User.java
Code:package shareProj.domainObjects; import javax.persistence.CascadeType; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.Table; @Entity @Table(name="users") public class User implements Serializable { private static final long serialVersionUID = 6199783429677613435L; @Id @GeneratedValue(strategy=GenerationType.AUTO) private int userid; private String userName; @Column(name="password") private String password; @Column(name="password", insertable=false, updatable=false ) private String retypePass; private String firstName; private String middleName; private String lastName; private boolean gender; private String contactNo; private String email; private String loweredEmail; private String passwordQuestion; private String passwordAnswer; private boolean isApproved; private boolean isLockedOut; @Column(name="CreateDate") private Date createDate; @Column(name="LastLoginDate") private Date lastLoginDate; @Column(name="lastPasswordChangedDate") private Date lastPasswordChangeDate; @Column(name="LastLockoutDate") private Date lastLockoutDate; @Column(name="FailedPasswordAttemptCount") private int failedPasswordAttemptCount; @Column(name="FailedPasswordAnswerAttemptCount") private int failedPasswordAnswerAttemtCount; @Column(name="Comment") private String comment; @Column(name="password", insertable=false, updatable=false ) private String newPassword; @ManyToOne(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}, targetEntity=Roles.class) @JoinColumn(name="RoleId", nullable=false) private Roles roles; public User() { } //Getter Setter methods. }
Roles.java
Code:package shareProj.domainObjects; import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.Table; @Entity @Table(name="roles") public class Roles implements Serializable { /** * */ private static final long serialVersionUID = -8876159097748295926L; @Id @GeneratedValue private int roleId; private String roleName; private String loweredRoleName; private String description; @OneToMany(mappedBy="roles", cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE}) private Set<User> users = new HashSet<User>(); public Roles() { } // Getter setter methods }
From what I have seen on forums, my code seems ok. There might be some problem with my spring configuration. I dont know what could be the problem. Would someone be able to help me with this??
Thank You in advance.


Reply With Quote
