Announcement Announcement Module
Collapse
No announcement yet.
persist() method doesn't seem to work Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • persist() method doesn't seem to work

    I am new to JPA.
    I was trying out an application. I can retrieve data from tables, but the persist() method is not saving data into table. I am using Netbeans 7.0., Glassfish Server. Plz tell me where i'm getting wrong.

    My persistence.xml

    <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="WebApplication1PU" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceP rovider</provider>
    <jta-data-source>abjetdb_</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
    <property name="eclipselink.ddl-generation" value="create-tables"/>
    </properties>
    </persistence-unit>
    </persistence>



    My applicationcontext.xml

    <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverM anagerDataSource"
    p:driverClassName="com.mysql.jdbc.Driver"
    p:url="jdbc:mysql://localhost:3306/abjetdb"
    p:username="root"
    password="root" />

    <!-- ADD PERSISTENCE SUPPORT HERE (jpa, hibernate, etc) -->

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerE ntityManagerFactoryBean">
    <property name="persistenceUnitName" value="WebApplication1PU"/>

    <property name="jpaVendorAdapter">
    <bean class="org.springframework.orm.jpa.vendor.EclipseL inkJpaVendorAdapter">
    <property name="showSql" value="true" />
    <property name="generateDdl" value="true" />
    <property name="databasePlatform" value="org.eclipse.persistence.platform.database.M ySQLPlatform" />
    </bean>
    </property>
    <property name="loadTimeWeaver">
    <bean class="org.springframework.instrument.classloading .ReflectiveLoadTimeWeaver"/>
    </property>
    </bean>

    <bean id="txManager" class="org.springframework.orm.jpa.JpaTransactionM anager">

    <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <bean class="org.springframework.orm.jpa.support.Persist enceAnnotationBeanPostProcessor"/>
    <tx:annotation-driven transaction-manager="txManager" />

    </beans>


    My entity class

    @Entity
    @Table(name = "emp")
    @XmlRootElement
    @NamedQueries({
    @NamedQuery(name = "Emp.findAll", query = "SELECT e FROM Emp e"),
    @NamedQuery(name = "Emp.findById", query = "SELECT e FROM Emp e WHERE e.id = :id"),
    @NamedQuery(name = "Emp.findByFirstname", query = "SELECT e FROM Emp e WHERE e.firstname = :firstname")})
    public class Emp implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "id")
    private Long id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 50)
    @Column(name = "firstname")
    private String firstname;

    public Emp() {
    }

    public Emp(Long id) {
    this.id = id;
    }

    public Emp(Long id, String firstname) {
    this.id = id;
    this.firstname = firstname;
    }

    public Long getId() {
    return id;
    }

    public void setId(Long id) {
    this.id = id;
    }

    public String getFirstname() {
    return firstname;
    }

    public void setFirstname(String firstname) {
    this.firstname = firstname;
    }

    @Override
    public int hashCode() {
    int hash = 0;
    hash += (id != null ? id.hashCode() : 0);
    return hash;
    }

    @Override
    public boolean equals(Object object) {
    // TODO: Warning - this method won't work in the case the id fields are not set
    if (!(object instanceof Emp)) {
    return false;
    }
    Emp other = (Emp) object;
    if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
    return false;
    }
    return true;
    }

    @Override
    public String toString() {
    return "com.web.model.Emp[ id=" + id + " ]";
    }

    }

    My dispatcher-servlet.xml

    <bean class="org.springframework.web.servlet.mvc.support .ControllerClassNameHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.annotat ion.AnnotationMethodHandlerAdapter" />
    <bean class="org.springframework.web.servlet.mvc.annotat ion.DefaultAnnotationHandlerMapping" />

    <context:component-scan base-package="com.web" />
    <mvc:annotation-driven />
    <bean id="mydaoimpl" class="com.web.dao.MyDAOImpl" />

    <bean id="viewResolver"
    class="org.springframework.web.servlet.view.Intern alResourceViewResolver"
    prefix="/WEB-INF/jsp/"
    p:suffix=".jsp" />

    My DAO

    public class MyDAOImpl{

    @PersistenceContext
    EntityManager em;

    public EntityManager getEm() {
    return em;
    }

    public void setEm(EntityManager em) {
    this.em = em;
    }

    @Transactional(propagation=Propagation.REQUIRED,re adOnly=false)
    public void save(){

    Emp e=new Emp();
    e.setFirstname("tester");
    e.setId(2L);
    em.persist(e);
    System.out.println("Object Saved");
    }
    }

    My controller

    @Controller
    public class tester {
    @Autowired
    MyDAOImpl mydaoimpl;

    public MyDAOImpl getMydaoimpl() {
    return mydaoimpl;
    }

    public void setMydaoimpl(MyDAOImpl mydaoimpl) {
    this.mydaoimpl = mydaoimpl;
    }

    @RequestMapping("/tester.htm")
    public String test(ModelMap model){

    mydaoimpl.save();

    return "success";
    }
    }


    Plz help me out.
    Thnx in Advance.

  • #2
    Use [ code][/code ] tags when posting code that way it remains readable !!!

    I suggest using the forum search as this question has been answered numerous times before. In short your setup is wrong. You have tx:annotation-driven in the root context whereas your beans are in the dispatcherservlet context. Bean(Factory)PostProcessors only operate on the context they are defined in not the parent or child contexts!

    Comment


    • #3
      So Marten what do you suggest, i should do?

      Comment


      • #4
        Well how about reading my post?!

        Step 1 (you should be able to solve it with these pointers)
        Originally posted by mdeinum
        In short your setup is wrong. You have tx:annotation-driven in the root context whereas your beans are in the dispatcherservlet context. Bean(Factory)PostProcessors only operate on the context they are defined in not the parent or child contexts!
        If that fails
        Originally posted by mdeinum
        I suggest using the forum search as this question has been answered numerous times before.
        Last edited by Marten Deinum; Aug 29th, 2011, 08:29 AM.

        Comment


        • #5
          Problem Solved!!!!!
          Thnx Marten

          Comment

          Working...
          X