5

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

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
Arindam
  • 79
  • 1
  • 2
  • 5

5 Answers5

3

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)

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
  • I have these properties set in my persistence.xml. But it does not work. Then I changed the jpa provider to hibernate and used "hibernate.hbm2ddl.auto" value="create" in my persistence.xml. I wrote a small java class to create these tables as follows, but it throws an error, says "the project which referenced by the classpath does not exist. public class CreateTable { public static void main(String[] args) { Configuration cfg = new Configuration(); cfg.addAnnotatedClass(Event.class); cfg.configure(); new SchemaExport(cfg).create(true, true); – Arindam Jan 07 '11 at 10:56
  • I am sure I am doing something wrong. If you can point me to what I need to do, it'd be great. Alternatively, if there is any good resource/book etc available please point me to that. – Arindam Jan 07 '11 at 11:03
  • "the project which referenced by the classpath does not exist" this error says it all. You have some build path issues. it is hard to tell you what the real issue is without you sharing the code. share the code and I can help – Aravind Yarram Jan 07 '11 at 22:58
  • i had a similar situation it was the catalog property (check your entity mapping) – demonz demonz Nov 02 '12 at 19:20
  • check and do what demonz? – Ismail Sahin Aug 26 '16 at 08:11
2

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.

Artefacto
  • 96,375
  • 17
  • 202
  • 225
1

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.

j0k
  • 22,600
  • 28
  • 79
  • 90
Nougnold
  • 19
  • 1
1

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.

Armando
  • 459
  • 1
  • 8
  • 22
0

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>
JediCate
  • 396
  • 4
  • 8