Eclipse 中的 JPA 项目出现问题 - 注释为 @Entity 的类中出现错误:表“xxx”无法解决

发布于 2024-12-05 12:11:24 字数 557 浏览 4 评论 0 原文

我正在尝试在 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”
“日食如何知道”我的表是否存在?我还应该配置什么?

I'm trying to create simple EJB + JPA project in Eclipse (Indigo). I created new EJB project where:

  • 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)

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:

    <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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

GRAY°灰色天空 2024-12-12 12:11:24

找到了“Table xxx无法解析”错误的解决方案:

在Eclipse中,转到Window -> >首选项->验证,JPA Validator,关闭构建。

有时,您正在开发一个应用程序以及与之配套的新数据库架构,但您会得到以下结果:

无法解析表“XXXX”的架构“null”。

无论如何,这可能不应该默认打开;人们最有可能从头开始构建新应用程序,而不是构建新应用程序来适应旧数据库,即使他们确实从旧数据库构建,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:

Schema "null" cannot be resolved for table "XXXX".

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."

半城柳色半声笛 2024-12-12 12:11:24

在 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.

动听の歌 2024-12-12 12:11:24

我正在使用 Eclipse。我注意到,当我在 JPA 环境中使用“数据源资源管理器”窗口时,Eclipse 会识别表(可能不需要编译,但对于在同一 Eclipse IDE 中检查数据库很有用)。然后,当我在该窗口中创建到数据库的连接时,我在连接属性中遇到了更改数据的问题:

不工作(表无法解析):

Database: my_database
URL: jdbc:mysql://localhost:3306
User Name: jpa_user
Password: jpa_pass

已修复(我在 URL 字段中添加数据库):

Database: my_database
URL: jdbc:mysql://localhost:3306/my_database
User Name: jpa_user
Password: jpa_pass

记住使用 @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):

Database: my_database
URL: jdbc:mysql://localhost:3306
User Name: jpa_user
Password: jpa_pass

Fixed (I add database in URL field):

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.

平定天下 2024-12-12 12:11:24

您需要在 persistence.xml 文件中指定在何处查找实体,请查看 Java EE 6 教程,以下是取自该教程的示例:

持久性单位

持久化单元定义了所有实体的集合
由应用程序中的 EntityManager 实例管理的类。
这组实体类代表包含在一个实体类中的数据
单一数据存储。

持久化单元由 persistence.xml 配置定义
文件。以下是 persistence.xml 文件示例:

 <持久>
   
      <描述>该单元管理订单和客户。
            它不依赖于任何特定于供应商的功能,并且可以
            因此可以部署到任何持久性提供者。
      
      jdbc/MyOrderDB
      MyOrderApp.jar
      com.widgets.Order;
      com.widgets.Customer
  
 

该文件定义了一个持久化
名为 OrderManagement 的单元,它使用 JTA 感知的数据源:
jdbc/MyOrderDB。 jar-file 和 class 元素指定托管
持久化类:实体类、可嵌入类和映射类
超类。 jar-file 元素指定 JAR 文件
对于包含托管的打包持久性单元可见
持久化类,而类元素显式命名
托管持久性类。

另请使用 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:

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.

Also take a look at this stackoverflow question using hibernate you can scan for your classes that have the Entity annotation

白芷 2024-12-12 12:11:24

我遇到了这个错误,并通过非常曲折的努力才使其正常工作。我正在使用 PostgreSQL。

首先,我按照上面的建议进行操作并关闭了构建验证。然后我手动运行验证以连接事物。我的步骤基本上是这样的:

1:通过 Data Source Explorer 连接到我的数据库。确保连接处于正确状态,进入表并点击刷新。在连接上点击刷新不会刷新表。

2:可能是可选的,但我使用 JPA Tools 将架构导出到数据库。再次按步骤 1 中的方式点击刷新。

3:使用实体和表注释实体。在实体中添加名称,在表注释中添加 NAME、CATALOG 和 SCHEMA。

@Entity(name="DBAssembly")
@Table(name="DBAssembly",catalog="lag",schema="public")

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.

@Entity(name="DBAssembly")
@Table(name="DBAssembly",catalog="lag",schema="public")

4: Run validation manually. Adding the catalog was important. It's very picky.

⒈起吃苦の倖褔 2024-12-12 12:11:24

使用 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.

任性一次 2024-12-12 12:11:24

它有效,但是,除了 ma​​xmcbyte(谢谢!!)答案之外,有时最好仅针对某些项目应用此解决方案,通过访问:项目属性 > 单独执行此操作。验证>在构建列打开“启用项目特定设置”,然后关闭 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...

Config

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文