gpt4 book ai didi

java - java.lang.IllegalArgumentException : Not a managed type 的问题

转载 作者:行者123 更新时间:2023-12-02 04:16:27 26 4
gpt4 key购买 nike

我知道我问的问题与其他用户之前问过的问题相同,但我想知道 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包含两个存储库接口(interface)。其中之一是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);
}

类(class)Estado存在于包cl.claro.berrws.persistence.entities中由常量 CLASS_ENTITIES 引用在 AppConfig类(class)。类(class)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 class 定义了使用实体类的 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。不知道有没有关系。

最佳答案

可能出现此问题是因为您的类 cl.claro.berrws.persistence.entities.Estado 的 bean 未创建。在 DatabaseConfig .class 中,您仅指定要扫描的包 AppConfig.CLASS_REPOSITORIES,因此请尝试添加以下内容:

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

这样实体类也会被 spring 拾取。所以你的配置应该是这样的:

@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"} )

关于java - java.lang.IllegalArgumentException : Not a managed type 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56657483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com