java.lang.IllegalArgumentException问题:不是托管类型

时间:2019-06-18 21:47:28

标签: java spring spring-boot jpa spring-data

我知道我在问其他用户之前问过的相同问题,但是我想知道Stack Overflow中是否有任何页面可以解决我的问题。

我有一个这样定义的数据库配置类:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = {AppConfig.CLASS_REPOSITORIES})
public class DatabaseConfig {

    private static final Logger LOG = Log.getLog(AppConfig.class);

    @Autowired
    AppConfig config;

    //Bean methods
}

常量AppConfig.CLASS_REPOSITORIES在“应用程序配置”类中定义如下:

@Configuration
@ComponentScan({AppConfig.CLASS_SERVICES,AppConfig.CLASS_WEB, AppConfig.CLASS_WEBCORE}) 
@Import({DatabaseConfig.class, WebConfig.class, DBIccidsConfig.class, DBOabiConfig.class})
public class AppConfig  {
    private static final Logger LOG = Log.getLog(AppConfig.class);
    /**************************************************/

    /**************************************************/
    public static final  String CONS_APP_CONFIG      =  Constants.NAME.toUpperCase();    
    public static final  String CLASS_SERVICES       = "cl.claro.berrws.business.services";
    public static final  String CLASS_WEB            = "cl.claro.berrws.soap.controllers";
    public static final  String CLASS_WEBCORE        = "cl.claro.berrws.soap.core";
    public static final  String CLASS_REPOSITORIES   = "cl.claro.berrws.persistence.repositories";
    public static final  String CLASS_ENTITIES       = "cl.claro.berrws.persistence.entities";
    public static final  String CLASS_ICCIDS_REPOSITORIES   = "cl.claro.berrws.persistence.iccids.repositories";
    public static final  String CLASS_ICCIDS_ENTITIES       = "cl.claro.berrws.persistence.iccids.entities";   
    public static final  String CLASS_OABI_REPOSITORIES   = "cl.claro.berrws.persistence.imeioabi.repositories";
    public static final  String CLASS_OABI_ENTITIES       = "cl.claro.berrws.persistence.imeioabi.entities";   

//Bean methods that are not relevant for this problem

常量CLASS_REPOSITORIES指向包含两个存储库接口的软件包cl.claro.berrws.persistence.iccids.repositories。其中之一是EstadoRepository,定义如下:

package cl.claro.berrws.persistence.repositories;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import cl.claro.berrws.persistence.entities.Estado;

@Repository
public interface EstadoRepository extends JpaRepository<Estado, String>{
    @Query("SELECT e FROM Estado e WHERE e.imei=:imei")
    public Estado obtieneEstado(@Param("imei") String imei);
}

Estado存在于cl.claro.berrws.persistence.entities类中的常量CLASS_ENTITIES所引用的包AppConfig中。类Estado的定义如下:

@Data
@Entity
@Table(name = "TBL_ESTADOS")
@XmlRootElement
public class Estado implements Serializable {
    //Some attributes
    @Id
    @Column(name = "IMEI", length=20)
    @Basic(optional = false)
    private String imei;

    //More attributes
}

属性的顺序在数据库(Oracle数据库)中建立

DatabaseConfig类定义使用实体类的Bean方法,如下所示:

@Bean
    LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, Environment env) {
        if (LOG.isInfoEnabled()) LOG.info("Configurando ENTITY MANAGER");
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource);
        entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        entityManagerFactoryBean.setPackagesToScan(AppConfig.CLASS_ENTITIES);
        Properties jpaProperties = new Properties();
        jpaProperties.put("hibernate.dialect",Config.getConfig("hibernate.dialect"));        
        jpaProperties.put("hibernate.show_sql",Config.getConfig("hibernate.showSql"));
        jpaProperties.put("hibernate.format_sql",Config.getConfig("hibernate.format_sql")); 
        jpaProperties.put("hibernate.hbm2ddl.auto",Config.getConfig("hibernate.hbm2ddl")); 
        jpaProperties.put("hibernate.enable_lazy_load_no_trans",true); 
        entityManagerFactoryBean.setJpaProperties(jpaProperties);        
        return entityManagerFactoryBean;
    }    

仍然,当我执行我的应用程序时,出现以下异常: java.lang.IllegalArgumentException: Not a managed type: class cl.claro.berrws.persistence.entities.Estado

我错过了什么吗? 预先谢谢大家。

P.D .:该应用程序还使用Maven。我不知道这是否相关。

1 个答案:

答案 0 :(得分:0)

也许是由于未创建类cl.claro.berrws.persistence.entities.Estado的bean而发生此问题。在DatabaseConfig .class中,您仅指定要扫描的软件包AppConfig.CLASS_REPOSITORIES,因此,请尝试添加以下内容:

@ComponentScan("cl.claro.berrws.persistence.entities")

,以便在春季之前也可以提取实体类。因此您的配置应类似于:

@Configuration
@EnableTransactionManagement
@ComponentScan("cl.claro.berrws.persistence.entities")
@EnableJpaRepositories(basePackages = {AppConfig.CLASS_REPOSITORIES})
public class DatabaseConfig {

    private static final Logger LOG = Log.getLog(AppConfig.class);

    @Autowired
    AppConfig config;

    //Bean methods
}

或者您可以尝试使用@EntityScan注释指定实体类包:

@EntityScan( basePackages = {"domain"} )