I use spring jpa+hibernate to connect to an Oracle DB with 2 tables: Customers and LegacyCustomers.
Customers extends LegacyCustomers by adding some additional columns.
@Entity
@Table(name="Customers")
public class Customers extends LegacyCustomers {
@Column(name="NewId") private String newId;
@Column(name="PhoneNumber") private String phoneNumber;
}
@Entity
@Table(name="LegacyCustomers")
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class LegacyCustomers {
@Column(name="OldId") private String oldId;
@Column(name="Name") private String name;
@Column(name="Address") private String address;
}
However they are completely different tables. I am looking for a way to express the relationship in java but avoid the polymorphism that hibernate creates when querying the superclass table (LegacyCustomers). How can I tell hibernate to use only columns from the superclass when I query for LegacyCustomers?
Unfortunately the @Polymorphism annotation suggested here doesnt help.
Thanks for the help
To achieve your goal, you need to use the @MappedSuperclass annotation instead to a new BaseCustomers class that encapsulates the common properties:
@MappedSuperclass
public class BaseCustomers {
@Column(name="OldId") private String oldId;
@Column(name="Name") private String name;
@Column(name="Address") private String address;
}
Afterward, the LegacyCustomers just extend the BaseCustomers and only adds the @Entity annotation since the BaseCustomers is not treated as an entity:
@Entity
@Table(name="LegacyCustomers")
public class LegacyCustomers extends BaseCustomers {
}
And the Customers entity does the same:
@Entity
@Table(name="Customers")
public class Customers extends BaseCustomers {
@Column(name="NewId") private String newId;
@Column(name="PhoneNumber") private String phoneNumber;
}
That's it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With