0 Replies Latest reply on Jan 28, 2008 1:34 PM by infoherlinsys

    Cannot instantitate @Embeddable attribute

    infoherlinsys

      Dear Partners.

      The following xhtml piece references two attributes of an Entity-EJB.
      The first one 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.

      You advice is appreciated.

      Thank you.

      Roger Mori.



      0)0)0)0)0) XHTML page



      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
      package com.dpn.model.common;
      import java.io.Serializable;
      import java.text.NumberFormat;
      import java.util.*;
      import javax.persistence.*;
      @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;
      }

      public int hashCode() {
      int result;
      result = value.hashCode();
      result = 29 * result + currency.hashCode();
      return result;
      }

      public String toString() {
      return "Value: '" + getValue() + "', " +
      "Currency: '" + getCurrency() + "'";
      }

      public int compareTo(Object o) {
      if (o instanceof MonetaryAmount) {
      // TODO: This requires some conversion magic and is therefore not implemented
      return this.getValue().compareTo(((MonetaryAmount) o).getValue());
      }
      return 0;
      }

      }