gpt4 book ai didi

java - Hibernate 表未映射但表存在

转载 作者:行者123 更新时间:2023-12-02 11:02:14 25 4
gpt4 key购买 nike

我有一个使用 Hibernate 的项目,但有一个错误表未映射。

org.jboss.resteasy.spi.UnhandledException: javax.ejb.EJBException: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: TrainingRoom is not mapped [SELECT DISTINCT t FROM TrainingRoom t ORDER BY t.id]
at org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:77)
at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:220)
at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:175)
...
Caused by: javax.ejb.EJBException: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: TrainingRoom is not mapped [SELECT DISTINCT t FROM TrainingRoom t ORDER BY t.id]
at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:187)
at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:277)
at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:327)
...
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: TrainingRoom is not mapped [SELECT DISTINCT t FROM TrainingRoom t ORDER BY t.id]
at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79)
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:218)
...

这是我的 TrainingRoom.java

@Entity
@Table(name = "ppp_trainingRoom")
public class TrainingRoom {

@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "id", updatable = false, nullable = false)
private Long id;

@Column
private String roomName;

//GET SET Method

}

TrainingRoomEndpoint.java

@Stateless
@Path("/trainingRoom")
public class TrainingRoomEndpoint {

@Inject
TrainingRoomService trainingRoomService;

@GET
@Path("/listAll")
@Produces("application/json")
public Response listAll(@Context HttpServletRequest request, @QueryParam("start") Integer startPosition, @QueryParam("max") Integer maxResult) {
List<TrainingRoom> trainingRoomList = trainingRoomService.listAll(startPosition, maxResult);
return Response.status(200).entity(trainingRoomList).build();
}
}

TrainingRoomService.java

public List<TrainingRoom> listAll(@QueryParam("start") Integer startPosition,
@QueryParam("max") Integer maxResult) {
logger.info(":: listAll ::");
TypedQuery<TrainingRoom> findAllQuery = em.createQuery(
"SELECT DISTINCT t FROM TrainingRoom t ORDER BY t.id", TrainingRoom.class);
if( startPosition != null ) {
findAllQuery.setFirstResult(startPosition);
}
if( maxResult != null ) {
findAllQuery.setMaxResults(maxResult);
}
final List<TrainingRoom> results = findAllQuery.getResultList();
return results;
}

在遇到此错误之前,我有 2 个 EntityManager,因为我有一些同义词表。但我发现其实没有必要。所以我删除了它,然后像上面的错误一样卡住了。

我该如何解决这个问题?

提前谢谢

最佳答案

您可以添加新属性:

 <property name="hibernate.archive.autodetection" value="class" />  

或者您可以在持久单元标记下为每个类指定类标记:

<class>org.cmh.itsetup.model.TrainingRoom</class>

关于java - Hibernate 表未映射但表存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51245000/

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