These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Is this a bug or I have a mistake?
PostPosted: Wed Nov 16, 2005 8:29 am 
Newbie

Joined: Wed Nov 16, 2005 8:05 am
Posts: 2
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();



session.close();

if(result!=null){

//manually i reach the billing country///
/////////////////////////////////////////////////////////////
((WebUser) result.get(0)).getCountry().getStrName();
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


Top
 Profile  
 
 Post subject: Re: Is this a bug or I have a mistake?
PostPosted: Wed Nov 16, 2005 8:43 am 
Newbie

Joined: Wed Nov 16, 2005 8:05 am
Posts: 2
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.