首页 > 解决方案 > 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

请给我建议。

标签: javamysqlspring-boothibernate

解决方案


也许,当我使用多模块应用程序时,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);
}

推荐阅读