我试图在这里重用一些代码。假设我有一个基类:
@MappedSuperclass
@PersistenceUnit(name = "sample", unitName = "cools")
@IdClass(MyKey.class)
public class ParentEntity implements Serializable {
@Id
@Column(name = "my_id", nullable = false)
private String id;
@Id
@Column(name = "name")
private String name;
// setters and getters
}
然后我有这两个孩子:ChildAEntity
@Entity
@Table(name = "Table_A", schema = "COOL_SCHEMA")
@NamedQuery(name = "getEverything.a", query = "select a from ChildAEntity a "
+ "where a.id=:id"),
)
public class ChildAEntity extends ParentEntity implements Serializable {
}
和ChildBEntity:
@Entity
@Table(name = "Table_B", schema = "COOL_SCHEMA")
@NamedQuery(name = "getEverything.b", query = "select a from ChildBEntity a "
+ "where a.id=:id"),
)
public class ChildBEntity extends ParentEntity implements Serializable {
}
我的命名查询基本相同,除了它们指向的表。 (以及命名的查询名称,但这是因为它们必须是不同的。)
是否可以创建一个以实体为参数的命名查询?查看this post,可以在超类上创建命名查询。但是,我不确定如何在命名查询select语句中说“扩展我的类将用作实体”。