Eclipse 中的 JPA 项目出现问题 - 注释为 @Entity 的类中出现错误:表“xxx”无法解决
我正在尝试在 Eclipse (Indigo) 中创建简单的 EJB + JPA 项目。我创建了新的 EJB 项目,其中:
- 目标:现有 Glassfish 服务器
- 配置:EJB 模块 + GlassFish 部署描述符文件 + Java + JPA 在窗口 JPA Facet 中,我声明与 postgres db 的连接(ping 成功),
当我尝试定义实体时遇到问题:表“Employee”无法解析。我添加了带有指定名称参数的 @Table 注释,但这不起作用。 我的 persistence.xml 文件:
<persistence-unit name="pu_name">
<jta-data-source>jdbc/baza1Postgres</jta-data-source>
</persistence-unit>
在 glassfish 中,我定义了 JDBC 资源,名称为:“jdbc/baza1Postgres”
“日食如何知道”我的表是否存在?我还应该配置什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
找到了“Table xxx无法解析”错误的解决方案:
在Eclipse中,转到Window -> >首选项->验证,JPA Validator,关闭构建。
有时,您正在开发一个应用程序以及与之配套的新数据库架构,但您会得到以下结果:
无论如何,这可能不应该默认打开;人们最有可能从头开始构建新应用程序,而不是构建新应用程序来适应旧数据库,即使他们确实从旧数据库构建,Eclipse 的 JPA 工具也具有从表构建实体的功能。”
Found this solution to "Table xxx cannot be resolved" error:
In Eclipse, go to Window -> Preferences -> Validation, JPA Validator, turn off for Build.
Sometimes you’re developing an app along with a new database schema to go with it, but you get this:
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 工具创建表。
右键单击项目> JPA工具>从实体生成表
我想您也可以关闭验证,但创建表似乎更有意义。
In Eclipse to make it happy I had to create the table via JPA Tools.
Right Click Project > JPA Tools > Generate Tables from Entities
I guess you could turn off validation too, but creating the table seems to make more sense.
我正在使用 Eclipse。我注意到,当我在 JPA 环境中使用“数据源资源管理器”窗口时,Eclipse 会识别表(可能不需要编译,但对于在同一 Eclipse IDE 中检查数据库很有用)。然后,当我在该窗口中创建到数据库的连接时,我在连接属性中遇到了更改数据的问题:
不工作(表无法解析):
已修复(我在 URL 字段中添加数据库):
记住使用 @Table(name="如果数据库中的表不是大写的,则在您的实体中。
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:
NOt Working (table cannot be resolved):
Fixed (I add database in URL field):
Remember to use @Table(name="") in your entities if the tables are not with upper case in database.
您需要在 persistence.xml 文件中指定在何处查找实体,请查看 Java EE 6 教程,以下是取自该教程的示例:
另请使用 hibernate 查看此 stackoverflow 问题,您可以扫描具有 Entity 注释的类
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:
Also take a look at this stackoverflow question using hibernate you can scan for your classes that have the Entity annotation
我遇到了这个错误,并通过非常曲折的努力才使其正常工作。我正在使用 PostgreSQL。
首先,我按照上面的建议进行操作并关闭了构建验证。然后我手动运行验证以连接事物。我的步骤基本上是这样的:
1:通过 Data Source Explorer 连接到我的数据库。确保连接处于正确状态,进入表并点击刷新。在连接上点击刷新不会刷新表。
2:可能是可选的,但我使用 JPA Tools 将架构导出到数据库。再次按步骤 1 中的方式点击刷新。
3:使用实体和表注释实体。在实体中添加名称,在表注释中添加 NAME、CATALOG 和 SCHEMA。
4:手动运行验证。添加目录很重要。它非常挑剔。
I had this error and was able to get it working through a very tortuous effort. I am using 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.
2: Probably optional, but I used JPA Tools to export the schema to the database. Again, I hit refresh as in step 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.
4: Run validation manually. Adding the catalog was important. It's very picky.
使用 maxmcbyte 答案的另一个选项是仅为特定项目设置它。右键单击该项目并选择验证。启用项目特定设置并禁用 JPA Validator for Build。
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 验证。查看图片...
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 off JPA Validation in the Build Column. See the Image...