These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: save method doesn't save!!!
PostPosted: Fri Oct 27, 2006 5:04 am 
Regular
Regular

Joined: Mon Oct 02, 2006 12:03 pm
Posts: 62
Hello forum. I'v developing a webshop with hibernate technology. I'm going to enclosing to concrete problem: Role entity:

I have these files:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 24-oct-2006 10:13:50 by Hibernate Tools 3.2.0.beta8 -->
<hibernate-mapping>
    <class name="com.gmsoft.model.Role" table="ROLE">
        <id name="codi" type="int">
            <column name="CODI" precision="38" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="descripcio" type="string">
            <column name="DESCRIPCIO" length="32" not-null="true" />
        </property>
        <set name="clients" inverse="true">
            <key>
                <column name="ROL" precision="38" scale="0" not-null="true" />
            </key>
            <one-to-many class="com.gmsoft.model.Client" />
        </set>
    </class>
</hibernate-mapping>


I have created two classe as follow:

Role.java



Code:
package com.gmsoft.model;

// Generated 24-oct-2006 10:13:50 by Hibernate Tools 3.2.0.beta8

import java.util.HashSet;
import java.util.Set;

/**
* Role generated by hbm2java
*/
public class Role implements java.io.Serializable {

   // Fields   

   private int codi;

   private String descripcio;

   private Set<Client> clients = new HashSet<Client>(0);

   // Constructors

   /** default constructor */
   public Role() {
   }

   /** minimal constructor */
   public Role(int codi, String descripcio) {
      this.codi = codi;
      this.descripcio = descripcio;
   }

   /** full constructor */
   public Role(int codi, String descripcio, Set<Client> clients) {
      this.codi = codi;
      this.descripcio = descripcio;
      this.clients = clients;
   }

   // Property accessors
   public int getCodi() {
      return this.codi;
   }

   public void setCodi(int codi) {
      this.codi = codi;
   }

   public String getDescripcio() {
      return this.descripcio;
   }

   public void setDescripcio(String descripcio) {
      this.descripcio = descripcio;
   }

   public Set<Client> getClients() {
      return this.clients;
   }

   public void setClients(Set<Client> clients) {
      this.clients = clients;
   }

}


And RoleDAO.java -->

Code:
public class RoleDAO {

...

   public void persist(Role transientInstance) {
      log.debug("persisting Role instance");
      try {
         org.hibernate.Session s = HibernateUtil.currentSession();
         s.save(transientInstance);
         s.flush();   <<---------------

         log.debug("persist successful");
      } catch (RuntimeException re) {
         log.error("persist failed", re);
         throw re;
      }
...
   }


And HibernateUtil class is typical:

HibernateUtil.java -->

Code:
public class HibernateUtil {

    private static Log log = LogFactory.getLog(HibernateUtil.class);

    private static final SessionFactory sessionFactory;

    static {
        try {
            // Create the SessionFactory
            sessionFactory = new Configuration().configure("./webshop/WEB-INF/classes/hibernate.cfg.xml").buildSessionFactory();
        } catch (Throwable ex) {
            log.error("Initial SessionFactory creation failed.", ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static final ThreadLocal session = new ThreadLocal();

    public static Session currentSession() throws HibernateException {
        Session s = (Session) session.get();
        // Open a new Session, if this Thread has none yet
        if (s == null) {
            s = sessionFactory.openSession();
            session.set(s);
        }
        return s;
    }

    public static void closeSession() throws HibernateException {
        Session s = (Session) session.get();
        session.set(null);
        if (s != null)
            s.close();
    }
}


At last, Hibernate.cfg.xml -->

Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
    <session-factory name="GeneralSessionFactory">
        <property name="hibernate.bytecode.use_reflection_optimizer">false</property>
        <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="hibernate.connection.password">password</property>
        <property name="hibernate.connection.url">jdbc:oracle:thin:@192.168.1.2:1500:webshop</property>
        <property name="hibernate.connection.username">user</property>
        <property name="hibernate.default_schema">APP</property>
        <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
        <mapping resource="com/gmsoft/model/Role.hbm.xml" />
        <mapping resource="com/gmsoft/model/Client.hbm.xml" />
    </session-factory>
</hibernate-configuration>


So, I execute this code, in doPost method on my ControllerServlet -->

Code:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      try {
         response.getWriter().println("Intentant guardar role...");
         com.gmsoft.model.Role rol = new Role();
         rol.setCodi(60);
         rol.setDescripcio("Hibernate");
         com.gmsoft.model.RoleHome roleDAO = new RoleHome();
         roleDAO.persist(rol);
         response.getWriter().println("Role guardat flush!!!");
      }catch (Exception ex) {
         response.getWriter().println(ex.getMessage());
         ex.printStackTrace(response.getWriter());
      }
   }


However, my new role isn't saved to my database. Why?
I execute "SELECT * FROM ROLE;" into SQLPLUS session but this role has been saved. Can you explain why it doesn't work correctly?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 27, 2006 5:18 am 
Senior
Senior

Joined: Mon Oct 23, 2006 5:12 am
Posts: 141
Location: Galicia, Spain
You must enclose your DAO code in a transaction:

public void persist(Role transientInstance) {
log.debug("persisting Role instance");
try {
org.hibernate.Session s = HibernateUtil.currentSession();
s.beginTransaction();
s.save(transientInstance);
//s.flush(); NOT NEEDED
s.getTransaction().commit();
log.debug("persist successful");
} catch (RuntimeException re) {
log.error("persist failed", re);
throw re;
}

Regards

_________________
andresgr (--don't forget to rate)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 27, 2006 7:57 am 
Regular
Regular

Joined: Mon Oct 02, 2006 12:03 pm
Posts: 62
Thanks, I'm a scatterbrained


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 27, 2006 8:02 am 
Senior
Senior

Joined: Mon Oct 23, 2006 5:12 am
Posts: 141
Location: Galicia, Spain
If the answer did help you, please rate.

_________________
andresgr (--don't forget to rate)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.