开发者

eclipselink does not generate tables from annotated JPA classes

开发者 https://www.devze.com 2023-02-02 23:56 出处:网络
My IDE is eclipse -Helios and I am using mojarra jsf, mysql, eclipselink for jpa. In my project, if I create the tables manually in mysql, I can see those tables in the \"JPA Details\" view. And if I

My IDE is eclipse -Helios and I am using mojarra jsf, mysql, eclipselink for jpa.

In my project, if I create the tables manually in mysql, I can see those tables in the "JPA Details" view. And if I don't create any table, the eclipse IDE shows an error, "Table "trainingsession" cannot be resolved".

I am not sure what's wrong. When would JPA create these tables ? and how ? my persistence.xml is as follows,

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" 
 xmlns:xsi=开发者_如何学Python"http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
  <persistence-unit name="wompower2" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>trainer</jta-data-source>
    <class>com.jsfcompref.trainer.entity.User</class>
    <class>com.jsfcompref.trainer.entity.TrainingSession</class>
    <class>com.jsfcompref.trainer.entity.Event</class>
    <class>com.jsfcompref.trainer.entity.AbstractEntity</class>
    <validation-mode>NONE</validation-mode>
    <properties>
      <property name="eclipselink.target-database" value="MySQL"/>
      <property name="eclipselink.ddl-generation" value="create-tables"/>
      <property name="eclipselink.ddl-generation.output-mode" value="both"/>
      <property name="eclipselink.application-location" value="C:\wompower2\DDL"/>
      <property name="eclipselink.create-ddl-jdbc-file-name" value="create.sql"/>
      <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/demo"></property>
      <property name="javax.persistence.jdbc.user" value="user"></property>
      <property name="javax.persistence.jdbc.password" value="pwd"></property>
      <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"></property>
    </properties>
  </persistence-unit>
</persistence>

Thank you, Arindam.


First let me clarify that JPA is a standard spec for ORM and EclipseLink is one of the implementor of the spec (Hibernate is another example). The spec doesn't mandate the creation of the schema or tables though EclipseLink provides a mechanism to create the tables for you through configuration. Below are the two config properties controlling that

<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />

Go through this tutorial for more information (specifically 3.2 section)


GlassFish reads the property eclipselink.ddl-generation to decide whether it will use its own table generation functionality -- java2db. This only works during deployment and if the target is the DAS.

It doesn't matter the value of eclipselink.ddl-generation.output-mode you give, GlassFish will set it to "sql-script" if java2db is to be used (so that it can then run the scripts) or "none" if it is disabled. See glassfish3/persistence/jpa-connector/src/main/java/org/glassfish/persistence/jpa/PersistenceUnitLoader.java.


I saw "Table 'nnn' cannot be resolved" being reported after a couple of tests I made with JPA, MySQL, Eclipse.

My issue was caused by myself. I switched the data connection during my tests. But the tool - I assume I was the JPA plugin - was continuing to validate the table names against the first data connection i defined.

So, my solution was:

Open the project specific JPA properties (right click project -> JPA) and ensure that the connection settings refer to the correct database. Rebuild... that's it.


This is a bit late, but just in case anybody comes across this. You just need to run a query like an INSERT or a SELECT against the database and the tables will be created. It has worked for me before. I hope this help anybody with the same issue.


Just a persistence.xml example using eclipselink and mysql for others looking for a working solution:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
 http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

  <persistence-unit name="java2curs"  transaction-type="RESOURCE_LOCAL">    
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>db.YourClasses</class>      
  <properties>
    <property name="javax.persistence.jdbc.url" 
          value="jdbc:mysql://localhost:3306/dbname"/>
    <property name="javax.persistence.jdbc.user" value="dbuser"/>
    <property name="javax.persistence.jdbc.password" value="dbpassword"/>
    <property name="eclipselink.ddl-generation" value="create-tables" />
    <property name="eclipselink.ddl-generation.output-mode" value="database" />
    <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>   
   </properties>
 </persistence-unit>

</persistence>
0

精彩评论

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