使用 Spring & JPA,获取 java.lang.IllegalArgumentException: Unknown entity

Posted

技术标签:

【中文标题】使用 Spring & JPA,获取 java.lang.IllegalArgumentException: Unknown entity【英文标题】:Using Spring & JPA, getting java.lang.IllegalArgumentException: Unknown entity 【发布时间】:2017-05-10 11:17:33 【问题描述】:

所以我一直在尝试让这个应用程序使用 Spring 4、Spring Boot 1.4 和 Hibernate 5 工作(通过检查 Maven 中的 maven 依赖项进行验证,但无论出于何种原因,它都不会确认我的查看位置设置对于特定包中的实体类,而不是在一个完全不同的包中查找,我找不到它的指定位置。使用 javax.persistence pacakge 代替 org.hibernate 等步骤没有结果。也使用 SessionFactory 代替 EntityManager产生了同样的错误。没有使用persistence.xml(或者实际上任何xml,就像使用Spring 3一样,我想要一个完全基于Java的配置,从研究来看这应该是可能的?

事不宜迟,这是当前代码。

POM 文件:

<properties>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
    <spring.boot.version>1.4.2.RELEASE</spring.boot.version>
</properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>$spring.boot.version</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-batch</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.integration</groupId>
        <artifactId>spring-integration-file</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
    </dependency>

    <!-- Other -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.4.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.2</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.40</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

实体类:

@Entity
@Table(name = "inventory")
public class InventoryDBModel implements Serializable 

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "product_id")
    private int productID;

    @Id
    @Column(name = "store_id")
    private int storeID;

    @Column(name = "quantity")
    private int quantity;

    @Column(name = "reported_on")
    private String reportedOn; //Convert to Date

    @Column(name = "updated_at")
    private String updatedAt; //Convert to Date

    public int getProductID() 
        return productID;
    

    public int getStoreID() 
        return storeID;
    

    public int getQuantity() 
        return quantity;
    

    public String getReportedOn() 
        return reportedOn;
    

    public String getUpdatedAt() 
        return updatedAt;
    

    public void setProductID(final int productID) 
        this.productID = productID;
    

    public void setStoreID(final int storeID) 
        this.storeID = storeID;
    

    public void setQuantity(final int quantity) 
        this.quantity = quantity;
    

    public void setReportedOn(final String reportedOn) 
        this.reportedOn = reportedOn;
    

    public void setUpdatedAt(final String updatedAt) 
        this.updatedAt = updatedAt;
    

DAO 接口:

public interface InventoryDAO 
    public void createOne(LCBOInventory lcboInventory);

    public void createMany(List<? extends LCBOInventory> lcboInventorysItems);

    public void delete(int productID, int styleID);

    public List<LCBOInventory> getByProduct(int productID);

    public LCBOInventory getByProductAndStore(int productID, int storeID);

    public List<LCBOInventory> getByStore(int storeID);

    public List<LCBOInventory> list();

    public void updateOne(LCBOInventory lcboInventory);

    public void updateMany(List<LCBOInventory> lcboInventoryItems); 

DAO 实现类:

public class InventoryDAOImpl implements InventoryDAO 

    static final String SELECT_INVENTORY = "SELECT i FROM inventory ";
    static final String PRODUCTID = "productID";
    static final String STOREID = "storeID";

    @Autowired
    private EntityManager em;

    public InventoryDAOImpl() 
         //basic constructor
    

    public InventoryDAOImpl(final EntityManager em) 
        this.em = em;
    

    @Override
    @Transactional
    public void createOne(final LCBOInventory newLCBOInventoryItem) 
        LCBOInventory lcboInventoryObject = new LCBOInventory();

        lcboInventoryObject.setProductID(newLCBOInventoryItem.getProductID());
        lcboInventoryObject.setQuantity(newLCBOInventoryItem.getQuantity());
        lcboInventoryObject.setReportedOn(newLCBOInventoryItem.getReportedOn());
        lcboInventoryObject.setStoreID(newLCBOInventoryItem.getStoreID());
        lcboInventoryObject.setUpdatedAt(newLCBOInventoryItem.getUpdatedAt());

        em.persist(lcboInventoryObject);
    

    @Override
    @Transactional
    public void createMany(final List<? extends LCBOInventory> lcboInventorysItems)        
        lcboInventorysItems.stream().forEach((currentInventoryItem) -> 
            LCBOInventory lcboInventoryObject = new LCBOInventory();

            lcboInventoryObject.setProductID(currentInventoryItem.getProductID());
            lcboInventoryObject.setQuantity(currentInventoryItem.getQuantity());
            lcboInventoryObject.setReportedOn(currentInventoryItem.getReportedOn());
            lcboInventoryObject.setStoreID(currentInventoryItem.getStoreID());
            lcboInventoryObject.setUpdatedAt(currentInventoryItem.getUpdatedAt());

            em.persist(lcboInventoryObject);
        ); 
    

    @Override
    public void delete(final int productID, final int styleID) 
        Query loadSpecificProductStoreCombo = em.createQuery("DELETE i FROM inventory i WHERE i.productID = :productID AND i.storeID = :storeID");

        loadSpecificProductStoreCombo.setParameter("productID", productID);
        loadSpecificProductStoreCombo.setParameter("storeID", styleID);

        loadSpecificProductStoreCombo.executeUpdate();
    

    @Override
    public List<LCBOInventory> getByProduct(final int productID) 
        TypedQuery<LCBOInventory> loadInventoryByProduct = em.createQuery(SELECT_INVENTORY + "WHERE i.productID = :productID " +
                                                                            "AND i.storeID = :storeID", LCBOInventory.class);

        loadInventoryByProduct.setParameter("productID", productID);

        return loadInventoryByProduct.getResultList();
    

    @Override
    public LCBOInventory getByProductAndStore(final int productID, final int storeID) 
        TypedQuery<LCBOInventory> loadSpecificProductStoreCombo = em.createQuery(SELECT_INVENTORY + "WHERE i.productID = :productID " +
                                                                                    "AND i.storeID = :storeID", LCBOInventory.class);

        loadSpecificProductStoreCombo.setParameter("productID", productID);
        loadSpecificProductStoreCombo.setParameter("storeID", storeID);

        return loadSpecificProductStoreCombo.getSingleResult();
    

    @Override
    public List<LCBOInventory> getByStore(final int storeID) 
        TypedQuery<LCBOInventory> loadInventoryByStore = em.createQuery(SELECT_INVENTORY + "WHERE i.productID = :productID " +
                                                                            "AND i.storeID = :storeID", LCBOInventory.class);

        loadInventoryByStore.setParameter("storeID", storeID);

        return loadInventoryByStore.getResultList();
    

    @Override
    public List<LCBOInventory> list() 
        return em.createQuery("SELECT i FROM product i", LCBOInventory.class).getResultList();
    

    @Override
    @Transactional
    public void updateOne(final LCBOInventory newLCBOInventoryItem) 
        TypedQuery<LCBOInventory> loadSpecificProductStoreCombo = em.createQuery(SELECT_INVENTORY + "WHERE i.productID = :productID " +
                                                                                    "AND i.storeID = :storeID", LCBOInventory.class);

        loadSpecificProductStoreCombo.setParameter("productID", newLCBOInventoryItem.getProductID());
        loadSpecificProductStoreCombo.setParameter("storeID", newLCBOInventoryItem.getStoreID());

        LCBOInventory oldLCBOInventoryItem = loadSpecificProductStoreCombo.getSingleResult();

        oldLCBOInventoryItem.setProductID(newLCBOInventoryItem.getProductID());
        oldLCBOInventoryItem.setQuantity(newLCBOInventoryItem.getQuantity());
        oldLCBOInventoryItem.setReportedOn(newLCBOInventoryItem.getReportedOn());
        oldLCBOInventoryItem.setStoreID(newLCBOInventoryItem.getStoreID());
        oldLCBOInventoryItem.setUpdatedAt(newLCBOInventoryItem.getUpdatedAt());
    

    @Override
    public void updateMany(List<LCBOInventory> lcboInventoryItems) 
        // TODO Auto-generated method stub

    

数据库配置类:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages="com.sample.hibernate.model")
public class DatabaseConfig 

    @Autowired
    private LCBOInventoryTrackerProperties properties;

    @Bean(name = "entityManager")
    public EntityManagerFactory entityManagerFactory() throws SQLException 

        final LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
        factoryBean.setDataSource(dataSource());
        factoryBean.setJpaDialect(new HibernateJpaDialect());
        factoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        factoryBean.setPersistenceProviderClass(HibernatePersistenceProvider.class); 
        factoryBean.setPersistenceUnitName("persistenceUnit");
        factoryBean.setJpaProperties(getHibernateProperties());
        factoryBean.setPackagesToScan(new String[]"com.sample.hibernate.model"); 
        factoryBean.afterPropertiesSet();

        return factoryBean.getObject();
    

    @Bean
    public DataSource dataSource() throws SQLException 
        DriverManagerDataSource dataSource = new DriverManagerDataSource();

        dataSource.setDriverClassName(properties.getDb().getDriver());
        dataSource.setUrl(properties.getDb().getUrl());
        dataSource.setUsername(properties.getDb().getUsername());
        dataSource.setPassword(properties.getDb().getPassword());

        return dataSource;
    

    private Properties getHibernateProperties() 
        Properties hibernateConfigProperties = new Properties();

        hibernateConfigProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
        hibernateConfigProperties.put("hibernate.show_sql", true);
        hibernateConfigProperties.put("hibernate.generate_statistics", true);
        hibernateConfigProperties.put("hibernate.hbm2ddl.auto", "update");
        hibernateConfigProperties.put("hibernate.use_sql_comments", true);

        return hibernateConfigProperties;
    

    @Bean
    public PlatformTransactionManager transactionManager() throws SQLException
       JpaTransactionManager transactionManager = new JpaTransactionManager();
       transactionManager.setEntityManagerFactory(this.entityManagerFactory());

       return transactionManager;
    

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation()
       return new PersistenceExceptionTranslationPostProcessor();
    

    //DAO Autowires

    @Autowired
    @Bean(name = "inventoryDAO")
    public InventoryDAO getInventoryDAO(final EntityManager entityManager) 
        return new InventoryDAOImpl(entityManager);
    

最后但并非最不重要的是堆栈跟踪。

java.lang.IllegalArgumentException: Unknown entity: com.sample.lcbo.domain.LCBOInventory
    at org.hibernate.jpa.spi.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:1149) ~[hibernate-entitymanager-5.0.11.Final.jar:5.0.11.Final]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_92]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_92]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_92]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_92]
    at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:347) ~[spring-orm-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at com.sun.proxy.$Proxy60.persist(Unknown Source) ~[na:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_92]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_92]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_92]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_92]
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:298) ~[spring-orm-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at com.sun.proxy.$Proxy60.persist(Unknown Source) ~[na:na]
    at com.sample.lcbo.dao.InventoryDAOImpl.lambda$0(InventoryDAOImpl.java:56) ~[classes/:na]
    at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(Unknown Source) ~[na:1.8.0_92]
    at java.util.stream.ReferencePipeline$Head.forEach(Unknown Source) ~[na:1.8.0_92]
    at com.sample.lcbo.dao.InventoryDAOImpl.createMany(InventoryDAOImpl.java:47) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_92]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_92]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_92]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_92]
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333) [spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) [spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) [spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) ~[spring-tx-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) [spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at com.sun.proxy.$Proxy62.createMany(Unknown Source) ~[na:na]
    at com.sample.lcbo.writer.LCBOInventoryWriter.write(LCBOInventoryWriter.java:20) ~[classes/:na]
    at org.springframework.batch.core.step.item.SimpleChunkProcessor.writeItems(SimpleChunkProcessor.java:175) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.step.item.SimpleChunkProcessor.doWrite(SimpleChunkProcessor.java:151) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.step.item.SimpleChunkProcessor.write(SimpleChunkProcessor.java:274) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.step.item.SimpleChunkProcessor.process(SimpleChunkProcessor.java:199) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.step.item.ChunkOrientedTasklet.execute(ChunkOrientedTasklet.java:75) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:406) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.step.tasklet.TaskletStep$ChunkTransactionCallback.doInTransaction(TaskletStep.java:330) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:133) ~[spring-tx-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.batch.core.step.tasklet.TaskletStep$2.doInChunkContext(TaskletStep.java:271) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.scope.context.StepContextRepeatCallback.doInIteration(StepContextRepeatCallback.java:81) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.repeat.support.RepeatTemplate.getNextResult(RepeatTemplate.java:374) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.repeat.support.RepeatTemplate.executeInternal(RepeatTemplate.java:215) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.repeat.support.RepeatTemplate.iterate(RepeatTemplate.java:144) ~[spring-batch-infrastructure-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.step.tasklet.TaskletStep.doExecute(TaskletStep.java:257) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:200) ~[spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.job.AbstractJob.handleStep(AbstractJob.java:392) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.job.SimpleJob.doExecute(SimpleJob.java:135) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50) [spring-core-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_92]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_92]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_92]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_92]
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333) [spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190) [spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) [spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127) [spring-batch-core-3.0.7.RELEASE.jar:3.0.7.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:213) [spring-aop-4.3.4.RELEASE.jar:4.3.4.RELEASE]
    at com.sun.proxy.$Proxy65.run(Unknown Source) [na:na]
    at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.execute(JobLauncherCommandLineRunner.java:216) [spring-boot-autoconfigure-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.executeLocalJobs(JobLauncherCommandLineRunner.java:233) [spring-boot-autoconfigure-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.launchJobFromProperties(JobLauncherCommandLineRunner.java:125) [spring-boot-autoconfigure-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.run(JobLauncherCommandLineRunner.java:119) [spring-boot-autoconfigure-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:784) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at org.springframework.boot.SpringApplication.afterRefresh(SpringApplication.java:771) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1186) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at org.springframework.boot.SpringApplication.run(SpringApplication.java:1175) [spring-boot-1.4.2.RELEASE.jar:1.4.2.RELEASE]
    at com.sample.lcbo.config.LCBOBatchConfig.main(LCBOBatchConfig.java:69) [classes/:na]

对此有任何帮助,我将不胜感激。虽然我尽了最大的努力,但如果有任何问题,请尽管提出,我会尽力提供。

【问题讨论】:

看起来你已经混合了一些来自 hibernate + spring 项目的配置和一些来自 spring 数据项目的配置。请仔细阅读这个tutorial,不要实现DAO层。您还可以发布整个堆栈跟踪并指出您实际想要实现的目标......? @AntJavaDev 哦,我并不感到惊讶,它看起来像是各种物品的集合,被挤成一块拼凑的被子。试图找到一个高质量的教程并不容易,所以不得不从各种来源中获取片段。我将再试一次该教程,并包含一个堆栈跟踪以供审查。至于实现什么,只要能够使用hibernate和Spring Batch进行数据库操作即可。这就是这个项目开始的一般要点。 您问题中的实体类名为InventoryDBModel,而您尝试持久化的类型名为LCBOInventory。您确定您发布了正确的代码吗?您能否查看代码(或您的应用程序)并发布正确的代码(或更正您的应用程序)? 首先,您发布的实体InventoryDBModel 没有在您的代码中引用。其次 。从您的堆栈跟踪中,很明显 Unknown entity : com.sample.lcbo.domain.LCBOInventory ,因为您将这个包传递给您的 entityManager 以扫描 com.sample.hibernate.model ... ? 所以发现原因似乎是多个教程的实施不佳。完全重构代码以使用直接 Spring JPA 并且没有任何旧代码看起来已经解决了这个问题。对于学习新技术带来的困惑和乐趣,我们深表歉意。 【参考方案1】:

重要的是,persistence.xml 免费方法从 Spring 3.1 开始就可以使用。请参阅文档here。

另外,您应该知道 Spring Boot 1.4 需要 Spring Framework 4.3。请参阅其中的release notes。

你看到官方 Spring 指南Accessing Data with JPA了吗?

更新 #1

您正在尝试使用实体 com.sample.lcbo.domain.LCBOInventory(请参阅异常详细信息),但您将 com.sample.hibernate.model 设置为 EntityManagerFactoryBean.setPackagesToScan 方法。

【讨论】:

感谢您的回复。我已验证该应用程序在 Eclipse 的 Maven 依赖项下将 Spring core 4.3.4、Spring boot 1.4.2 和 Hibernite 5.0.11 列为 jar。我还检查了链接的 Spring Guides。看起来我没有遗漏任何值得注意的内容,或者至少没有遗漏任何会导致此错误发生的内容。 感谢您的回复。它所指的项目也不是域,而只是定义库存对象的类。实体类位于“com.sample.hibernate.model”中,但无论出于何种原因,程序都希望转到 com.sample.lcbo.domain,但在任何地方都没有指明它可以扫描。因此混乱 没有混淆,因为您将 EntityManager 的“持久”和“选择查询”与 LCBOInventory 类一起使用,这就是 Hibernate 将此类视为实体类的原因。应该在 DAO 中使用 InventoryDBModel 而不是 LCBOInventory。 有趣。我认为 Entity 注释和 setPackages 将是它的确定方式。

以上是关于使用 Spring & JPA,获取 java.lang.IllegalArgumentException: Unknown entity的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot JPA多对多关系-Rest Web Service无法获取Get All中的所有子属性

spring-data-jpa 如何使用多个数据源? [复制]

使用 Spring 数据 JPA 获取随机记录

使用 spring-data-jpa 获取这些数据如何更正确?

Spring数据jpa保存无法获取id

Spring数据jpa保存无法获取id