-->
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: Setting POJO Properties without using setters
PostPosted: Fri Apr 21, 2006 6:50 am 
Newbie

Joined: Fri Apr 21, 2006 6:33 am
Posts: 8
Hibernate version: 3.1.2

Hello Hibernate World !

How can I get or set any property of a POJO without use the getter and setters ? Hibernate knows every detail about a POJO, but I

don't know what hibernate class should I use in this case.


Consider the following class:

Code:
package events;

import java.util.*;

public class Person {

   private Long id;
   private int age;

   public Person() {}


   public Long getId() {
       return id;
   }

   public void setId(Long id) {
       this.id = id;
   }

   public int getAge() {
       return age;
   }

   public void setAge(int age) {
       this.age = age;
   }

}


* Consider that I have configured hibernate.cfg.xml and the Person.hbm.xml correctly !

My question is: How can I get or set any property of a POJO without use the getter and setters ?

I would like to use something like this:


Code:
  private void test() {

       Session session = HibernateUtil.getSessionFactory().getCurrentSession();
       session.beginTransaction();

       Person aPerson = (Person) session.load(Person.class, personId);

   int MyAge = 80;

       //  aPerson.setAge(MyAge);     // normal way
   aPerson.setAttribute("age", MyAge);  // I would like to implement something like this

       session.getTransaction().commit();
}


I read something in the API about the org.hibernate.property, but it could not help me.

Thanks id advance.

Best regards,
Joao Araujo


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 7:29 am 
Beginner
Beginner

Joined: Thu Apr 21, 2005 5:37 am
Posts: 45
Location: Switzerland
I think you have to supply a getter and a setter method for every attribute Hibernate has to persist.
But I think you can write in your getter and setter method whatever you want.

Why not make something like this:
Code:

public class Person{
public final static String AGE = "age";

protected HashMap attributes = new HashMap();

public void setAge(Integer years) {
       this.setAttribute(AGE, years);
}


public void setAttribute(String attribute, Object value) {
     attributes.put(attribute, value);
}
}


This way you have both ways to access your "fields". Getter methods should work analoguely.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 8:43 am 
Newbie

Joined: Fri Apr 21, 2006 6:33 am
Posts: 8
keule wrote:
I think you have to supply a getter and a setter method for every attribute Hibernate has to persist.
But I think you can write in your getter and setter method whatever you want.

Why not make something like this:
Code:

public class Person{
public final static String AGE = "age";

protected HashMap attributes = new HashMap();

public void setAge(Integer years) {
       this.setAttribute(AGE, years);
}


public void setAttribute(String attribute, Object value) {
     attributes.put(attribute, value);
}
}


This way you have both ways to access your "fields". Getter methods should work analoguely.


Thank you, Keule. You are right !!! This solution works, but I have the same value twice (one in HashMap and the other in the age attribute) and have to synchronize the HashMap with the POJO attributes. If Hibernate use direct access to the fields (access="field") the synchronization is lost.

I could solve the redudance problem writing a big If-else block in the setAttribute method, but it is not my objective.

Suppose that I have 80 POJOS, each one with 150 properties. I would prefer to use hibernate class and methods to get and set properties into the POJOS . I think that hibernate uses the java.lang.reflect package to invoke getters and setters methods, but "HOW" is the question . If I could also use the java.lang.reflect, but I don't know if hibernate will notice the changes.

I really need this solution, because I am at the beginning of a big project and I have to consider the best solution using best practices with minimum writng of code, with the best performance and best use of resources.


I would really appreciate other comments to this topic.

Thank you for your help !!!

Best regards,
Joao Araújo


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 10:31 am 
Beginner
Beginner

Joined: Thu Apr 21, 2005 5:37 am
Posts: 45
Location: Switzerland
Quote:
Thank you, Keule. You are right !!! This solution works, but I have the same value twice (one in HashMap and the other in the age attribute) and have to synchronize the HashMap with the POJO attributes. If Hibernate use direct access to the fields (access="field") the synchronization is lost.


Hmm, I thought you could make it without an actual attribute age. I think you would have to configure hibernate to access the field via the getters and setters, then you don't need an actual attribute/field, just store the value in the HashMap. But you would have to write the getters and setters.

So your are looking for a solution where you could do without the getters and setters?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Apr 21, 2006 12:06 pm 
Newbie

Joined: Fri Apr 21, 2006 6:33 am
Posts: 8
keule wrote:
So your are looking for a solution where you could do without the getters and setters?


Keule, I can understand better your solution now. Thanks !!! I will write a minimum code for the getters and setters of all POJOS and store the values only in the HashMap. There is no redundance of data in this solution.

Well, I will forget the problem with the option access="field" for now (page 58 of the reference manual of hibernate 3.1.2).

Thanks again.

best regards,
Joao Araújo


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 25, 2006 4:28 am 
Newbie

Joined: Fri Apr 21, 2006 6:33 am
Posts: 8
Hello again,

Finally I found what I was looking for. The following code uses hibernate classes to read and modify properties of a POJO:

Code:
 
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.EntityMode;


private void addEmailToPerson(Long personId, String emailAddress) {

        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        Person aPerson = (Person) session.load(Person.class, personId);

        ClassMetadata mMetaData = HibernateUtil.getSessionFactory().getClassMetadata(Person.class);

        // you can read all available properties
        String[] metaPropertyNames = mMetaData.getPropertyNames();
        // you have access to all the properties values
        Object[] propertyValues = mMetaData.getPropertyValues(aPerson, EntityMode.POJO);
       
        // you can set all the attributes using this command:
        mMetaData.setPropertyValue(aPerson, "age", new Integer (31), EntityMode.POJO);
        mMetaData.setPropertyValue(aPerson, "firstname", "Joca", EntityMode.POJO);
       
        // you can set all the attributes using this command:
        mMetaData.setPropertyValue(aPerson, "age", new Integer (31), EntityMode.POJO);
        mMetaData.setPropertyValue(aPerson, "firstname", "Joca", EntityMode.POJO);
       
        // you can get a property value for Hash variables
        Set addresses = (Set) mMetaData.getPropertyValue(aPerson, "emailAddresses", EntityMode.POJO);
        // and add a new object
        addresses.add(emailAddress);


        session.getTransaction().commit();
    }
   


Best regards,
Joao Araujo


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.