首页 > 解决方案 > JPQL 未在服务器上运行

问题描述

我正在研究一个 MySQL 数据库。我正在使用 Spring Boot JPA 并使用 JPQL 创建查询。

@Query(value="SELECT DISTINCT c FROM Category c WHERE c.distributorId " +
            "IN (SELECT DISTINCT d.distributorId FROM Distributor d WHERE d.partnerId=?1)")
    List<Category> findDistinctByPartnerId(long partnerId);

它工作得很好而且很漂亮,除了在服务器中(当然)。问题是,它启动了服务器(意思是,实体类正确加载),但查询响应以下错误:

ERROR [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (default task-1) Table 'exampleDb.category' doesn't exist

我从未将数据库名称添加到实体中,这甚至没有显示在调试 sql 中:

Hibernate: select distinct category0_.CategoryId as Category1_5_, category0_.CatalogCode as CatalogC2_5_, category0_.CategoryName as Category3_5_, category0_.DistributorId as Distribu4_5_, category0_.ParentId as ParentId5_5_ from category categor
y0_ where category0_.DistributorId in (select distinct distributo1_.DistributorID from distributor distributo1_ where distributo1_.PartnerId=?)

我一直在寻找对此的回应,但无济于事。提前感谢您的所有帮助!

类别类别:

@Entity
@Builder(toBuilder = true)
@AllArgsConstructor(access = AccessLevel.PACKAGE)
@NoArgsConstructor(access = AccessLevel.PACKAGE)
@Setter(value = AccessLevel.PUBLIC)
@Getter
@Table(name="category")
public class Category implements Serializable {
    @Id
    @Column(name="CategoryId")
    @GeneratedValue(generator="system-uuid")
    @GenericGenerator(name="system-uuid", strategy = "uuid")
    String categoryId;
    @Column(name="CategoryName")
    String categoryName;
    @Column(name="CatalogCode")
    String catalogCode;
    @ManyToOne
    @LazyCollection(LazyCollectionOption.FALSE)
    @JoinColumn(name = "ParentId", referencedColumnName = "CategoryId")
    @OnDelete(action = OnDeleteAction.CASCADE)
    Category parent;
//    @OneToOne
//    @LazyCollection(LazyCollectionOption.FALSE)
//    @JoinColumn(name = "DistributorId", referencedColumnName = "DistributorId")
//    Distributor distributor;
    @Column(name="DistributorId")
    Long distributorId;

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Category category = (Category) o;
        return categoryId.equals(category.categoryId) &&
                Objects.equals(categoryName, category.categoryName) &&
                Objects.equals(parent, category.parent) &&
                distributorId.equals(category.distributorId);
    }

    @Override
    public int hashCode() {
        return Objects.hash(categoryId, categoryName, distributorId);
    }
}

通过 JNDI 连接:

spring.datasource.jndi-name=java:/jdbc/mapper
spring.datasource.hikari.pool-name=mapper

标签: mysqlhibernatespring-bootjpa

解决方案


正如例外所说,数据库表不存在,您可以尝试在 application.properties 中插入一个条目,例如spring.jpa.hibernate.ddl-auto=update.

也许,您的数据库没有该表,并且您的应用程序在启动时假定该表存在于数据库中。

而且,如果不是这种情况,那么请您分享服务器启动时间日志,这将有助于进一步调试此行为的原因。谢谢!


推荐阅读