Use getter/setter with HQL query?

hibernate, hql, java

Solution

You can change the access type to AccessType.PROPERTY

@Embeddable
public class Address {
    String street;
    String town;

    @Embedded
    @Access(AccessType.PROPERTY)
    ZipCode zipCode;

    public String getZipCode() {
        return zipCode().getZip();
    }

    public void setZipCode(String zip) {
        zipCode.setZip(zip);
    }
}

And then it would be possible to do

String FIND_BY_ZIP = "SELECT c FROM Customer c WHERE c.address.zipCode :=zip";

By default, the access type of properties is AccessType.FIELD if you put your mapping annotation in the field (e.g. you have `@Embedded` in your `zipCode`).

In order to to override this default behavior, either you can mark your property explicitly with `AccessType.PROPERTY`, or move your `@Embedded` mapping annotation to the `getZipCode()` method.

Problem

I'd like to use an `@Embedded` field directly using a wrapping getter/setter with a `HQL` sql statement. But it does not work: ``` org.hibernate.QueryException: could not resolve property: address.zip of: Customer [SELECT c FROM Customer c WHERE c.address.zip :=zip] ``` Is the following possible? ``` @Embeddable pulic class ZipCode { String country; String zip; } @Embeddable public class Address { String street; String town; @Embedded ZipCode zipCode; public String getZip() { return zipCode().getZip(); } public void setZip(String zip) { zipCode.setZip(zip); } } @Entity public class Customer { @Embedded Address address; } String FIND_BY_ZIP = "SELECT c FROM Customer c WHERE c.address.zip :=zip"; ```

Original source