开发者

Problem with JPA Project in Eclipse - error in class annotated @Entity: Table "xxx" cannot be resolved

I'm开发者_StackOverflow中文版 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?


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


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.


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.


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


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.


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.


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

Problem with JPA Project in Eclipse - error in class annotated @Entity: Table "xxx" cannot be resolved

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜