mysql - 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
解决方案
正如例外所说,数据库表不存在,您可以尝试在 application.properties 中插入一个条目,例如spring.jpa.hibernate.ddl-auto=update
.
也许,您的数据库没有该表,并且您的应用程序在启动时假定该表存在于数据库中。
而且,如果不是这种情况,那么请您分享服务器启动时间日志,这将有助于进一步调试此行为的原因。谢谢!
推荐阅读
- python-3.x - 如何在 Eclipse 首选项中将 python3.8 设置为 RobotFramework 的 python 路径?
- mysql - JOIN 两个表并得到结果
- unit-testing - 如何使用基于属性的测试生成有意义的示例来测试 n 元属性?
- ios - 此代码将永远不会执行警告 swift equatable enum
- arrays - jq通过在数组上选择空结果
- excel - 如何使用 VBA excel 在 Internet Explorer 文本框中输入数据
- javascript - 将 Redux 存储中的对象数组渲染为 React 组件
- java - 如何在 JTextField 中禁止特殊字符
- java - 根据映射到类的 yaml 中的条件查找“行”字段
- cassandra - scylla读取路径和cassandra读取路径有什么区别?