Hi all,
I tried configuring my application to have multiple database access by following the above suggestions, but, was unsuccessful.
I'm using a HibernateUtil class to configure each session. And am doing this at the same time:
Code:
private static final SessionFactory firstFactory, secondFactory;
private static Logger logger = Logger.getLogger(HibernateUtil.class.getName());
static {
try {
// Create the SessionFactory from hibernate.cfg.xml
firstFactory = new Configuration().configure("first.cfg.xml").buildSessionFactory();
secondFactory = new Configuration().configure("second.cfg.xml").buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
logger.error("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getFirstFactory() {
return firstFactory;
}
public static SessionFactory getSecondFactory() {
return secondFactory;
}
The static block finishes fine for the first one, but, not the second.
There are similar table names in each db and was wondering if this might be the problem. I've given each model class differing package names, but, still get the ExceptionInInitializerError.
Code:
Caused by: java.lang.ExceptionInInitializerError
at com.util.HibernateUtil.<clinit>(Unknown Source)
at com.delegate.hbm.HibAnnualExamDel.getAnnualExam(Unknown Source)
at com.action.AdminExamAction.showExamDetail(Unknown Source)
... 49 more
Caused by: java.lang.ClassCastException
at org.hibernate.tuple.PropertyFactory.buildVersionProperty(PropertyFactory.java:83)
at org.hibernate.tuple.EntityMetamodel.<init>(EntityMetamodel.java:157)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:412)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:108)
at org.hibernate.persister.PersisterFactory.createClassPersister(PersisterFactory.java:55)
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:215)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1176)
... 52 more
Also, I'm attempting to use the firstFactory first. By initializing them at the same time would I somehow be referencing the second one even though I explicitly call getFirstFactory?
Code:
Session session = HibernateUtil.getFirstFactory().getCurrentSession();
session.beginTransaction();
// doesn't look to get past this point
com.model.hbm.sp.AnnualExam annualExam = (com.model.hbm.sp.AnnualExam) session
.createQuery("select a from AnnualExam a where a.id = :aid")
.setParameter("aid", new Integer(annualExamId)).uniqueResult();
// find the questions
Query q = session.createQuery(
"from AnnualExamQuestion aeq where aeq.annualExam = :ae");
List questions = q.setParameter("ae", annualExam).list();
annualExam.setAnnualExamQuestions(Collections.synchronizedSet(new HashSet(questions)));
session.getTransaction().commit();
TIA - g