我知道我在问其他用户之前问过的相同问题,但是我想知道堆栈溢出中是否有任何页面可以解决我的问题。
我有一个这样定义的数据库配置类:
@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。我不知道这是否相关。
最佳答案
可能是由于未创建类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"} )