我有一个Animal类,它由两个类扩展:
@XmlRootElement
@Entity
@Table(name="class_animal")
@Inheritance(strategy = InheritanceType.JOINED)
@DiscriminatorColumn(name="type", discriminatorType=DiscriminatorType.STRING,length=31)
@DiscriminatorValue("animal")
@Cacheable
@Cache(coordinationType=CacheCoordinationType.INVALIDATE_CHANGED_OBJECTS, alwaysRefresh=true, expiry=300)
public class Animal {
@Id @Column(name="uuid") private String id;
@Column(name="owner") private String owner;
@Column(name="name") private String name;
@Column(name="type") private String type;
}
Cat类扩展Animal
@Entity
@Table(name="class_animal_cat")
@Cacheable
@Cache(coordinationType=CacheCoordinationType.INVALIDATE_CHANGED_OBJECTS, alwaysRefresh=true, expiry=300)
@DiscriminatorValue("cat")
public class Cat extends Animal {
// ...
}
Mouse类,扩展Animal
@Entity
@Table(name="class_animal_mouse")
@Cacheable
@Cache(coordinationType=CacheCoordinationType.INVALIDATE_CHANGED_OBJECTS, alwaysRefresh=true, expiry=300)
@DiscriminatorValue("mouse")
public class Mouse extends Animal {
// ...
}
我使用以下(简化)语法接收所有动物:
ExpressionBuilder builder = new ExpressionBuilder(Animal.class);
ReadAllQuery databaseQuery = new ReadAllQuery(type, builder);
Expression exp = builder.getField("owner").equal("Thomas");
databaseQuery.setSelectionCriteria(exp);
Query query = ((JpaEntityManager)em.getDelegate()).createQuery(databaseQuery);
List<Animal> resultsList = query.getResultList();
只要数据库中没有任何项目,一切都很好,我得到一个空列表。
如果我添加“Cat”类型的项目,我会收到以下错误:
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.5.1.v20130918-f2b9fc5): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.microsoft.sqlserver.jdbc.SQLServerException: Invalid column name 'owner'.
Error Code: 207
Call: SELECT t0.uuid, t0.type, t0.name, t0.owner, t1.uuid FROM class_animal t0, class_animal_cat t1 WHERE ((t1.owner = ?) AND ((t1.uuid = t0.uuid) AND (t0.type = ?)))
bind => [2 parameters bound]
Query: ReadAllQuery(referenceClass=Animal sql="SELECT DISTINCT type FROM class_animal WHERE (owner = ?)")
t1.owner
的字段访问权限错误 - t0.owner
是正确的。如何配置/分配字段以始终回退到Animal.owner而不是i.E. Cat.owner?