You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by David Beer <da...@gmail.com> on 2012/05/23 15:57:03 UTC

Problem creating CriteriaQuery to retrieve an @Embedded Object

Hi All

I have been trying to create a criteria query to select an @Embedded object
without much luck. I originally had a JPQL query but on switching from
EclipseLink to OpenJPA I had a warning that JPQL cross queries are not
allowed anymore and to use CriteriaQuery.

I have a very simple @Embedded Object Called Address this has two variables
at the moment. Customer contains the embedde object as field address. See
code below. I am trying to construct a query to retrieve the Address Object
as a whole.

@Embeddable
public class Address implements Serializable {

    @Column(name="HouseNameOrNumber")
    private String houseNameNumber;
    private String postcode;

    public Address() {
    }

    public Address(String houseNameNumber, String postcode) {
        this.houseNameNumber = houseNameNumber;
        this.postcode = postcode;
    }

    public String getHouseNameNumber() {
        return houseNameNumber;
    }

    public void setHouseNameNumber(String houseNameNumber) {
        this.houseNameNumber = houseNameNumber;
    }

    public String getPostcode() {
        return postcode;
    }

    public void setPostcode(String postcode) {
        this.postcode = postcode;
    }

    @Override
    public String toString() {
        return "Address{" + "houseNameNumber=" + houseNameNumber + ",
postcode=" + postcode + '}';
    }

}

@Entity
public class Customer implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String firstName;
    private String lastName;
    @Embedded
    private Address address;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Address getAddress() {
        return address;
    }

    public void setAddress(Address address) {
        this.address = address;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields
are not set
        if (!(object instanceof Customer)) {
            return false;
        }
        Customer other = (Customer) object;
        if ((this.id == null && other.id != null) || (this.id != null &&
!this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.copperarrow.demo.jpa2criteriaquery.model.Customer[ id="
+ id + " ]";
    }

}

Any help would be greatly appreciated. I can construct basic queries on the
customer class and queries using the WHERE clasue. but can't seem to get
back the Address Object.

Thanks

David