java - org.hibernate.hql.internal.ast.QuerySyntaxException:公寓未映射[来自公寓]
问题描述
我有 Hibernate 和 MySQL 的 springboot rest 应用程序。我有这个错误:
嵌套异常是 org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.ast.QuerySyntaxException: Apartment is not mapped [from Apartment]
但我不知道我哪里错了。我有两张桌子:DB 中的 Apartments 和 Residents。但现在我只尝试 getAllApartments() 方法。我使用 Intellij,我什至在她身上检查了我的数据库。而且我的 Entity 类附近有一张小照片,那里有正确的数据源。而且我认为我检查了我的班级和字段的名称。
这是我的实体:
@Entity
@Table(name = "Apartments")
public class Apartment {
@Column(name = "apartment_id")
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer apartmentId;
@Column(name = "apartment_number"
private Integer apartmentNumber;
@Column(name = "apartment_class")
private String apartmentClass;
@OneToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.DETACH, CascadeType.REFRESH},
mappedBy = "apartment")
List<Resident> residentList;
我的 DAO 方法:
@Repository
public class ApartmentDAOImpl implements ApartmentDAO {
@Autowired
private EntityManager entityManager;
@Override
public List<Apartment> getAllApartment() {
Session session = entityManager.unwrap(Session.class);
Query query = session.createQuery("from Apartment");
List<Apartment> apartmentList = query.getResultList();
return apartmentList;
}
我的控制器:
@RestController
@RequestMapping("/api")
public class ApartmentController {
@Autowired
ApartmentService apartmentService;
@GetMapping("/apartments")
public List<Apartment> getAllApartments() {
List<Apartment> apartmentList = apartmentService.getAllApartment();
return apartmentList;
}
我也有没有任何逻辑的服务层。我的 property.file
spring.datasource.url=jdbc:mysql://localhost:3306/correct_db?useSSL=false&serverTimezone=UTC
spring.datasource.username=projuser
spring.datasource.password=projuser
请给我建议。
解决方案
也许,当我使用多模块应用程序时,Hibernate 或 Spring 没有看到我的实体。我清楚地表明了我的实体类
@EntityScan(basePackages = {"com.punko.entity"})
在我的 SpringBootApplication 类下:
@SpringBootApplication(scanBasePackages = "com.punko")
@EntityScan(basePackages = {"com.punko.entity"})
public class SpringBootApplicationConfig {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplicationConfig.class, args);
}
推荐阅读
- javascript - 在 VueJS 中使用计算属性搜索过滤器
- java - 无法确定类型:java.util.List,表:file_post,列:[org.hibernate.mapping.Column(file)]
- eclipse - 在 Eclipse 中设置单元测试 arduino 代码
- javascript - 使用 PHP 和 Ajax 不保存表单详细信息
- docker - 可以从 docker 映像中检索 Dockerfile 吗?
- c# - 尝试将覆盖添加到 DLL 中的表单
- python - 在 create_app 之前运行烧瓶
- c# - 是否有一个 C# 函数可以检查列表是否为空并且不将 null 计为元素?
- javascript - 使用 getDerivedStateFromProps 从父级响应更新子组件状态
- java - 使用 Spring Batch ItemWriter 写入文件时如何关闭分隔符