Reputation:
Lets suppose, we have three classes: A,B,C. B extends A, C extends A. Is it possible (in principle,for example dynamically) to set different column names for:
I mean 1 and 2 at the same time. I use eclipselink.
Upvotes: 3
Views: 2947
Reputation: 1567
Yes it is possible and can be done in following way
@Embeddable public class Address {
protected String street;
protected String city;
protected String state;
@Embedded protected Zipcode zipcode;
}
@Embeddable public class Zipcode {
protected String zip;
protected String plusFour;
}
@Entity public class Customer {
@Id protected Integer id;
protected String name;
@AttributeOverrides({
@AttributeOverride(name="state",
column=@Column(name="ADDR_STATE")),
@AttributeOverride(name="zipcode.zip",
column=@Column(name="ADDR_ZIP"))
})
@Embedded protected Address address;
...
}
It can be applied to an entity that extends a mapped superclass or to an embedded field or property to override a basic mapping or id mapping defined by the mapped superclass or embeddable class (or embeddable class of one of its attributes). If AttributeOverride is not specified, the column is mapped the same as in the original mapping.
In below example it applied to entity
@MappedSuperclass
public class Employee {
@Id protected Integer id;
@Version protected Integer version;
protected String address;
public Integer getId() { ... }
public void setId(Integer id) { ... }
public String getAddress() { ... }
public void setAddress(String address) { ... }
}
@Entity
@AttributeOverride(name="address", column=@Column(name="ADDR"))
public class PartTimeEmployee extends Employee {
// address field mapping overridden to ADDR
protected Float wage();
public Float getHourlyWage() { ... }
public void setHourlyWage(Float wage) { ... }
}
Upvotes: 8