1 Reply Latest reply on Feb 4, 2008 6:20 AM by mars1412

    Cannot instantitate @Embeddable attribute.

    infoherlinsys

      Dear Partners.

      The following xhtml piece references two attributes
      of an Entity-EJB. The first works fine. The second one is
      an @Embeddable attribute but it throws an exception:
      "Cannot instantiate MonetaryAmount"

      Something is wrong.

      Attached is the related code.

      If appreciate your advice.

      Thank you.

      Roger Mori.



      0)0)0)0)0)



      DOB

      <s:decorate>
      <h:inputText value="#{person.dateOfBirth}"/>
      </s:decorate>





      Annual Income

      <s:decorate>
      <h:inputText value="#{person.annualIncome.value}"/>
      </s:decorate>




      1)1)1)1)1) Action Bean

      [...]
      @Stateful
      @Name("personManager")
      public class PersonAction implements PersonManager {
      @In (required=false) @Out (required=false)
      private Person person;


      @PersistenceContext (type=EXTENDED)
      private EntityManager em;

      // @RequestParameter
      Long pid;

      public String sayHello () {
      em.persist (person);
      return "persons";
      }

      @DataModel
      private List fans;

      @DataModelSelection
      private Person selectedFan;

      @Factory("persons")
      public void findFans () {
      System.out.println("Find called");
      fans = em.createQuery("select p from Person p")
      .getResultList();
      }

      public void setPid (Long pid) {
      this.pid = pid;

      if (pid != null) {
      person = (Person) em.find(Person.class, pid);
      } else {
      person = new Person ();
      }
      }

      public Long getPid () {
      return pid;
      }

      public String delete () {
      Person toDelete = em.merge (selectedFan);
      em.remove( toDelete );
      findFans ();
      return null;
      }

      public String update () {
      return "persons";
      }

      @Remove @Destroy
      public void destroy() {}

      }

      2)2)2)2)2) Entity Bean
      [....]
      public class Person extends Party {
      private Date dateOfBirth ;
      [...]
      private MonetaryAmount annualIncome;
      [...]
      @Embedded
      @AttributeOverrides ( {
      @AttributeOverride(
      name = "value",
      column = @Column (name = "ANNUAL_INCOME")
      ),
      @AttributeOverride(
      name = "currency",
      column = @Column (name = "ANUAL_INCOME_CURRENCY", length = 16 )
      )
      }
      )
      public MonetaryAmount getAnnualIncome() {
      return annualIncome;
      }
      public void setAnnualIncome(MonetaryAmount annualIncome) {
      this.annualIncome = annualIncome;
      }

      [...]
      }

      2)2)2)2) Embeddable Attribute
      [...]
      @Embeddable
      public class MonetaryAmount implements Serializable {
      //Attributes
      private Currency currency;
      private BigDecimal value;

      //Constructor
      MonetaryAmount(){}
      public MonetaryAmount(BigDecimal value, Currency currency) {
      this.value = value;
      this.currency = currency;
      }

      public MonetaryAmount(BigDecimal value) {
      this.value = value;
      this.currency = Currency.getInstance(Locale.getDefault());
      }

      //Accessor Methods
      @Column (name = "CURRENCY", length = 16, nullable = false)
      public Currency getCurrency() {
      return currency;
      }
      public void setCurrency(Currency currency) {
      this.currency = currency;
      }

      @Column (name = "VALUE", nullable = false )
      public BigDecimal getValue() {
      return value;
      }

      public void setValue(BigDecimal value) {
      this.value = value;
      }


      // Common Methods
      public boolean equals(Object o) {
      if (this == o) return true;
      if (!(o instanceof MonetaryAmount)) return false;

      final MonetaryAmount monetaryAmount = (MonetaryAmount) o;

      if (!currency.equals(monetaryAmount.currency)) return false;
      if (!value.equals(monetaryAmount.value)) return false;

      return true;
      }

      [...]

      }