OK, we have an Hibernate 3 application that I am trying to update to hibernate 4. I can retrieve the data without any problem, but cannot add or update the database. I don't get any error messages, the transaction seems to work, but nothing gets changed in the database. Some help would be greatly appreciated.
Here's the config file hibernate.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">false</property>
<property name="hibernate.current_session_context_class">thread</property>
<property name="hibernate.connection.datasource">jdbc/misc</property>
<property name="hibernate.jndi.url">iiop://127.0.0.1:3700</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.SunONETransactionManagerLookup</property>
<mapping resource="sponsor.hbm.xml"/>
<mapping resource="portfolio.hbm.xml"/>
<mapping resource="clientID.hbm.xml"/>
<mapping resource="legacyID.hbm.xml"/>
<mapping resource="badClient.hbm.xml"/>
<mapping resource="language.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Here's a sam;ole map file of the table I am currently working with sponsor.hbm.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="com.lingosys.hibernate.Sponsor" table="sponsor">
<id name="companyID" type="integer">
<generator class="assigned"/>
</id>
<property name="companyName" type="string"/>
<property name="sponsorID" type="integer"/>
<property name="sponsorName" type="string"/>
<property name="status" type="string"/>
</class>
</hibernate-mapping>
Here's the class for creating the session factory HibenateUtil.java:
package com.lingosys.hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
/**
* Hibernate Utility class with a convenient method to get Session Factory object.
*
* @author mphoenix
*/
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Here's the dao file SponsorDAO.java:
package com.lingosys.hibernate;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
/**
*
* @author mphoenix
*/
public class SponsorDAO {
private Session session = null;
public SponsorDAO() {
}
private void startOperation() {
session = HibernateUtil.getSessionFactory().openSession();
}
public void create(Sponsor sponsor) {
startOperation();
session.saveOrUpdate("Sponsor", sponsor);
session.close();
}
public void update(Sponsor sponsor) {
startOperation();
session.update("Sponsor", sponsor);
session.close();
}
public Sponsor findSponsor(int id) {
startOperation();
Sponsor sponsor = (Sponsor) session.get(Sponsor.class, new Integer(id));
session.close();
return sponsor;
}
public List <Sponsor> findAllSponsors() {
List <Sponsor> sponsors = null;
startOperation();
Query query = session.createQuery("from Sponsor");
sponsors = query.list();
session.close();
return sponsors;
}
public void delete(Sponsor sponsor) {
startOperation();
session.delete(sponsor);
session.close();
}
}
Here's the create transaction code:
UserTransaction tx = null;
try {
tx = (UserTransaction)new InitialContext()
.lookup("java:comp/UserTransaction");
tx.begin();
dao.create(sponsor);
tx.commit();
} catch (HibernateException ex) {
try {
tx.rollback();
} catch (Exception ex2) {
facesCtx.addMessage(null, new FacesMessage("Error on rollback.",
ex2.toString()));
}
facesCtx.addMessage(null, new FacesMessage("Hibernate Error.",
ex.toString()));
return null;
} catch (Exception ex) {
facesCtx.addMessage(null, new FacesMessage("Non-hibernate Error.",
ex.toString()));
return null;
}
Here's a retrieval transaction:
try {
tx = (UserTransaction)new InitialContext()
.lookup("java:comp/UserTransaction");
tx.begin();
setSponsorItems(new ArrayList<Sponsor>());
List<Sponsor> sponsors = dao.findAllSponsors();
for (Sponsor aSponsor : sponsors) {
if (companyItemsMap.get(aSponsor.getCompanyID()) != null) {
getSponsorItems().add(aSponsor);
}
}
tx.commit();
} catch (HibernateException ex) {
try {
tx.rollback();
} catch (Exception ex2) {
facesCtx.addMessage(null, new FacesMessage(ex2.toString()));
}
facesCtx.addMessage(null, new FacesMessage(ex.toString()));
return false;
} catch (Exception ex) {
facesCtx.addMessage(null, new FacesMessage(ex.toString()));
return false;
}
And finally the object being mapped to Sponsor.java
package com.lingosys.hibernate;
import java.io.Serializable;
/**
*
* @author mphoenix
*/
public class Sponsor implements Serializable {
private int companyID;
private String companyName;
private int sponsorID;
private String sponsorName;
private String status;
public Sponsor() {
}
public int getCompanyID() {
return companyID;
}
public void setCompanyID(int companyID) {
this.companyID = companyID;
}
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
public int getSponsorID() {
return sponsorID;
}
public void setSponsorID(int sponsorID) {
this.sponsorID = sponsorID;
}
public String getSponsorName() {
return sponsorName;
}
public void setSponsorName(String sponsorName) {
this.sponsorName = sponsorName;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Sponsor other = (Sponsor) obj;
if (this.companyID != other.companyID) {
return false;
}
return true;
}
@Override
public int hashCode() {
int hash = 5;
hash = 97 * hash + this.companyID;
return hash;
}
//DEBUG CASPERW
@Override
public String toString() {
return "CompanyID: "+companyID+" CompanyName: "+companyName+" SponsorID: "+sponsorID+" SponsorName: "+sponsorName+"\n";
}
}