为什么我的实体不能在SpringBoot中使用,尽管它在没有 [英] Why does my Entity does not work with SpringBoot although it works without

查看:127
本文介绍了为什么我的实体不能在SpringBoot中使用,尽管它在没有的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

((请注意:正在调查此问题我更好地发现了我在这里介绍的问题的根源)

(Please note : while investigating on this issue I better spotted the source of problem that I introduce here)

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

I am very new to Hibernate and SpringBoot. My project deals with a search engine which indexing (javafx client) and searching (web client) parts are separated. The web client uses SpringBoot and during the process of the user's request, I need to retrieve information on the Solr Index to search against. These information are stored in a Hibernate / H2 local database where I use the following Entity class :

@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类具有一种方法,该方法可以列出数据库中的所有IndexSetup(还有其他方法,但是专注于此方法足以理解该问题).我需要在控制器中调用它,但得到一个不是实体:类IndexSetup" .

A LocalDatabase class features a method to list all IndexSetups from the DB (there are also other methods but focusing on this one is enough to understand the issue). I need to call it in a controller but I get a "Not an entity : class IndexSetup".

但是,使用我的实体的唯一方法是阻止SpringBoot的启动(即,注释以SpingApplication.run(...)开头的行):

However the only way to use my Entity is to prevent the launching of SpringBoot (ie commenting the line starting with SpingApplication.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处理用户的请求!

Of course this is not a solution since I do need to process the user's request via Spring Boot :-D!

最后,hibernate.cfg.xml文件读取:

Finally the hibernate.cfg.xml file reads :

<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在启动时打印的这两行:

And I've just noticed those two lines printed by Spring Boot at startup :

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应用程序时停止工作.

It looks unlikely to be a dependency problem as it only stops working when Spring application is run.

所以我的问题是:如何使Entity在SpringBoot中工作?

So my question is : what should I do to make my Entity work within SpringBoot ?

任何帮助,不胜感激!

推荐答案

将@EntityScan添加到ServerApplication.class.

Add @EntityScan to ServerApplication.class.

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

@EntityScan (org.springframework.boot.autoconfigure.domain.EntityScan) identifies which classes should be used by a specific persistence context.

这篇关于为什么我的实体不能在SpringBoot中使用,尽管它在没有的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆