【发布时间】:2020-04-20 18:40:16
【问题描述】:
我正在开发一个 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
【问题讨论】:
-
也添加您的
Category课程。 -
添加连接信息
-
我认为 Table 'exampleDb.category' 不存在 是自我解释。
-
Spring boot 启动的第一件事就是验证表,以及jndi连接。模型由 start 绑定,所以它不是自我解释的。
标签: mysql hibernate spring-boot jpa