-->
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.  [ 6 posts ] 
Author Message
 Post subject: not persistent property
PostPosted: Sun Mar 06, 2011 7:04 am 
Beginner
Beginner

Joined: Sun Jan 30, 2011 8:14 am
Posts: 24
Hi guys,
I've a question, I'm a newbie.
I've a hibernate object, that implemens java.io.Serializable.
Object properties are name, surname, birthdate, that are all persistent and mapped with mapping file.
What I need is a fourth property, age, not mapped to db, that I want to get in my application to runtime calculate age, basing on birthdate that is mapped to db.


Code:
<hibernate-mapping>
   <class name="test.entity.User" table="USER" schema="ROOT">
        <id name="idUser" column="ID_USER" type="long">
            <generator class="identity" />
       
        </id>
        <property name="name" type="string">
            <column name="NAME" length="100" />
        </property>
        <property name="surname" type="string">
            <column name="SURNAME" length="100" />
        </property>
        <property name="birthdate" type="date">
            <column name="BIRTHDATE" length="10" />
        </property>
       
       
    </class>
</hibernate-mapping>



I need to add a not persisten property, age.
I tried in this way

Code:
<property name="age" type="string">
        </property>

without adding column...but it's always persisted to db...how can I proceed? I need a not persistent property in my hibernate object...is there a way? Please help me with some code...thanks


Top
 Profile  
 
 Post subject: Re: not persistent property
PostPosted: Sun Mar 06, 2011 7:43 pm 
Regular
Regular

Joined: Sun Aug 01, 2004 6:49 pm
Posts: 76
Why don't you simply omit the age property in your mapping file?
Sorry I am not so familiar with mapping files, I really advice to go with annotations based configuration. There you can use the @Transient annotation for a getter to mark a property invisible for Hibernate.

Thomas


Top
 Profile  
 
 Post subject: Re: not persistent property
PostPosted: Tue Mar 08, 2011 6:30 pm 
Beginner
Beginner

Joined: Sun Jan 30, 2011 8:14 am
Posts: 24
Thanks thhart,
now you're my only hope to get it working.
Following you I moved to use of annotation.
Here my code

Code:
public class User extends org.jfxtras.lang.XObject implements java.io.Serializable{
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "ID")
    private long    id;
    @Column(name = "NAME")
    private String name;
    @Column(name = SURNAME")
    private String surname;
    @Column(name = "BIRTH_DATE")
    private Date   birthdate;
    //it's required by xtableview
    @Transient
    private int age;
   

    public Paziente() {
    }
    getter & setter

public int getAge()
{
//return some logic basing on birth_date
}
}


The question is that I've to retrieve user with hibernate and bing them to xtableview (jfxtras). In the table one column is bound to age.
What I see is that my table shows correcly data except of age, that is always empty.
Using debug it seems like get and set method of age aren't never invoked.
What can I do ?
Maybe it due to transient?
I'm missing something?

Thanks,
Regards


Top
 Profile  
 
 Post subject: Re: not persistent property
PostPosted: Wed Mar 09, 2011 2:56 am 
Regular
Regular

Joined: Sun Aug 01, 2004 6:49 pm
Posts: 76
Please annotate the getter not the field and you can do there what you want, Hibernate will ignore it then. I don't know xtableview and I don't know if this is affected by annotations as well.

@Transient
public int getAge() {
return some logic basing on birth_date
}


Top
 Profile  
 
 Post subject: Re: not persistent property
PostPosted: Wed Mar 09, 2011 9:35 am 
Beginner
Beginner

Joined: Sun Jan 30, 2011 8:14 am
Posts: 24
Thanks thhart,
the problem is not caused bu xtableview.
I observed that with configuration you proposed me when I run

Code:
protected List findUsers(Class clazz) {
        List objects = null;
        try {
            startOperation();
            Query query = session.createQuery("from " + clazz.getName());
            objects = query.list();
            tx.commit();
        } catch (HibernateException e) {
            handleException(e);
        } finally {
            HibernateFactory.close(session);
        }
        return objects;
    }


I retrieve list of users, but age (that is not stored in the db) is not retrieved, it's null...like get method for this property was not runned, like done for other properties.
Is it possible to make not persistent this property but allowing its retrieving when I try to get list of users?
It seems like only persistent property values are retrieved.
My scope is to get this list (also with age property , not persistent but calculated) and binding it to table.

I hope you can help me,
Thanks


Top
 Profile  
 
 Post subject: Re: not persistent property
PostPosted: Wed Mar 09, 2011 10:47 am 
Expert
Expert

Joined: Thu Jan 08, 2009 6:16 am
Posts: 661
Location: Germany
what are you trying to do? if you annotate your fields you do not need an age-field in case it is calculated in the get-method.

If you want to do the calculation at retreivak-time you could use @Formula-annotation.

If you want to select properties from the db but not update them, use @Column(insert=false, update=false).

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


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