5 Replies Latest reply on Mar 30, 2006 3:36 AM by jameswoodward

    entitiy ejb-design

    Udo Krass Newbie

      Hi,

      i have an user with an attribute bithdate. I want to calculate the age, what is no problem. But i want to show the age with #{user.age}. The first time i call this the age-value is calculated properly, but when i reload my page it will show just a 0 value. I tried it to implement that with the Transient annotation, but with no success. What is the best way to implement a transient/calculated ejb attribute???

      Thanx in advance.

      Udo

        • 1. Re: entitiy ejb-design
          jameswoodward Newbie


          @Transient
          public Integer getAge() {
          // Calculate age based on date of birth.

          return age;
          }

          • 2. Re: entitiy ejb-design
            Udo Krass Newbie

            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 with
            sessionContext.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 Newbie

              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 Newbie

                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 Newbie

                  The Java Calendar and Date classes are known to be buggy. Have a look at Joda-Time, http://joda-time.sourceforge.net/