开发者

Hibernate, Associations not working

开发者 https://www.devze.com 2023-03-26 08:09 出处:网络
I have a model like this Seizurs => MatCountry => MatCountryI18n When I try to query the data like so criteria.

I have a model like this

Seizurs => MatCountry => MatCountryI18n

When I try to query the data like so

criteria.
createAlias("matCountry","seizure_country",CriteriaSpecification.INNER_JOIN).       
createAlias("seizure_country.matCountryI18ns","seizure_country_translation",CriteriaSpecification.INNER_JOIN).
add(Restrictions.eq("seizure_country_translation.matLanguageCode", "de").
setFetchMode("seizure_country", FetchMode.JOIN).
setFetchMode("seizure_country_translation", FetchMode.JOIN);

and get the data from the DB

开发者_运维问答
List<Seizure> seizures = getHibernateTemplate().findByCriteria(criteria);

I would expect that hibernate gives me only those objects (specifically those child objects) that match my given query (where translation.matLanguageCode = de)

But when I access the child objects for MatCountryI18n

logger.info("Seizures: "+seizures.get(0).getMatCountry().getMatCountryI18ns().size());

It gives me back ALL the MatCountryI18n objects that are associated with MatCountry

Please help.

What am I'm doing wrong.

Regards JS


You are asking hibernate to give you all Seizurs that have MatCountry's that have one or more MatCountryI18n's with matLanguageCode == 'de'. Which is exactly what you are getting.

It sounds like you want to only select only the specific MatCountryI18n's with matLanguageCode == 'de'. I would do this by staring your select with a MatCountryI18n's Criteria and fetching the Seizurs though your associations in reverse order as you are doing right now. Either programatically or though a ResultTransformer.

0

精彩评论

暂无评论...
验证码 换一张
取 消