-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate: Filtering related issue .
PostPosted: Wed Mar 03, 2010 3:44 pm 
Beginner
Beginner

Joined: Wed Mar 03, 2010 3:35 pm
Posts: 37
we are using a collection level filtering to fetch only those collection of entities (CodeValues) whose particular property called 'cdfMeaning' matches a specidfied value, while fetching their parent CodeSet entity.

@Entity
@Table(name = "CODE_SET")
@FilterDef(name="filterCodeValue", parameters = @ParamDef(name = "cdfMeaning", type = "string"))
public class CodeSet implements Serializable {
private static final long serialVersionUID = -6782112102089236956L;
private static final int CODE_SET_NAME = 256;

@Id
@Column(name = "CODE_SET", nullable = false, unique = true)
private Long codeSet;

@Column(name = "CODE_SET_NAME", nullable = false, length = CODE_SET_NAME, unique = true)
private String name;

@OneToMany (cascade={CascadeType.ALL}, mappedBy = "codeSet", fetch = FetchType.EAGER)
@Filter(name="filterCodeValue", condition=":cdfMeaning = VALUE_CDF_MEANING")
private List<CodeValue> codeValues = new ArrayList<CodeValue>();

the filter related annotation should make it clear. The problem we are facing is when trying to execute the below code for varies values of 'cdfMeaning ' ,

public CodeValue getUniqueCodeValue(String codeSetName, String cdfMeaning) {
CodeValue codeValue = null;
Session session = getSession();
session.enableFilter("filterCodeValue").setParameter("cdfMeaning", cdfMeaning);
Criteria criteria = session.createCriteria(CodeSet.class);
criteria.add(Restrictions.eq("name", codeSetName));
List<CodeSet> codeSets = criteria.list();
if (codeSets.isEmpty() || codeSets.get(0).getCodeValues().isEmpty())
return null;
else {
for (CodeSet codeSet: codeSets) {
codeValue = codeSet.getCodeValues().get(0);
}
}
return codeValue;
}


first time it filters the data properly, but for subsequent filetring it returns what it returned for the first retrieval, for example if the value for cdfMeaning was 'REALTIME' the first time, it returns CodeSet entity which only contains CodeValues containing that passed in value, but second time when cdfMeaning = 'BATCH', it still returns the same result as before. We are not really sure what w eare missing here thats causing this problem, any help on this would be great.


regards
Aravias


Top
 Profile  
 
 Post subject: Re: Hibernate: Filtering related issue .
PostPosted: Wed Mar 03, 2010 4:57 pm 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
Do you change your parameter value within the same session? If yes, the collection of your object is already initialized and won't be fetched again (using your new parameter).

Since you are using criteria anyway, why don't you just filter the collection using the criteria? You could go even further and optimize your query so that you fetch only the needed data (you return only the first entry of the whole collection, so why fetching so many objects).

_________________
-----------------
Need advanced help? http://www.viada.eu


Top
 Profile  
 
 Post subject: Re: Hibernate: Filtering related issue .
PostPosted: Wed Mar 03, 2010 6:23 pm 
Beginner
Beginner

Joined: Wed Mar 03, 2010 3:35 pm
Posts: 37
Actually in the line codeSet.getCodeValues().get(0);
the collection is already filtered when fetched and has only one CodeValue object . As you said, we were able to fix it by changing the tx advise attribute from the default to propagation="REQUIRES_NEW" for that method.


Top
 Profile  
 
 Post subject: Re: Hibernate: Filtering related issue .
PostPosted: Wed Mar 03, 2010 6:29 pm 
Beginner
Beginner

Joined: Wed Mar 03, 2010 3:35 pm
Posts: 37
One question I have is, when one service method which has tx setting as propagation="REQUIRES_NEW" invokes another service method with the same settings, does the second method use the same session or does it create a new one(assuming the default contextual settings are used)? If not what decides whether a new session is opened or the existing one is used in the invoked method, it would be great if someone can make this clear. thanks,


regards
Aravias


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.