1

I connected data base with hibernate to my application with this settings:

hibernateContext.xml

    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate4.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="hibernateUserDao" class="org.springframework.web.basepackage.HibernateUserDao">
        <property name="hibernateTemplate" ref="hibernateTemplate" />
    </bean>  

this is hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
  <session-factory>
      <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/fullproject</property>
      <property name="hibernate.connection.username">root</property>
      <property name="hibernate.connection.password">admin</property>
      <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
      <property name="hibernate.hbm2ddl.auto">update</property>
      <property name="hibernate.show_sql">true</property>
      <mapping resource="user.hbm.xml" />
  </session-factory>
</hibernate-configuration>

this is user.hbm.xml

<?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>
    <class name="org.springframework.web.basepackage.User" table="users">
        <id name="id" column="id">
            <generator class="native" />
        </id>
        <property name="login" column="login"/>
        <property name="password" column="password" />
        <property name="email" column="email" />
    </class>
</hibernate-mapping>

this is my user entity

public class User {

    private int id;
    private String login;
    private String password;
    private String email;

    public User(){

    }

    public User(String login, String password, String email){
        this.login = login;
        this.password = password;
        this.email = email;
    }

    public int getId(){
        return id;
    }
    public String getLogin(){
        return login;
    }
    public String getPassword(){
        return password;
    }
    public String getEmail(){
        return email;
    }

    public void setId(int id){
        this.id = id;
    }
    public void setLogin(String login){
        this.login = login;
    }
    public void setPassword(String password){
        this.password = password;
    }
    public void setEmail(String email){
        this.email = email;
    }
}

when i try save db in this way "hibernateTemplate.save(user)' i catch in console this line "Hibernate: insert into users (login, password, email) values (?, ?, ?)" and changes don't save into database. I tried in user.hbn.xml change generator class to "increment" because my id in data base table has AUTO INCREMENT parameter, but it doesn't work.

user3491167
  • 91
  • 3
  • 13

1 Answers1

0

This sound like your transaction didn't get commit after update. When you call HibernateTemplate.update() it only update your persistent but doesn't actually to commit those changes. Remember to always conclude your transaction and save it before moving on.

Here are some resources on hibernate sessions and transactions that might help you with your current problem 1 and 2

Community
  • 1
  • 1
Tri Chu
  • 110
  • 8
  • before that the settings from hibernate.cfg.xml were located in hibernateContext.xml(on top of post) as DataSource bean and all worked fine. but when i moved this settings to separate hibernate.cfg.xml then i see this issue – user3491167 Nov 07 '14 at 07:03
  • When you used the datasource bean did you also use the spring hibernateTemplate or did you just inject the session factory and create session with Spring? Have you also tried template.getSessionFactory().getCurrentSession().getTransaction().commit() ? – Tri Chu Nov 10 '14 at 00:52