I have springboot rest appplication with Hibernate and MySQL. I have this error:
nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: org.hibernate.hql.internal.ast.QuerySyntaxException: Apartment is not mapped [from Apartment]
But I dont know where I have a mistake. I have two tables: Apartments and Residents in DB. But now I try only getAllApartments() method. I use Intellij and I even checked my DB in her. And I have little picture near my Entity class, where I have correct data source. And I think that I checked names my class and fields.
This is my 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;
My DAO method:
@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; }
My Controller:
@RestController @RequestMapping("/api") public class ApartmentController { @Autowired ApartmentService apartmentService; @GetMapping("/apartments") public List<Apartment> getAllApartments() { List<Apartment> apartmentList = apartmentService.getAllApartment(); return apartmentList; }
I also have service layer without any logic. My property.file
spring.datasource.url=jdbc:mysql://localhost:3306/correct_db?useSSL=false&serverTimezone=UTC spring.datasource.username=projuser spring.datasource.password=projuser
Give me advice, please.
Advertisement
Answer
Maybe, As I used multi module application, Hibernate or Spring didn’t see my Entity.
And I clearly indicated my Entity class with @EntityScan(basePackages = {"com.punko.entity"})
under my SpringBootApplication class:
@SpringBootApplication(scanBasePackages = "com.punko") @EntityScan(basePackages = {"com.punko.entity"}) public class SpringBootApplicationConfig { public static void main(String[] args) { SpringApplication.run(SpringBootApplicationConfig.class, args); }