Hello,
I am working with Hibernate for 6 month now. I have developed a small 2tier application with Hibernate, so I will make a small summary and decide, if I will use Hibernate for my next big RCP application.
I used Hibernate 3.1.3 for my first application, along with the Hibernate-tools 3.1.0beta5. We begun to build the database model, then generated all model, dao and mapping files with the Hibernate-tools. It was really fine, never thought that creating the model-layer could be done so fast. After that we begun to build the user interface with Swing.
My problems with Hibernate was the following:
Starting up the application takes a long time, about 5 - 10 seconds. The reason is, that hibernate takes a long time to initialize the configuration and the mappings:
Code:
>>> hibernate startup <<<
2006-07-15 18:00:29,765 [main] INFO javakitty.init.JavaKITTYInit - Logger Started
2006-07-15 18:00:29,765 [main] INFO javakitty.init.InitJavaKITTY - Starting JavaKITTY
2006-07-15 18:00:29,765 [main] INFO javakitty.init.InitJavaKITTY - Initializing Hibernate session factory
2006-07-15 18:00:29,859 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.1.3
2006-07-15 18:00:29,875 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found
2006-07-15 18:00:29,875 [main] INFO org.hibernate.cfg.Environment - using CGLIB reflection optimizer
2006-07-15 18:00:29,875 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
2006-07-15 18:00:29,984 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml
2006-07-15 18:00:29,984 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml
2006-07-15 18:00:30,281 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource: javakitty/model/Admin.hbm.xml
2006-07-15 18:00:30,406 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: javakitty.model.Admin -> ADMIN
2006-07-15 18:00:30,437 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource: javakitty/model/EventAdmin.hbm.xml
2006-07-15 18:00:30,484 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: javakitty.model.EventAdmin -> EVENTADMIN
2006-07-15 18:00:30,546 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource: javakitty/model/HardwareOffer.hbm.xml
2006-07-15 18:00:30,593 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: javakitty.model.HardwareOffer -> HARDWARE_OFFER
2006-07-15 18:00:30,593 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource: javakitty/model/Transaction.hbm.xml
2006-07-15 18:00:30,625 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: javakitty.model.Transaction -> TRANSACTION
2006-07-15 18:00:30,625 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource: javakitty/model/Food.hbm.xml
2006-07-15 18:00:30,640 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: javakitty.model.Food -> FOOD_MEAL
2006-07-15 18:00:30,640 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource: javakitty/model/User.hbm.xml
2006-07-15 18:00:30,656 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: javakitty.model.User -> REGISTER_PERSON
2006-07-15 18:00:30,671 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource: javakitty/model/Tournament.hbm.xml
2006-07-15 18:00:30,734 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: javakitty.model.Tournament -> TOURNAMENT_TOURNAMENT
2006-07-15 18:00:30,734 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource: javakitty/model/Event.hbm.xml
2006-07-15 18:00:30,750 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: javakitty.model.Event -> EVENT
2006-07-15 18:00:30,750 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource: javakitty/model/TournamentRegistration.hbm.xml
2006-07-15 18:00:30,765 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: javakitty.model.TournamentRegistration -> TOURNAMENT_TOURNREG
2006-07-15 18:00:30,765 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource: javakitty/model/TournamentTeam.hbm.xml
2006-07-15 18:00:30,781 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: javakitty.model.TournamentTeam -> TOURNAMENT_TEAM
2006-07-15 18:00:30,781 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource: javakitty/model/EventRegistration.hbm.xml
2006-07-15 18:00:30,796 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: javakitty.model.EventRegistration -> REGISTER_REGISTRATION
2006-07-15 18:00:30,796 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource: javakitty/model/FoodOfferPosition.hbm.xml
2006-07-15 18:00:30,843 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: javakitty.model.FoodOfferPosition -> FOOD_MEALOFFER
2006-07-15 18:00:30,843 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource: javakitty/model/HardwareOfferPosition.hbm.xml
2006-07-15 18:00:30,859 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: javakitty.model.HardwareOfferPosition -> HARDWARE_HARDWAREOFFER
2006-07-15 18:00:30,859 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource: javakitty/model/FoodOrderPosition.hbm.xml
2006-07-15 18:00:30,859 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: javakitty.model.FoodOrderPosition -> FOOD_MEALORDERING
2006-07-15 18:00:30,875 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource: javakitty/model/FoodOffer.hbm.xml
2006-07-15 18:00:30,875 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: javakitty.model.FoodOffer -> FOOD_OFFER
2006-07-15 18:00:30,890 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource: javakitty/model/Hardware.hbm.xml
2006-07-15 18:00:30,890 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: javakitty.model.Hardware -> HARDWARE_HARDWARE
2006-07-15 18:00:30,890 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource: javakitty/model/HardwareRent.hbm.xml
2006-07-15 18:00:30,906 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: javakitty.model.HardwareRent -> RENT
2006-07-15 18:00:30,906 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource: javakitty/model/Ordering.hbm.xml
2006-07-15 18:00:30,968 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: javakitty.model.Ordering -> ORDERING
2006-07-15 18:00:30,968 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource: javakitty/model/TournamentDuel.hbm.xml
2006-07-15 18:00:30,984 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: javakitty.model.TournamentDuel -> TOURNAMENT_DUEL
2006-07-15 18:00:30,984 [main] INFO org.hibernate.cfg.Configuration - Reading mappings from resource: javakitty/model/HardwareRentPosition.hbm.xml
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: javakitty.model.HardwareRentPosition -> HARDWARE_RENT
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.Configuration - Configured SessionFactory: null
2006-07-15 18:00:31,000 [main] DEBUG javakitty.init.InitSessionFactory - classic factory
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.Admin.transactions -> TRANSACTION
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.Admin.eventadmins -> EVENTADMIN
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.HardwareOffer.hardwareHardwareoffers -> HARDWARE_HARDWAREOFFER
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.Transaction.orderings -> ORDERING
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.Transaction.rents -> RENT
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.Food.foodMealorderings -> FOOD_MEALORDERING
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.Food.foodMealoffers -> FOOD_MEALOFFER
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.User.registerRegistrations -> REGISTER_REGISTRATION
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.Tournament.tournamentTeams -> TOURNAMENT_TEAM
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.Tournament.tournamentDuels -> TOURNAMENT_DUEL
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.Tournament.tournamentTournregs -> TOURNAMENT_TOURNREG
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.Event.foodOffers -> FOOD_OFFER
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.Event.hardwareOffers -> HARDWARE_OFFER
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.Event.eventadmins -> EVENTADMIN
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.Event.registerRegistrations -> REGISTER_REGISTRATION
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.Event.tournamentTournaments -> TOURNAMENT_TOURNAMENT
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.TournamentRegistration.tournamentTeams -> TOURNAMENT_TEAM
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.TournamentTeam.tournamentTournregs -> TOURNAMENT_TOURNREG
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.TournamentTeam.tournamentDuelsForOpponent1 -> TOURNAMENT_DUEL
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.TournamentTeam.tournamentDuelsForOpponent2 -> TOURNAMENT_DUEL
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.TournamentTeam.tournamentDuelsForWinner -> TOURNAMENT_DUEL
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.EventRegistration.rents -> RENT
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.EventRegistration.orderings -> ORDERING
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.EventRegistration.tournamentTournregs -> TOURNAMENT_TOURNREG
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.EventRegistration.transactions -> TRANSACTION
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.FoodOffer.foodMealoffers -> FOOD_MEALOFFER
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.Hardware.hardwareHardwareoffers -> HARDWARE_HARDWAREOFFER
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.Hardware.hardwareRents -> HARDWARE_RENT
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.HardwareRent.hardwareRents -> HARDWARE_RENT
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.Ordering.foodMealorderings -> FOOD_MEALORDERING
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.TournamentDuel.tournamentDuelsForPredecessor2 -> TOURNAMENT_DUEL
2006-07-15 18:00:31,000 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: javakitty.model.TournamentDuel.tournamentDuelsForPredecessor1 -> TOURNAMENT_DUEL
2006-07-15 18:00:31,234 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
2006-07-15 18:00:31,234 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 1
2006-07-15 18:00:31,234 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false
2006-07-15 18:00:31,234 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:hsql://localhost/javaKittyDB
2006-07-15 18:00:31,234 [main] INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=kitty, password=****}
2006-07-15 18:00:31,359 [main] INFO org.hibernate.cfg.SettingsFactory - RDBMS: HSQL Database Engine, version: 1.8.0
2006-07-15 18:00:31,359 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC driver: HSQL Database Engine Driver, version: 1.8.0
2006-07-15 18:00:31,406 [main] INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.HSQLDialect
2006-07-15 18:00:31,421 [main] INFO org.hibernate.transaction.TransactionFactoryFactory - Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory
2006-07-15 18:00:31,421 [main] INFO org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
2006-07-15 18:00:31,421 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
2006-07-15 18:00:31,437 [main] INFO org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
2006-07-15 18:00:31,437 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch size: 15
2006-07-15 18:00:31,437 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled
2006-07-15 18:00:31,437 [main] INFO org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
2006-07-15 18:00:31,437 [main] INFO org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): disabled
2006-07-15 18:00:31,437 [main] INFO org.hibernate.cfg.SettingsFactory - Connection release mode: auto
2006-07-15 18:00:31,437 [main] INFO org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
2006-07-15 18:00:31,437 [main] INFO org.hibernate.cfg.SettingsFactory - Generate SQL with comments: enabled
2006-07-15 18:00:31,437 [main] INFO org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
2006-07-15 18:00:31,437 [main] INFO org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
2006-07-15 18:00:31,437 [main] INFO org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
2006-07-15 18:00:31,437 [main] INFO org.hibernate.cfg.SettingsFactory - Query language substitutions: {}
2006-07-15 18:00:31,437 [main] INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
2006-07-15 18:00:31,437 [main] INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled
2006-07-15 18:00:31,437 [main] INFO org.hibernate.cfg.SettingsFactory - Cache provider: org.hibernate.cache.EhCacheProvider
2006-07-15 18:00:31,453 [main] INFO org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
2006-07-15 18:00:31,453 [main] INFO org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
2006-07-15 18:00:31,468 [main] INFO org.hibernate.cfg.SettingsFactory - Echoing all SQL to stdout
2006-07-15 18:00:31,468 [main] INFO org.hibernate.cfg.SettingsFactory - Statistics: disabled
2006-07-15 18:00:31,468 [main] INFO org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
2006-07-15 18:00:31,468 [main] INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
2006-07-15 18:00:31,546 [main] INFO org.hibernate.impl.SessionFactoryImpl - building session factory
2006-07-15 18:00:31,546 [main] DEBUG net.sf.ehcache.CacheManager - Creating new CacheManager with default config
2006-07-15 18:00:31,546 [main] DEBUG net.sf.ehcache.CacheManager - Configuring ehcache from classpath.
2006-07-15 18:00:31,546 [main] WARN net.sf.ehcache.config.Configurator - No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/D:/Eclipse%20Workspace/JavaKITTY/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
2006-07-15 18:00:31,562 [main] DEBUG net.sf.ehcache.config.Configuration$DiskStore - Disk Store Path: C:\DOKUME~1\CHRIST~2\LOKALE~1\Temp\
2006-07-15 18:00:32,203 [main] INFO org.hibernate.impl.SessionFactoryObjectFactory - Not binding factory to JNDI, no JNDI name configured
2006-07-15 18:00:32,203 [main] INFO javakitty.init.InitSessionFactory - Session factory initiated.
>>> hibernate startup <<<
Perhaps it is possible to make the initialization faster, I don't know why it takes so long to initialize all mappings and configurations.
My next problem was my DAO classes, I generated all DAO classes with the hibernate-tools. I thought I could do all my selects, inserts, updates and deletes with the standard generated DAO classes. I implemented all my queries with criteria queries with the function findByExample (see below), but I think this is not a very good idea.
But would it be better to make a new function for every different query I have? Wouldn't it be a little bit unclear to have 50 functions in every dao class for all queries?
Code:
>>> hibernate generated dao class <<<
package javakitty.dao;
import javakitty.init.InitSessionFactory;
import javakitty.model.*;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import static org.hibernate.criterion.Example.create;
/**
* Home object for domain model class Admin.
* <br>
* A transaction like an insert, update or delete has to be encapsulated within an
* <tt>Session</tt>. There exists only one <tt>SessionFactory</tt> per thread.
* This is ensured by the InitSessionFactory class. A transaction is closed automatically,
* normally at the end of the current session.
* <br>
* An instance of an object could have several states over the full lifecycle of it:
* <i>transient:</i> never persistent, not associated with any <tt>Session</tt><br>
* <i>persistent:</i> associated with a unique <tt>Session</tt><br>
* <i>detached:</i> previously persistent, not associated with any <tt>Session</tt><br>
*
* @see javakitty.model.Admin
* @author Christoph Mayerhofer
*/
public class AdminDAO {
private static final Log log = LogFactory.getLog(AdminDAO.class);
private Transaction transaction = null;
private final SessionFactory sessionFactory = getSessionFactory();
/**
* Get the instance of the current <tt>SessionFactory</tt> and begin a
* transaction for the current session.
*
* @return The current session factory
*/
protected SessionFactory getSessionFactory() {
try {
SessionFactory sessionFactory = InitSessionFactory.getInstance();
return sessionFactory;
} catch (Exception e) {
log.error("Could not get session factory", e);
throw new IllegalStateException(
"Could not get session factory");
}
}
/**
* Begins a transaction for the current session. For every select, insert,
* update and delete we need an open transaction.
*/
public void beginTransaction() {
transaction = sessionFactory.getCurrentSession().beginTransaction();
log.debug("Begin transaction for AdminDAO");
}
/**
* Commits an open transaction of the current session. The transaction has
* to be commited to flush the data of the memory cache to the database.
*/
public void commitTransaction() {
if (transaction != null)
transaction.commit();
log.debug("Commiting transaction for AdminDAO");
}
/**
* Make a transient instance persistent.
*
* @param transientInstance a transient instance to be made persistent
* @throws HibernateException
*/
public void persist(Admin transientInstance) {
log.debug("persisting Admin instance");
try {
sessionFactory.getCurrentSession().persist(transientInstance);
log.debug("persist successful");
} catch (RuntimeException re) {
log.error("persist failed", re);
throw re;
}
}
/**
* Either save or update the given instance, depending upon the value of
* its identifier property. By default the instance is always saved. This behaviour may be
* adjusted by specifying an <tt>unsaved-value</tt> attribute of the identifier property
* mapping.
*
* @param instance a transient or detached instance containing new or updated state
* @throws HibernateException
*/
public void attachDirty(Admin instance) {
log.debug("attaching dirty Admin instance");
try {
sessionFactory.getCurrentSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
/**
* Reassociate a transient instance with the current session.
*
* @param instance the transient instance to be reassociated with the current session
* @throws HibernateException
*/
public void attachClean(Admin instance) {
log.debug("attaching clean Admin instance");
try {
sessionFactory.getCurrentSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
/**
* Remove a persistent instance from the datastore. The argument may be
* an instance associated with the receiving <tt>Session</tt> or a transient
* instance with an identifier associated with existing persistent state.
*
* @param persistentInstance the instance to be removed
* @throws HibernateException
*/
public void delete(Admin persistentInstance) {
log.debug("deleting Admin instance");
try {
sessionFactory.getCurrentSession().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
/**
* Copy the state of the given object onto the persistent object with the same
* identifier. If there is no persistent instance currently associated with
* the session, it will be loaded. Return the persistent instance.
*
* @param detachedInstance a detached instance with state to be copied
* @return an updated persistent instance
*/
public Admin merge(Admin detachedInstance) {
log.debug("merging Admin instance");
try {
Admin result = (Admin) sessionFactory.getCurrentSession().merge(
detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
/**
* Return the persistent instance of the given named entity with the given identifier,
* or null if there is no such persistent instance.
*
* @param id an identifier
* @return A persistent instance or null
* @throws HibernateException
*/
public Admin findById(int id) {
log.debug("getting Admin instance with id: " + id);
try {
Admin instance = (Admin) sessionFactory.getCurrentSession().get(
"javakitty.model.Admin", id);
if (instance == null) {
log.debug("get successful, no instance found");
} else {
log.debug("get successful, instance found");
}
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
/**
* Create a new criterion instance, for the given entity class and
* add a criterion restriction to constrain the results to be retrieved.
*
* @param instance a class, which is persistent, or has persistent subclasses
* @return The list of matched query results
*/
public List<Admin> findByExample(Admin instance) {
log.debug("finding Admin instance by example");
try {
List<Admin> results = (List<Admin>) sessionFactory
.getCurrentSession().createCriteria("javakitty.model.Admin").add(
create(instance).excludeZeroes()).list();
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
}
The next problem I had was lazy initialization. Yes, it's a nice feature, but I had problems with it. Perhaps I have to deal with it more, I don't know.
Perhaps some developers know what I can do to make the integration of hibernate in my next application better.
Regards,
Christopher
PS: Sorry for my bad english *gg*