I am trying to work out if it is possible get JPA to persist an abstract collection with concrete implementations.
So far my code looks like this:
@Entity
public class Report extends Model {
@OneToMany(mappedBy = "report",fetch=FetchType.EAGER)
public Set<Item> items;
}
@MappedSuperclass
public abstract class OpsItem extends Model {
@ManyToOne
public RetailOpsBranch report;
}
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class AItem extends OpsItem {
...
}
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class BItem extends OpsItem {
...
}
But I keep stumbling on the mapping error below and I don't really know if this is feasible?
JPA error
A JPA error occurred (Unable to build EntityManagerFactory): Use of @OneToMany or
@ManyToMany targeting an unmapped class: models.Report.items[models.OpsItem]
UPDATE
I don't think the problem is with the abstract class but the the @MappedSuperClass annotation. It looks like jpa does not like to map the one to many relationship with a @MappedSuperClass. If I change the abstract class into a concrete class I have the same error.
If I then change to @Entity annotation this seem to work with both the abstract and concrete class.
This seems a bit strange to map an abstract class with @Entity. I'm I missing anything?
SOLUTION
Managed to figure it out with the help from rhinds. Two points to note:
1) the abstract class needs to be annotated with @Entity and inheritance strategy of table per class for the subclasses to have their own table.
2) Identity Id generation will not work in this scenario and I had to use Table generation type.
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class OpsItem extends GenericModel {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
public Long id;
public String branchCode;
@ManyToOne
public Report report;
}
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public class AItem extends OpsItem {
...
}