gpt4 book ai didi

hibernate - JPA 2.0/hibernate : No supertype found

转载 作者:行者123 更新时间:2023-12-02 23:08:10 26 4
gpt4 key购买 nike

当我尝试查询使用 Hibernate 映射的实体时,JSF Web 应用程序抛出异常(见下文)。我究竟做错了什么?或者这是 Hibernate 中的一个错误?我该如何解决这个问题?感谢您的帮助:)

以下是相关类:

基类ShipmentUnit有一些子类:

@Entity
@Table(name = "tat_shipment_unit")
@Inheritance(strategy = SINGLE_TABLE)
@DiscriminatorColumn(name = "unit_type", discriminatorType = CHAR, length = 1)
@DiscriminatorValue("_")
public abstract class ShipmentUnit implements Serializable {
private Long id;
private List<ShipmentAction> actions;

@Id @GeneratedValue
public Long getId() { return id; } // and corresponding setter

@OneToMany(mappedBy = "unit")
@OrderBy("listIndex")
public List<ShipmentAction> getActions() { return actions; } // and setter

// hashCode, equals etc.
}

ShipmentAction 类:

@Entity
@Table(name = "tat_shipment_action")
@IdClass(ShipmentActionID.class)
public class ShipmentAction implements Serializable {
private ShipmentUnit unit;
private int listIndex;

@Id @ManyToOne @NotNull
public ShipmentUnit getUnit() { return unit; } // and setter

@Id @Column(name = "list_index", nullable = false)
public int getListIndex() { return listIndex; } // and setter
}

ShipmentActionID类还有unitlistIndex getter 和 setter 方法上具有相同签名的属性。

现在,我想显示 h:dataTableLazyDataModel<ShipmentAction> 。数据模型的实现使用 Criteria API 来 (1) 计数和 (2) 查询这些发货操作实体,如下所示:

CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Number> query = cb.createQuery(Number.class);
Root<ShipmentAction> root = query.from(ShipmentAction.class);
Predicate predicates = ...
int total = em.createQuery(
query.select(cb.count(root).where(predicates)
).getSingleResult().intValue();

目前TypedQuery应使用 em.createQuery(...) 创建抛出异常:

[SEVERE] Error Rendering View[/tat/report/actions.xhtml]: java.lang.IllegalStateException: No supertype found
at org.hibernate.ejb.metamodel.AbstractIdentifiableType.requireSupertype(AbstractIdentifiableType.java:85)
at org.hibernate.ejb.metamodel.AbstractIdentifiableType.getIdType(AbstractIdentifiableType.java:173)
at org.hibernate.ejb.criteria.expression.function.AggregationFunction$COUNT.renderArguments(AggregationFunction.java:110)
at org.hibernate.ejb.criteria.expression.function.ParameterizedFunctionExpression.render(ParameterizedFunctionExpression.java:94)
at org.hibernate.ejb.criteria.expression.function.BasicFunctionExpression.renderProjection(BasicFunctionExpression.java:71)
at org.hibernate.ejb.criteria.QueryStructure.render(QueryStructure.java:250)
at org.hibernate.ejb.criteria.CriteriaQueryImpl.render(CriteriaQueryImpl.java:338)
at org.hibernate.ejb.criteria.CriteriaQueryCompiler.compile(CriteriaQueryCompiler.java:223)
at org.hibernate.ejb.AbstractEntityManagerImpl.createQuery(AbstractEntityManagerImpl.java:619)

我正在使用 Hibernate 版本 4.0.0.Final,在 JBoss AS 7 上运行。

        <dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
<artifactId>jboss-ejb-api_3.1_spec</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.spec.javax.annotation</groupId>
<artifactId>jboss-annotations-api_1.1_spec</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.0.0.Final</version>
<scope>provided</scope>
</dependency>

最佳答案

我找到了解决问题的方法。我更改了依赖类ShipmentAction的映射通过使用 @EmbeddedId而不是两个@Id注释。如果您感兴趣,这里是更新后的类(ShipmentUnit 类的映射未更改1)

@Entity
@Table(name = "tat_shipment_action")
public class ShipmentAction implements Serializable {
private @EmbeddedId Key id;
private @MapsId("unit_id") @ManyToOne ShipmentUnit unit;

public ShipmentAction() {
id = new Key();
}

public ShipmentUnit getUnit() { return unit; } // and setter

public int getListIndex() {
return id.list_index;
}

public void setListIndex(int index) {
id.list_index = index;
}

@Embeddable
public static class Key implements Serializable {
public long unit_id;
public int list_index;
// hashCode() and equals()
}
}

好吧,这似乎有效,我的 LazyDataModel<ShipmentAction>现在喜欢它:-)
但唯一奇怪的是,这只适用于字段上的 JPA 注释,Hibernate 无法再处理 getter 方法上的这些注释(至少在受我的更改影响的类上)。对此有什么想法吗?

1 另一个小变化:@javax.persistence.OrderBy注释位于ShipmentUnit.getActions()必须替换为 @org.hibernate.annotations.OrderBy无论出于何种原因。

关于hibernate - JPA 2.0/hibernate : No supertype found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9179626/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com