muratti wrote:
Hi;
I have Users, Country and Timezone tables. User table have foreign keys to timezone and country tables. Users table has two foreign keys to Country table one of them is shiping country and another is billing country.
I have a method to take a user like this :
public static WebUser getWebUserWithKeys(Class c, Criterion... fields){
/* a Hibernate session */
Session session = null;
BasicConfigurator.configure();
/* we always need a transaction */
Transaction tx = null;
try {
/* get session of the current thread */
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
Criteria criteria = session.createCriteria(c);
for(int i=0;i<fields.length;i++){
criteria.add(fields[i]);
}
criteria.setFetchMode("country", FetchMode.JOIN);
criteria.setFetchMode("country1", FetchMode.JOIN);
criteria.setFetchMode("timezone", FetchMode.JOIN);
List result=criteria.list();
tx.commit();
session.close();
if(result!=null)
return (WebUser) result.get(0);
} catch (HibernateException e) {
if (tx != null)
try {
tx.rollback();
} catch (HibernateException e1) {
e1.printStackTrace();
}
} finally {
try {
if (session != null)
session.close();
} catch (HibernateException e1) {
e1.printStackTrace();
}
}
return null;
}
After calling this method i can take a user object and can fetch timezone,country1 but i can not fetch countr object (billing country).
But when i modified the method like this :
public static WebUser getWebUserWithKeys(Class c, Criterion... fields){
/* a Hibernate session */
Session session = null;
BasicConfigurator.configure();
/* we always need a transaction */
Transaction tx = null;
try {
/* get session of the current thread */
session = HibernateSessionFactory.currentSession();
tx = session.beginTransaction();
Criteria criteria = session.createCriteria(c);
for(int i=0;i<fields.length;i++){
criteria.add(fields[i]);
}
criteria.setFetchMode("country", FetchMode.JOIN);
criteria.setFetchMode("country1", FetchMode.JOIN);
criteria.setFetchMode("timezone", FetchMode.JOIN);
List result=criteria.list();
tx.commit();
//add this code////////////
//manually i reach the billing country///
/////////////////////////////////////////////////////////////
if(result!=null)
((WebUser) result.get(0)).getCountry().getStrName();
session.close();
if(result!=null){
return (WebUser) result.get(0);
}
} catch (HibernateException e) {
e.printStackTrace();
if (tx != null)
try {
tx.rollback();
} catch (HibernateException e1) {
e1.printStackTrace();
}
} finally {
try {
if (session != null)
session.close();
} catch (HibernateException e1) {
e1.printStackTrace();
}
}
return null;
}
I can take all referenced objects without any problem. I see that if you have 2 foreign keys to the same table u can take only one of them with using setFetchMode method.
Is this is a bug or known issue?
Best regards;
Murat