使用修改和查询注释时,我的表未映射?

问题描述 投票:0回答:1

我正在使用@Modifying和@Query注释直接执行我的sql语句,但是我收到一个错误,告诉我我的表没有映射,所以我不知道我做错了什么,这是我的代码:

@Repository
public interface TypesContratDaoJPA extends CrudRepository<Type, Long> {

    @Query("select type_id from declaration_type where declaration_id=:declaration")
    List<Integer> getListTypes(@Param("declaration") int declaration);

    @Modifying
    @Query("insert into declaration_type values(:declaration,:type)")
    void addTypeToContrat(@Param("declaration") int declaration, @Param("type") int type);

    @Modifying
    @Query("delete from declaration_type where declaration_id=:declaration and type_id=:type")
void deleteTypeFromContrat(@Param("declaration") int declaration, @Param("type") int type);

}

我收到此错误:

Caused by: java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: declaration_type is not mapped [delete from declaration_type where declaration_id=:declaration and type_id=:type]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:133)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:157)
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:164)
at org.hibernate.internal.AbstractSharedSessionContract.createQuery(AbstractSharedSessionContract.java:670)
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:23)

...
... 
... 
...

任何帮助将非常感激。

java spring-boot annotations mapping
1个回答
1
投票

看起来您想要执行本机查询而不是JPQL查询。

为了将查询标记为本机,您应该在nativeQuery = true注释中添加@Query作为属性。例如,您的第一个查询应如下所示:

@Query("select type_id from declaration_type where declaration_id=:declaration", nativeQuery = true)
    List<Integer> getListTypes(@Param("declaration") int declaration);

如果未在查询注释中添加nativeQuery = true,则该查询将被视为JPQL查询。 因此,为了使您的第一个查询起作用,您必须有一个名为declaration_type的类,该类使用@Entity进行注释,并且具有名为declaration_idtype_id的字段。您应该查看一些JPQL教程(或文档),以了解有关这些类型的查询的更多信息。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.