Question regarding Hibernate Polymorphism and extending a parent class (which I can not modify directly). My parent class is called Contact:
@Entity
@Table(name="contact")
@Inheritance(strategy=InheritanceType.JOINED)
public class Contact {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
public long id;
public String name;
}
And the child class is called ContactLocation which associates a Location to a Contact:
@Entity
@Table(name="contact_location")
@Inheritance(strategy=InheritanceType.JOINED)
public class ContactLocation extends Contact {
@ManyToOne(targetEntity=Location.class, cascade=CascadeType.ALL)
Location location;
}
The resulting database table structure appears to be correct:
contact:
id:long
name:varchar
contact_location:
contact_id:long
location_id:long
Here's my save method which I need to either update an existing ContactLocation, or save a new ContactLocation for an existing Contact:
public void saveContact(Object dialog) {
Contact contact = ui.getAttachedObject(dialog, Contact.class);
ContactLocation contactLocation = null;
if (contact instanceof ContactLocation) {
LOG.debug("Casting Contact to ContactLocation");
contactLocation = (ContactLocation)contact;
//TODO Update existing ContactLocation
//UPDATE contact_location SET location_id = 33 WHERE contact_id = 22;
}
else {
LOG.debug("Contact NOT instanceof ContactLocation");
//TODO Save new ContactLocation from existing Contact
//INSERT INTO contact_location (contact_id, location_id) VALUES (22,33);
}
}
How do I create a row in *contact_location* table which maps Contact to a Location using my ContactLocationDao?