@Embeddable
@MappedSuperclass
public class Address {
private String street;
private String city;
public String getStreet(){ return street;}
public String getCity(){ return city;}
public String setStreet(Sting street){ this.street= street;}
public String setCity(String city){ this.city=city;}
}
import org.hibernate.annotations.Entity;
@Entity
public class AddressHistory extends Address {
Long id;
@Id @XmlTransient
@GeneratedValue(strategy = GenerationType.AUTO)
public Long getId() {
return m_id;
}
public void setId(Long m_id) {
this.m_id = m_id;
}
}
Here is my scenario, I want to store address of a person. And I also have to store all the address that a person has live before. So what I am doing is I am embedding current address in person table and would like to store all his/her pervious address in history table. To achieve this. I made Address class embeddable and embedded it into person table and HistoryAddress table. But as of OOPS concept. HistoryAddress is a Address, so it is not good to do composition of Address class into AddressHistory. Then I decided to extends AddressHistory from Address class. BUt when I do this hibernate gives me this error
Use of @OneToMany or @ManyToMany targeting an unmapped class AddressHistory.
But clearly I have mapped AddressHistory
Can we do this ? Can we extend Entity from Embedded class ?
No, entity cannot extend embeddable. In JPA 2.0 specification this is told as follows:
Also target of relationship must be Entity. Embeddable is not entity, it does not have identity of its own. If you want collection of embeddables, use @ElementCollection.