为什么我的实体不能与 SpringBoot 一起工作,尽管它可以在没有 SpringBoot 的情况下工作

2024-01-09

(请注意:在调查时这个问题 https://stackoverflow.com/questions/52799706/springboot-not-an-entity?noredirect=1#comment92527948_52799706我更好地找到了我在这里介绍的问题根源)

我对 Hibernate 和 SpringBoot 非常陌生。我的项目涉及一个搜索引擎,其中索引(javafx 客户端)和搜索(Web 客户端)部分是分开的。 Web客户端使用SpringBoot,在用户请求的过程中,我需要检索Solr索引上的信息以进行搜索。这些信息存储在 Hibernate/H2 本地数据库中,我在其中使用以下实体类:

@Entity
@Table(name = "IndexSetups")
@Access(AccessType.PROPERTY)
public class IndexSetup {
 private final SimpleIntegerProperty id = new SimpleIntegerProperty();

 @Id
 @GeneratedValue(strategy = GenerationType.AUTO) // For H2 AUTO is required to auto increment the id
 public int getId() {
  return id.get();
}

 //... other properties, getters and setters

}

LocalDatabase 类具有一种列出数据库中所有 IndexSetups 的方法(还有其他方法,但关注这个方法就足以理解问题)。我需要在控制器中调用它,但我得到了“不是实体:类 IndexSetup” https://stackoverflow.com/questions/52799706/springboot-not-an-entity?noredirect=1#comment92527948_52799706.

然而,使用我的实体的唯一方法是阻止启动 Spring Boot(注释以 SpringApplication.run(...) 开头的行):

@SpringBootApplication
public class ServerApplication {

public static void main(String[] args) {
//      SpringApplication.run(ServerApplication.class, args); 

ArrayList<IndexSetup> availableSearchIndex = 
LocalDatabase.getInstance(false) // no GUI
            .getAllIndexSetups();

// The list is displayed as long as SpringApplication.run() call is commented. Otherwise "Not an entity"
System.err.println("The Index setups are " + availableSearchIndex); 
  }
}

当然,这不是一个解决方案,因为我确实需要通过 Spring Boot 处理用户的请求:-D!

最后 hibernate.cfg.xml 文件内容如下:

<hibernate-configuration>
  <session-factory>      
    <!-- Database connection settings -->
    <property name="connection.driver_class">org.h2.Driver</property>
    <!--The db file resides wihtin the parent project (so one level above the calling project see ../)-->
    <!--We use the auto server mode to be able to open the database from server and from indexer simultaneously
    see http://www.h2database.com/html/features.html#auto_mixed_mode -->
    <property name="connection.url">jdbc:h2:file:../MyAppDB;DB_CLOSE_ON_EXIT=TRUE;AUTO_SERVER=TRUE</property>
    <property name="connection.username">test</property>
    <property name="connection.password"/>
    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>
    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.H2Dialect</property>
    <!-- Disable the second-level cache -->
    <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">false</property>
    <!-- Updates the existing database schema on startup -->
    <property name="hbm2ddl.auto">update</property>
    <!-- The mapping information of entities -->
    <mapping class="my.package.Entities.IndexSetup"/>
 </session-factory>
</hibernate-configuration>

堆栈跟踪是:

Exception in thread "restartedMain" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)

Caused by: java.lang.IllegalArgumentException: Not an entity: class my.package.Entities.IndexSetup
at org.hibernate.metamodel.internal.MetamodelImpl.entity(MetamodelImpl.java:457)
at org.hibernate.query.criteria.internal.QueryStructure.from(QueryStructure.java:126)
at org.hibernate.query.criteria.internal.CriteriaQueryImpl.from(CriteriaQueryImpl.java:153)
at my.package.LocalDatabase.getAllIndexSetups(LocalDatabase.java:99)
at my.package.ServerApplication.main(ServerApplication.java:15)

我刚刚注意到 Spring Boot 在启动时打印的这两行:

2018-10-15 11:47:46.208  INFO 27730 --- [  restartedMain] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found // Maybe this one is not just "INFO"
2018-10-15 11:47:46.591  WARN 27730 --- [  restartedMain] org.hibernate.orm.connections.pooling    : HHH10001002: Using Hibernate built-in connection pool (not for production use!)
2018-10-15 11:47:46.594  INFO 27730 --- [  restartedMain] org.hibernate.orm.connections.pooling    : HHH10001005: using driver [org.h2.Driver] at URL [jdbc:h2:file:../MyAppDB;DB_CLOSE_ON_EXIT=TRUE;FILE_LOCK=NO] // Just to show that my bibernate.cfg.xml is found

它看起来不太可能是依赖问题,因为它仅在 Spring 应用程序运行时停止工作。

所以我的问题是:我应该怎么做才能让我的实体在 SpringBoot 中工作?

非常感谢任何帮助!


将@EntityScan添加到ServerApplication.class。

@EntityScan (org.springframework.boot.autoconfigure.domain.EntityScan) 标识特定持久化上下文应该使用哪些类。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

为什么我的实体不能与 SpringBoot 一起工作,尽管它可以在没有 SpringBoot 的情况下工作 的相关文章

随机推荐