Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version:3.2
I am trying to use hibernate in an application to connect to multiple data sources. One db is DB2 that houses most of the basic employee info companywide, the other is an Oracle db that houses specific performance appraisal info for the application.
is it possible to connect to both data sources in the same App using hibernate. i.e is it possible to have 2 separate SessionFactories configged for 2 separate dbs.
The major problem that i am running into is when i try to use a custom config file like oracle.properties (or oracle.cfg.xml).
It seems that Hibernate doesnt like this. I have tried a hundred different variations of code, trying both properties files and xml files and nothing seems to work.
her is an example of something that i have been trying. If anyone could give me an idea or example of something that will work, it would be greatly appreciated.
Properties props = new Properties();
props.load(new FileInputStream("oracle.properties"));
Configuration config = new Configuration().setProperties(props);
config.addClass(Associate.class);
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
try{
Query query = session.getNamedQuery "com.ae.common.getAssociateById");
query.setString("employeeId", employeeId);
associates = query.list();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
}
and my oracle.properties file is
hibernate.dialect=org.hibernate.dialect.OracleDialect
hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
hibernate.connection.url=jdbc:oracle:thin:@localhost:1521:apprdev
hibernate.connection.username=user
hibernate.connection.password=password
and the Associate.hmb.xml file is
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping
PUBLIC "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping>
<class name="com.ae.common.Associate" table="ASSOCIATE">
<meta attribute="class-description">
Represents an associate in the appraisal system
</meta>
<id
name="id"
type="int"
column="EMPLOYEE_ID">
<meta attribute="scope-set">protected</meta>
<generator class="assigned"/>
</id>
<property
name="fname"
type="string"
not-null="true"
column="FNAME"
length="50" />
<property
name="lname"
type="string"
not-null="true"
column="LNAME"
length="50" />
</class>
<query name="com.ae.common.getAssociateById">
<![CDATA[
from com.ae.common.Associate as associate where associate.employeeId = :employeeId
]]>
</query>
</hibernate-mapping>
and the exception i get is
org.hibernate.MappingException: Named query not known: com.ae.common.getAssociateById
at org.hibernate.impl.AbstractSessionImpl.getNamedQuery(AbstractSessionImpl.java:70)
at org.hibernate.impl.SessionImpl.getNamedQuery(SessionImpl.java:1253)
at com.ae.db.dao.AssociateDAOImpl.getAssociate(AssociateDAOImpl.java:47)
at com.ManualTest.main(ManualTest.java:30)
java.lang.NullPointerException
at com.ManualTest.main(ManualTest.java:30)
AssociateDAOImpl.java #47 - Query query = session.getNamedQuery("com.ae.common.getAssociateById");
when I rename oracle.properties to hibernate.properties it work perfectly fine.
Thanks,
Aaron