19

I am migrating some classes in a Hibernate hbm.xml file to JPA annotations.

We have an embeddable class Address that is used in several places. Each place uses a different subset of the properties in Address.

(getters/setters omitted for brevity)

@Embeddable
public class Address {
  String email;
  String address;
  String city; 
  String state;
  String zip;
  String country;
}

@Entity
@Table(name="customer")
public class Customer {
  @Embedded
  @AttributeOverrides({
    @AttributeOverride(name="address", column=@Column(name="ship_addr"),
    @AttributeOverride(name="city", column=@Column(name="ship_city"),
    @AttributeOverride(name="state", column=@Column(name="ship_state"),
    @AttributeOverride(name="zip", column=@Column(name="ship_zip"),
    @AttributeOverride(name="country", column=@Column(name="ship_country")
  })
  Address shippingAddress;

  @Embedded
  @AttributeOverrides({
    @AttributeOverride(name="address", column=@Column(name="bill_addr"),
    @AttributeOverride(name="city", column=@Column(name="bill_city"),
    @AttributeOverride(name="state", column=@Column(name="bill_state"),
    @AttributeOverride(name="zip", column=@Column(name="bill_zip")
  })
  Address billingAddress;
}

Note that in this contrived example, shippingAddress uses Address.country, but billingAddress does not; and neither of them use Address.email.

The problem is that Hibernate is inferring @Column tags for any column where I haven't explicitly provided one.

I tried adding @Transient to all the Address fields, but it appears that @AttributeOverride does not trump @Transient.

Is there any workaround for this?

2 Answers 2

8

My advice would be to create a new entity called PartialAddress/NationalAddress/BillingAddress. This would be used only for the JPA mapping, and not exposed on the Customer interface:

private PartialAddress billingAddress;

public Address getBillingAddress() {
    return billingAddress.toAddress();
}

Otherwise, I have come up with a somewhat ugly workaround for this problem, maybe it will work for you as well. Instead of mapping the field to a real column, I return a SQL null constant:

@Embedded
@AttributeOverrides({
    @AttributeOverride(name="address", column=@Column(name="bill_addr"),
    @AttributeOverride(name="city", column=@Column(name="bill_city"),
    @AttributeOverride(name="state", column=@Column(name="bill_state"),
    @AttributeOverride(name="zip", column=@Column(name="bill_zip"),
    @AttributeOverride(name="country", column=@Column(name="bill_id + null"),
})
Address billingAddress;

I'm using Oracle and EclipseLink, works on Hibernate 3.6 as well. And I've only tested it on read-only objects. Though, theoretically, setting the insertable and updatable attributes to false should be enough.

3
  • 3
    This is a very nice and innovative workaround . I directly map the ignored property to null (i.e column=@Column(name="null")
    – Ken Chan
    Nov 1, 2014 at 20:02
  • @KenChan 's workaround is acceptable for persisting data, but there is a problem when reading from the data base. It generetes an "javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet" and the message says: "o.h.e.j.s.SqlExceptionHelper - ERROR: column not found tabl0_.null" Dec 18, 2017 at 17:33
  • @KenChan simply entering NULL did not work for me on Oracle 12. However gfonte's original solution did. May 17, 2018 at 11:57
7

I don't think it is possible with annotations to "ignore" a field from address in your embedded objects.

A workaround is to create a base type Address without email and an ExtendedAddress (subclass of Address) with the field email.

3
  • The problem is that this is a common model class used all over our organization. I'm searching for a way to migrate this class to JPA without changing behavior, or forcing 30 different teams to adjust their code. Feb 20, 2012 at 17:41
  • Note also that this is an issue with JPA's switch to opt-out using &at;Transient, rather than opt-in using &at;Column. The idea was to make adopting JPA less invasive by not requiring &at;Column annotations everywhere--this is just a side effect that has bitten us and made it impossible to migrate this one class from legacy XML configuration to JPA annotations. Feb 20, 2012 at 17:43
  • If you can't change your model, than I don't see a solution. You will just have to ignore the properties of Address that aren't used, and thus leave them blank in the DB. Dirty solution, but the least invasive.
    – K.C.
    Feb 21, 2012 at 9:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.