This content has been marked as final.
Show 5 replies
-
1. Re: entitiy ejb-design
jameswoodward Mar 29, 2006 3:54 AM (in response to udo.krass)
@Transient
public Integer getAge() {
// Calculate age based on date of birth.
return age;
} -
2. Re: entitiy ejb-design
udo.krass Mar 29, 2006 11:28 AM (in response to udo.krass)Hi,
thanx for that hint. I tried that before, but it will only help when i e.g.return new Integer(10);
My problem is related to the user. I store the user withsessionContext.set("currentUser", user);
and i tried to Outject the currentUser. In the first view the age is calculated properly, but when i reload the page, the birthdate is changed to the actual timestamp!!! I dont understand why.
My birthday-attribute in my User.java enitiy EJB@NotNull @Column(name="USER_BIRTHDATE") @Temporal(TemporalType.TIMESTAMP) @Type(type="calendar") public Calendar getBirthdate() { return birthdate; }
I am going crazy! Cannot understand that! All other attributes are returned properly, only the birthdate value is changed! Any hints for a bemused developer???
Thanx. -
3. Re: entitiy ejb-design
udo.krass Mar 29, 2006 12:17 PM (in response to udo.krass)Ok,
i need holiday.
I set the dateOfBirth to the actual time, so it was my fault...
dateOfBirth.add(Calendar.YEAR, age);
Now it works.
Thanx. -
4. Re: entitiy ejb-design
udo.krass Mar 29, 2006 1:00 PM (in response to udo.krass)Hi,
i doesn't work exactly. My Method:@Transient public Integer getAge() { // Create a calendar object with the date of birth Calendar dateOfBirth = getBirthdate(); // Create a calendar object with today's date Calendar today = Calendar.getInstance(); today.setTimeZone(TimeZone.getTimeZone("UTC")); // Get age based on year Integer age = new Integer(today.get(Calendar.YEAR) - dateOfBirth.get(Calendar.YEAR)); // Add the tentative age to the date of birth to get this year's birthday dateOfBirth.add(Calendar.YEAR, age); // If this year's birthday has not happened yet, subtract one from age if (today.before(dateOfBirth)) { age--; dateOfBirth.add(Calendar.YEAR, +1); } dateOfBirth.add(Calendar.YEAR, -age); return age; }
When i use this code in public static void main method it will work and show the real age. When i use this code in the entity EJB this method will not work. It fails on today.before(dateOfBirth). In the main method this is true in the ejb it is false. WHY??? ARGH!
Thanks for any advice! -
5. Re: entitiy ejb-design
jameswoodward Mar 30, 2006 3:36 AM (in response to udo.krass)The Java Calendar and Date classes are known to be buggy. Have a look at Joda-Time, http://joda-time.sourceforge.net/