-->
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.  [ 3 posts ] 
Author Message
 Post subject: Is PostLoad called after a find ?
PostPosted: Sun Aug 20, 2006 9:04 am 
Newbie

Joined: Sun Aug 06, 2006 8:54 am
Posts: 15
Hibernate version: Hibernate-3.2 RC2
Name and version of the database : MySQL 5.0.24

Hi. I've got a Customer entity with a date of birth (TemporalType.DATE) and I calculate his age in a PostLoad method (stored in a transient attribut). I've copied paste this code from Hibernate EntityManager doc (ยง5.3 - calculateAge() method). Here is the code (simplified with no getters/setters) :

Code:
@Entity
@Table(name = "t_customer")
public class Customer extends Person {

    private String telephone;
    private String email;
    @Column(name = "date_of_birth")
    @Temporal(TemporalType.DATE)
    private Date dateOfBirth;
    @Transient
    private Integer age;
    @OneToOne(fetch = FetchType.LAZY, optional = true, cascade = CascadeType.ALL)
    @JoinColumn(name = "address_fk")
    private Address homeAddress;

    @PostLoad
    public void calculateAge() {
        if (dateOfBirth == null) {
            age = -1;
            return;
        }

        Calendar birth = new GregorianCalendar();
        birth.setTime(dateOfBirth);
        Calendar now = new GregorianCalendar();
        now.setTime(new Date());
        int adjust = 0;
        if (now.get(Calendar.DAY_OF_YEAR) - birth.get(Calendar.DAY_OF_YEAR) < 0) {
            adjust = -1;
        }
        age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR) + adjust;
    }


I wrote a test class that sets a birth date and makes sure that the age is set. This class creates a customer, finds it and checks that the age is not null. This fails except if you refresh the customer after the find :

Code:
        // previously set some default values to customer
        // persist the customer entity
        tx.begin();
        em.persist(customer);
        tx.commit();
        Long id = customer.getId();

        // Finds the created customer
        tx.begin();
        customer = em.find(Customer.class, id);
        // without the call to the refresh method, age is null
        em.refresh(customer);
        tx.commit();


For me PostLoad should be called after the find. The spec doesn't say much and the following sentence is ambiguous I find. In one hand the method is called "after the entity has been loaded into the current persistence context" but on the other hand PostLoad is "invoked before a query result is returned" (does it mean before the find?) :

The PostLoad method for an entity is invoked after the entity has been loaded into the current persistence context from the database or after the refresh operation has been applied to it. The PostLoad method is invoked before a query result is returned or accessed or before an association is traversed.


So, is it normal to call refresh to have the PostLoad working or should it be called after the find ?

Thanks,

Antonio


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 24, 2006 8:27 pm 
Newbie

Joined: Wed Aug 23, 2006 10:11 am
Posts: 9
This is just a guess, but I think that the entity is not getting loaded during the find, since it is already being managed by the entity manager following the persist. You could verify this by putting em.clear(), in between the two, and see if that causes the PostLoad to be called as a result of the find.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 25, 2006 4:57 am 
Newbie

Joined: Sun Aug 06, 2006 8:54 am
Posts: 15
Your guess was right. Clearing the entity manager causes the PostLoad to be called.

Thanks.


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