2

I have a form where i input details, but when i click on save it does not get saved in the database... though the table does get created.

My Contact POJO

package your.intermedix.domain;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="USER")

public class Contact implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long id;
    private String name;
    private String email;
    private String lastname;
    private String designation;

    @Id
    @GeneratedValue
    @Column(name="USER_ID")
    public Long getId() {
        return id;
    }

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

    @Column(name="DESIGNATION")
    public String getDesignation(){
        return designation;
    }

    public void setDesignation(String designation){
        this.designation = designation;
    }

    @Column(name="EMAIL")
    public String getEmail(){
        return email;
    }

    public void setEmail(String email){
        this.email = email;
    }

    @Column(name="LASTNAME")
    public String getLastname(){
        return lastname;
    }

    public void setLastname(String lastname){
        this.lastname= lastname;
    }

    @Column(name="FIRSTNAME")
    public String getName(){
        return name;
    }

    public void setName(String name){
        this.name = name;
    }


    public String toString()
    {
        return "designation = '" + designation + "',email='"+ email +"', lastname='"+ lastname +"', name = '" + name + "'";
    }

}

My Application-Context.xml

    <?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:context="http://www.springframework.org/schema/context"
  xmlns:p="http://www.springframework.org/schema/p"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="
  http://www.springframework.org/schema/tx 
  http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!-- Turn on AspectJ @Configurable support -->

<context:spring-configured />
<context:property-placeholder location="classpath*:*.properties" />
<context:component-scan base-package="your.intermedix"/>
<context:annotation-config/>
 <!-- enable the configuration of transactional behavior based on annotations -->
  <tx:annotation-driven transaction-manager="txManager"/>

  <!-- a PlatformTransactionManager is still required -->
  <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <!-- (this dependency is defined somewhere else) -->
  <property name="dataSource" ref="myDataSource"/>
  </bean>


<!-- Turn on @Autowired, @PostConstruct etc support -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor" />


    <bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="myDataSource" />
        <property name="annotatedClasses">
            <list>
                <value>your.intermedix.domain.Contact</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.hbm2ddl.auto">create</prop>
            </props>
        </property>
    </bean>

    <bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://127.0.0.1:3306/spring"/>
        <property name="username" value="monty"/>
        <property name="password" value="indian"/>
    </bean>   
</beans>

I am not getting any sort of error.... in the console.

Updated code..

package your.intermedix.services;

import org.hibernate.SessionFactory;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Service;

import your.intermedix.domain.Contact;
import your.intermedix.services.IContact;

@Service
public class ContactSerImpl implements IContact {

    private HibernateTemplate hibernateTemplate;

        public void setSessionFactory(SessionFactory sessionFactory) {
            this.hibernateTemplate = new HibernateTemplate(sessionFactory);
    }
            @Transactional
        public void saveContact(Contact contact) {
            System.out.println("Hello Guru contact");
            System.out.println(contact);
            hibernateTemplate.saveOrUpdate(contact);
        }

        public void hello() {
            System.out.println("Hello Guru");
        }
}

My Service class where i the print statements work

7
  • Can you give us the save code ? Your method where you create your new object and save it. Commented Jan 3, 2011 at 14:11
  • You've shown us your entity & configuration, but you haven't shown us the relevant part. That is, code handling your form. Commented Jan 3, 2011 at 14:11
  • Have you defined a transaction ? Commented Jan 3, 2011 at 14:17
  • Where should i define it... and why? Commented Jan 3, 2011 at 14:18
  • I am also assuming that your println Hello Guru is printed out? I would also recommend using log4j or another logging system and logging the hibernate classes to see what is going on. Commented Jan 3, 2011 at 14:30

2 Answers 2

5

You need a running transaction. Spring transaction management is the way to go if using HibernateTemplate. Read the documentation. It's too long to include in an answer, but here is in short:

  • you need to define a transaction manager as a spring bean
  • you need <tx:annotation-driven />
  • you need to annotate your transactional methods with @Transactional
Sign up to request clarification or add additional context in comments.

4 Comments

I have updated my main post, where i have added Transaction Manager and other stuffs. Still the object does not get persist in the DB.
@theJava - you'd need a HibernateTransactionManager
<bean id="myTxManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="mySessionFactory" /> </bean> I updated with this and does not work either
@thejava did you update the manager id in tx annotation driven
3

I don't know the Spring-Hibernate framework, but often when data is not written it menas it is not flushed to the databackend. What gives you

System.err.println(hibernateTemplate.getFlushMode());

1 Comment

This statement does not get called itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.