-->
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.  [ 9 posts ] 
Author Message
 Post subject: Indexing of a non database property variable in a class
PostPosted: Fri Apr 16, 2010 6:48 pm 
Newbie

Joined: Fri Mar 19, 2010 7:00 pm
Posts: 14
OK so I have a variable in my class that contains several other variables that have an @Field and that are mapped to a database field value. I also have another variable that I want to set either true or false depending on what value a database field contains.

What I am trying to do is to make this un-mapped variable (doesn't correspond to a database field) to be indexed and saved along with the other indices. Is this possible?

Here is how I'm declaring my variable:

Code:
// un-mapped variable NOT INDEXED:
@Field(index = Index.UN_TOKENIZED, store = Store.YES)
@Transient
protected boolean ageSetField;

...

// mapped variable INDEXED:
@Field(index = Index.UN_TOKENIZED, store = Store.YES)
@Column(name = "age")
protected int age;


But it's not being added to the index. The way its getting set is in this method:

Code:
public void setAge(int age) {
        if (age  <= 0) {
            this.age = null;
            this.ageSetField = false;
        } else {
            this.age = age;
            this.ageSetField = true;
        }
    }


Any thoughts? I also tried making a setAgeSetField() that I would call from setAge() but it still didn't work.

Thank you!


Last edited by Mocchi on Wed Apr 21, 2010 1:45 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Indexing of a non database property variable in a class
PostPosted: Sat Apr 17, 2010 6:24 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
could you try it on a getter method?
I know it worked as I've used the same strategy in the past, but I never annotate fields.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Indexing of a non database property variable in a class
PostPosted: Mon Apr 19, 2010 1:57 pm 
Newbie

Joined: Fri Mar 19, 2010 7:00 pm
Posts: 14
Hi thanks for the reply. I have both a getter and setter methods. I know the index is not being created because I am using Luke to check the indices. And the 'ageSetField' is the only one that is missing.

I am using annotations and not the xml...

Any thought are greatly appreciated!


Top
 Profile  
 
 Post subject: Re: Indexing of a non database property variable in a class
PostPosted: Mon Apr 19, 2010 2:51 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Ok then try moving the annotations from the fields to the getters

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Indexing of a non database property variable in a class
PostPosted: Wed Apr 21, 2010 1:45 pm 
Newbie

Joined: Fri Mar 19, 2010 7:00 pm
Posts: 14
Hi sorry for delayed reply, I have been out of town. OK I'm still trying to figure this out. I put the annotation by the method like you suggest but it still wont index. Here is what the replaced annotations now look like:

Code:
@Transient
protected boolean ageSetField;

...

@Field(index = Index.UN_TOKENIZED, store = Store.YES)
public Boolean getAgeSetField(int age) {
       return ageSetField;
}

public void setAgeSetField(Boolean ageSetField) {
       this.ageSetField = ageSetField;
}


If I move the @Transient from the field to the getter method I get exception thrown out.


Top
 Profile  
 
 Post subject: Re: Indexing of a non database property variable in a class
PostPosted: Thu Apr 22, 2010 3:39 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
If I move the @Transient from the field to the getter method I get exception thrown out.

you didn't post the exception, but a polite guess makes me thing that you have all annotations on fields. you can't jsut move one, you have to move all annotations to getters as one rule must be chosen consistently.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Indexing of a non database property variable in a class
PostPosted: Thu Apr 22, 2010 4:13 pm 
Newbie

Joined: Fri Mar 19, 2010 7:00 pm
Posts: 14
OK so I figured out the problem...the problem is in the method:

Code:
@Field(index = Index.UN_TOKENIZED, store = Store.YES)
public Boolean getAgeSetField(int age) {
       return ageSetField;
}


It's not returning anything and so I'm guessing that's why its not being created. Its not being set even though I havethis:

Code:
public void setAge(int age) {
        if (age  <= 0) {
            this.age = null;
            this.ageSetField = false;
        } else {
            this.age = age;
            this.ageSetField = true;
        }
    }


So what I had to do was to give the ageSetField a value directly in the method like so:

Code:
@Field(index = Index.UN_TOKENIZED, store = Store.YES)
public Boolean getAgeSetField(int age) {

       if (age  <= 0) {
           return false;
        } else {
            return true;
        }

}


And after I do that THEN the index is created for ageSetField. Any idea why that is? why isn't it set in the initials setAge() method??

Thanks!


Top
 Profile  
 
 Post subject: Re: Indexing of a non database property variable in a class
PostPosted: Thu Apr 22, 2010 6:50 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
Any idea why that is? why isn't it set in the initials setAge() method??

that's your code :) Are you sure you're calling setAge() ?

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Indexing of a non database property variable in a class
PostPosted: Thu Apr 22, 2010 8:58 pm 
Newbie

Joined: Fri Mar 19, 2010 7:00 pm
Posts: 14
This is where I'm ignorant with the workings of the indexing and such. The age IS set and saved and indexed for Age (which is a DB mapped field). That's where I was hoping you could shed some light. As far as I know a getter and setter method is required for each DB mapped class variable/property that has the @Field and @Column annotation. So again, age is a property in my class with its own @Field and @Column and it is mapped to a DB field in table. Age property is set correctly and indexed. However when I try to also set the non DB mapped variable that only has @Field but no @Column (ageSetField) then that is not set at all even though I'm setting it in the setter method for 'age' property which gets set just right!

So what I'm saying is that when I index with the .index() method I was under the impression that setter methods are called somewhere automatically....


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