Spring/Hibernate: Entity is not mapped

I’m doing a Spring/Hibernate MVC CRUD project (which can be found here: https://github.com/Benjamin-Re/customer-tracker-demo/tree/main/web-customer-tracker1/src/base_package/de/entity). In the servlet I declare the base-package to be scanned. However it does not scan the sub-packages. I receive the error that my entity “is not mapped”.

org.hibernate.hql.internal.ast.QuerySyntaxException: Customer is not mapped [from Customer]

I specified only the entity package to be scanned and the error goes away. But then the other packages are not scanned for beans and so no mapping can be found.

I tried to list the sub packages like this:

<context:component-scan base-package="base_package.de.entity, base_package.de.controller, base_package.de.dao, base_package.de.service" />

But the Entity is not found. I get the same error.

I also tried to list them like this:

<context:component-scan base-package="'base_package.de.entity', 'base_package.de.controller', 'base_package.de.dao', 'base_package.de.service'" />

Here I don’t get an error but a warning: No mapping for GET /web-customer-tracker1/customer/list. So I guess it can’t find the controller.

I then tried to put all classes directly in one package. I then receive the error: Error creating bean with name ‘customerController’: Unsatisfied dependency expressed through field ‘customerService’ So here now it doesn’t find the customerService bean. As you can see each bean has the correct annotation @Entity, @Controller, @Repository, @Service. I checked the imports and they are org.springframework.stereotype.

What am I doing wrong here? Any input would be greatly appreciated.

Folder and package structure


<context:component-scan .../> has nothing to do with Entity scanning. You should leave it as you have it

<context:component-scan base-package="base_package.de" />

Going through your github repo the answer is in spring-mvc-crud-demo-servlet.xml and more specifically in the declaration of sessionFactory. There you have a property named packagesToScan and this property should be set to the package of your Entities and not to “controller” as it is right now.

<property name="packagesToScan" value="base_package.de.entity" />