Consider the following hierarchy, where entities WidgetA and WidgetB extend an abstract Widget superclass:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Widget implements Serializable {
@Column(name="serialNumber", length=64, nullable=false, unique=true)
private String serialNumber;
...
and
@Entity
public class WidgetA extends Widget implements Serializable {
...
and
@Entity
public class WidgetB extends Widget implements Serializable {
...
I need to search for Widgets by serialNumber, but I don’t know the concrete type of the Widget I’m searching for at runtime. What is the correct way to search for widgets by serialNumber such that if the serialNumber is that of a WidgetA, then an instance of WidgetA gets returned, and so on?
I am trying to use a findyBySerialNumber() in the Widget DAO, and I’m getting an error telling me I can’t instantiate an abstract class, which makes sense, but I thought the persistence provider would know how to look in the concrete child entity tables and return the correct instance. Can I make it do this?
I am using “Spring Data JPA”, so the Widget DAO is really simple:
public interface WidgetDAO extends JpaRepository<Widget, Long> {
public Widget findBySerialNumber(String serialNumber);
}
It seems you didn’t specify a discriminator explicitly for your widget hierarchy. I think you can try to define it explicitly because Spring Data will manipulate bytecode to generate the queries, and so I suspect SpringData need to have those values explicitely defined.
Additionally in subclasses you need to indicate the discriminator value for each subclass.
–
–