Eclipse 中 JPA 项目的问题 - 类注释 @Entity 中的错误:无法解析表“xxx”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7475228/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Problem with JPA Project in Eclipse - error in class annotated @Entity: Table "xxx" cannot be resolved
提问by Adam Szecowka
I'm trying to create simple EJB + JPA project in Eclipse (Indigo). I created new EJB project where:
我正在尝试在 Eclipse (Indigo) 中创建简单的 EJB + JPA 项目。我创建了新的 EJB 项目,其中:
- Target: existing Glassfish Server
- Configuration: EJB Module + GlassFish Deployment Descriptor Files + Java + JPA In window JPA Facet I declare connection to postgres db (ping successful)
- 目标:现有的 Glassfish 服务器
- 配置:EJB 模块 + GlassFish 部署描述符文件 + Java + JPA 在窗口 JPA Facet 我声明连接到 postgres db(ping 成功)
I have problem when I'm trying to define entity: Table "Employee" cannot be resolved. I added @Table annotation with specified name parameter but this didn't work. My persistence.xml file:
我在尝试定义实体时遇到问题:无法解析表“员工”。我添加了带有指定名称参数的 @Table 注释,但这不起作用。我的 persistence.xml 文件:
<persistence-unit name="pu_name">
<jta-data-source>jdbc/baza1Postgres</jta-data-source>
</persistence-unit>
In glassfish I have defined JDBC Resource with name: "jdbc/baza1Postgres"
How "eclipse know" if my table exist? What else I should configure?
在 glassfish 中,我定义了名称为“jdbc/baza1Postgres”的 JDBC 资源
“eclipse 知道”我的表是否存在?我还应该配置什么?
回答by maxmcbyte
Found this solution to "Table xxx cannot be resolved" error:
找到了“无法解析表xxx”错误的解决方法:
In Eclipse, go to Window -> Preferences -> Validation, JPA Validator, turn off for Build.
在 Eclipse 中,转到 Window -> Preferences -> Validation,JPA Validator,关闭 Build。
Sometimes you're developing an app along with a new database schema to go with it, but you get this:
有时,您正在开发一个应用程序以及与之配套的新数据库架构,但您会得到以下信息:
Schema "null" cannot be resolved for table "XXXX".
无法为表“XXXX”解析架构“null”。
This probably shouldn't be on by default anyway; people are most likely going to build new apps from scratch than build new apps to fit old databases, and even if they do build from old, Eclipse's JPA Tools has a build entities from tables function."
无论如何,这可能不应该默认开启;人们最有可能从头开始构建新应用程序,而不是构建新应用程序以适应旧数据库,即使他们确实从旧数据库构建,Eclipse 的 JPA 工具也具有从表构建实体的功能。”
回答by bobtheowl2
In Eclipse to make it happy I had to create the table via JPA Tools.
在 Eclipse 中,为了让它开心,我不得不通过 JPA 工具创建表。
Right Click Project > JPA Tools > Generate Tables from Entities
右键单击项目> JPA 工具> 从实体生成表
I guess you could turn off validation too, but creating the table seems to make more sense.
我想您也可以关闭验证,但创建表格似乎更有意义。
回答by molavec
I'm using Eclipse. I noted that eclipse recognize tables when I'm using "Data Source Explorer" window in JPA eviroment (probably not necessary to compile but useful to check database in the same eclipse IDE). Then, when I created a connection to database in that window I got the problem change data in connection properties:
我正在使用 Eclipse。我注意到,当我在 JPA 环境中使用“数据源资源管理器”窗口时,eclipse 会识别表(可能不需要编译,但对于在同一个 eclipse IDE 中检查数据库很有用)。然后,当我在该窗口中创建到数据库的连接时,我在连接属性中遇到了问题更改数据:
NOt Working (table cannot be resolved):
不工作(无法解析表):
Database: my_database
URL: jdbc:mysql://localhost:3306
User Name: jpa_user
Password: jpa_pass
Fixed (I add database in URL field):
固定(我在 URL 字段中添加数据库):
Database: my_database
URL: jdbc:mysql://localhost:3306/my_database
User Name: jpa_user
Password: jpa_pass
Remember to use @Table(name="") in your entities if the tables are not with upper case in database.
如果数据库中的表不是大写,请记住在实体中使用 @Table(name="") 。
回答by Ross
You need to specify in your persistence.xml file where to look for the entities, have a look at the Java EE 6 tutorial, here's an example taken from there:
您需要在persistence.xml 文件中指定在何处查找实体,查看Java EE 6 教程,这是从那里获取的示例:
Persistence Units
A persistence unit defines a set of all entity classes that are managed by EntityManager instances in an application. This set of entity classes represents the data contained within a single data store.
Persistence units are defined by the persistence.xml configuration file. The following is an example persistence.xml file:
<persistence> <persistence-unit name="OrderManagement"> <description>This unit manages orders and customers. It does not rely on any vendor-specific features and can therefore be deployed to any persistence provider. </description> <jta-data-source>jdbc/MyOrderDB</jta-data-source> <jar-file>MyOrderApp.jar</jar-file> <class>com.widgets.Order</class> <class>com.widgets.Customer</class> </persistence-unit> </persistence>
This file defines a persistence unit named OrderManagement, which uses a JTA-aware data source: jdbc/MyOrderDB. The jar-file and class elements specify managed persistence classes: entity classes, embeddable classes, and mapped superclasses. The jar-file element specifies JAR files that are visible to the packaged persistence unit that contain managed persistence classes, whereas the class element explicitly names managed persistence classes.
持久性单元
持久性单元定义了一组由应用程序中的 EntityManager 实例管理的所有实体类。这组实体类表示单个数据存储中包含的数据。
持久性单元由persistence.xml 配置文件定义。以下是一个示例 persistence.xml 文件:
<persistence> <persistence-unit name="OrderManagement"> <description>This unit manages orders and customers. It does not rely on any vendor-specific features and can therefore be deployed to any persistence provider. </description> <jta-data-source>jdbc/MyOrderDB</jta-data-source> <jar-file>MyOrderApp.jar</jar-file> <class>com.widgets.Order</class> <class>com.widgets.Customer</class> </persistence-unit> </persistence>
这个文件定义了一个名为 OrderManagement 的持久化单元,它使用一个 JTA-aware 数据源:jdbc/MyOrderDB。jar-file 和 class 元素指定托管持久性类:实体类、可嵌入类和映射超类。jar-file 元素指定对包含受管持久性类的打包持久性单元可见的 JAR 文件,而 class 元素显式命名受管持久性类。
Also take a look at this stackoverflow questionusing hibernate you can scan for your classes that have the Entity annotation
还可以使用 hibernate查看此stackoverflow 问题,您可以扫描具有实体注释的类
回答by K.Nicholas
I had this error and was able to get it working through a very tortuous effort. I am using PostgreSQL.
我遇到了这个错误,并且能够通过非常曲折的努力让它工作。我正在使用 PostgreSQL。
First off, I did as suggested above and turned off validation for the build. Then I ran validation manually getting things connected. My steps were essentially this:
首先,我按照上面的建议做了,并关闭了构建验证。然后我手动运行验证来连接东西。我的步骤基本上是这样的:
1: Connect through Data Source Explorer to my database. Insure that the connection is in the correct state my going to the TABLES and hit refresh. Hitting refresh on the connection didn't refresh the tables.
1:通过数据源资源管理器连接到我的数据库。确保连接处于正确状态,我将转到 TABLES 并点击刷新。在连接上点击刷新并没有刷新表。
2: Probably optional, but I used JPA Tools to export the schema to the database. Again, I hit refresh as in step 1.
2:可能是可选的,但我使用 JPA 工具将架构导出到数据库。再次,我在步骤 1 中点击刷新。
3: Annotate the Entity with both Entity and Table. Add the name in entity and the NAME, CATALOG and SCHEMA in the Table annotation.
3:用实体和表注释实体。在实体中添加名称,在表注释中添加 NAME、CATALOG 和 SCHEMA。
@Entity(name="DBAssembly")
@Table(name="DBAssembly",catalog="lag",schema="public")
4: Run validation manually. Adding the catalog was important. It's very picky.
4:手动运行验证。添加目录很重要。它非常挑剔。
回答by cristianchiess
It work's but, In addition to maxmcbyte(Thanks!!) answer, some times it is better to apply this solution just for some projects, do it individually by accessing: project properties > Validation > Turn on"Enabled project specific settings"and than turn offJPA Validation in the Build Column. See the Image...
它工作的,但是,除了maxmcbyte(谢谢!)答案,有时它是更好地运用这一解决方案只是一些项目,通过访问单独做到这一点:项目属性>验证>打开的“已启用项目特定设置”,比关闭关闭JPA验证在构建列。看图...
回答by AngryMonkey
Another option using maxmcbyte's answer is to set it only for specific projects. Right-click on the project and select Validation. Enable project specific settings and disable JPA Validator for Build.
使用 maxmcbyte 答案的另一个选项是仅为特定项目设置它。右键单击项目并选择验证。启用项目特定设置并禁用 JPA Validator for Build。