L
L
lovk4ch2019-12-07 02:56:23
Java
lovk4ch, 2019-12-07 02:56:23

Spring Data JPA: Not a managed type Exception - solutions?

I tried all the tips, I'm creating a project from scratch for the third time in a row. For the second day, an error occurs during assembly, the compiler rested on its horn.
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class spring.model.entity.User
My classes are:

@Entity
@Table(name = "User")
public class User {
    private int id;
    private String name;
    private String password;
    ...
}

@Repository
public interface UserRepository extends CrudRepository<User, Long> { }

@SpringBootApplication
@ImportResource("classpath:applicationContext.xml")
@EntityScan(basePackages = "spring.model.entity")
@EnableJpaRepositories(basePackages = "spring.model.repositories")
public class Application {
    ....
        SpringApplication.run(Application.class);
    ....
}

Everywhere on Google, only one piece of advice is given: add the @EnableJpaRepositories annotation. I added that the path to the packages is correct. Did not help.
Just in case context.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/data/jpa https://www.springframework.org/schema/data/jpa/spring-jpa.xsd">

    <!-- It register the beans in context and scan the annotations inside beans and activate them -->
    <context:component-scan base-package="spring" />
    <context:property-placeholder location="database.properties" />

    <!-- This produces a container-managed EntityManagerFactory;
         rather than application-managed EntityManagerFactory as in case of LocalEntityManagerFactoryBean-->
    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- This makes /META-INF/persistence.xml is no longer necessary -->
        <property name="packagesToScan" value="spring.model.entity" />
        <!-- JpaVendorAdapter implementation for Hibernate EntityManager.
             Exposes Hibernate's persistence provider and EntityManager extension interface -->
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
        </property>
        <property name="jpaProperties">
            <props>
                <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            </props>
        </property>
    </bean>

    <!-- Simple implementation of the standard JDBC DataSource interface,
         configuring the plain old JDBC DriverManager via bean properties -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${hibernate.connection.driver_class}" />
        <property name="url" value="${hibernate.connection.url}" />
    </bean>

    <!-- This transaction manager is appropriate for applications that use a single JPA EntityManagerFactory for transactional data access.
        JTA (usually through JtaTransactionManager) is necessary for accessing multiple transactional resources within the same transaction. -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
</beans>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2019-12-07
Hasanly @azerphoenix

Hello!
I can only guess where the error is buried ... And you will already need to exclude these points ..
See

public class User {
    private int id;
}

As an ID you create an int
@Repository
public interface UserRepository extends CrudRepository<User, Long> { }

And in the repository use Long
Then it would be more correct to use
public class User {
@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
}

Another point, pay attention to the presence of annotations @ID &@GeneratedValue(strategy = GenerationType.AUTO)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question