I'm new to hibernate. I'm trying to store a person with one type of address (either domestic or international address). I understand my example below may seem a bad design for some people. Let's assume that's the requirement.
I'm able to store one type of address using @Embedded, but I had to state DomesticAddress or InternationalAddress, but not Address.
I read this post. I thought my pojo structure is not unreasonable. Is it possible to do it?
Person table
id - INT - AUTO-ID
email - VARCHAR
address1 - VARCHAR
address2 - VARCHAR
address3 - VARCHAR
city - VARCHAR
state - VARCHAR
zip - VARCHAR
country - VARCHAR
Person.java
@Entity
public class Person {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String email;
// this works, but does not work for international address!
@Embedded
private DomesticAddress address;
// this works, but does not work for international address!
@Embedded
@Target(DomesticAddress.class)
private Address address;
// this does not work; being ignored!
@Embedded
private Address address;
....
}
Note: address is being declared only once. I just wanna show it in 3 different variations that I tried.
Address.java
public interface Address {
....
}
DomesticAddress.java
@Embeddable
public class DomesticAddress implements Address {
private String address1;
private String address2;
private String city;
private String state;
private String zip;
....
}
InternationalAddress.java
@Embeddable
public class InternationalAddress implements Address {
private String address1;
private String address2;
private String address3;
private String country;
....
}