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?