2

Hello I have got problem with enabling auto generating database tables from java models @Entity classes. I have tried almost everything.

I have created maven project in Intellij Idea at first I have downloaded JavaEE 8 api implementation which I am using.

I am using Glassfish 5.0 server as a java ee 8 implementation provider and MySQL database.

My Project structure:

enter image description here

My pom.xml looks like:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.zerg.web</groupId>
<artifactId>PlanYourTime</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
    <!-- https://mvnrepository.com/artifact/javax/javaee-api -->
    <dependency>
        <groupId>javax</groupId>
        <artifactId>javaee-api</artifactId>
        <version>8.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

My model @Entity class wchich I want to generate tables in my MySQL database.

package model;

import javax.persistence.*;
@Entity
@TableGenerator(name = "USER")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "USERID")
private int userId;
@Column(name = "USERNAME")
private String username;
@Column(name = "PASSWORD")
private String password;
@Column(name = "EMAIL")
private String email;

public User(String username, String password, String email) {
    this.username = username;
    this.password = password;
    this.email = email;
}

public User() {
}

public int getUserId() {
    return userId;
}

public void setUserId(int userId) {
    this.userId = userId;
}

public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}
}

My persistence.xml file src/main/java/META-INF/ looks like:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_2.xsd"
         version="2.2">

<persistence-unit name="myPu" transaction-type="JTA">
    <jta-data-source>jdbc/PlanYourTime</jta-data-source>
    <properties>
        <property name="javax.persistence.schema-generation.database.action" value="create"/>
        <property name="javax.persistence.schema-generation.create-source" value="metadata"/>
        <property name="javax.persistence.schema-generation.drop-source" value="metadata"/>
        <property name="javax.persistence.sql-load-script-source" value="META-INF/load-script.sql"/>
        <property name="eclipselink.logging.level" value="FINE"/>
        <property name="eclipselink.ddl-generation" value="create-or-extend-tables"/>
    </properties>
</persistence-unit>

And in my glassfish server admin page I have also added Jdbc resourcure and jdbc connection pool like this:

enter image description here

enter image description here

And my war artifact structure looks like in IntellijIdea which I deploy to glassfish 5.0 server:

enter image description here

When i was checking server.log for glassfish I don't get any errors.

I will be very thankful for every help.

Robert Columbia
  • 6,313
  • 15
  • 32
  • 40
Ppyyt
  • 125
  • 2
  • 9
  • 1
    What problems do you have, if any, with your deployed application hitting the database? If the table isn't there, all CRUD operations should fail with exceptions. – Chris Mar 04 '19 at 19:15
  • I have just added Stateless class with PersistenceContext EntityManager. This class is doing nothing but just declarations and now all model Entity classes started generating automatically. Thank for all help. – Ppyyt Mar 05 '19 at 11:56
  • 1
    Please don't edit your answer into your question. Instead, post it as an answer so others can vote on it. I have rolled back your recent edit. – Robert Columbia Mar 05 '19 at 12:37

3 Answers3

1

Did you created/used entity manager? If not, persistence module did not start. Make a insert using entity manager, or try:

Use eclipselink.deploy-on-startup to configure deployment on startup (at the creation of the EntityManagerFactory) instead of occurring the first time an EntityManager is created.

https://www.eclipse.org/eclipselink/documentation/2.4/jpa/extensions/p_deploy_on_startup.htm#delayonstartup

Peter Šály
  • 2,848
  • 2
  • 12
  • 26
  • I have tried with this also. I need first to download this implementation for example in pom.xml but this still doesn't work. I think glass fish container should have his own Eclipe Link provider for JPA and this is not needed in persistance.xml – Ppyyt Mar 03 '19 at 16:44
  • Yes I have copied JDBC Driver for MySQL (Connector/J) to glassfish container without this jar I couldnt do succesful ping in JDBC connection pool in Glassfish server administrator page. – Ppyyt Mar 03 '19 at 17:09
  • I added this line to my persistence.xml and nothing changed, everything deployed sucessfully and this table still didn't add in my database. – Ppyyt Mar 03 '19 at 17:14
  • 1
    see https://stackoverflow.com/questions/44861694/eclipselink-mysql-glassfish-table-is-not-created-even-though-connection-works – Peter Šály Mar 03 '19 at 17:15
  • Ok I will try this in a moment and check for results thanks for help. – Ppyyt Mar 03 '19 at 17:21
1

Glassfish hijacks the EclipseLink DDL generation functionality to force it to write out scripts, taking control of the schema generation process more directly. This works great for most cases, but not when you want to use the EclipseLink "create-or-extend-tables" which doesn't fit into script generation.

See https://stackoverflow.com/a/13388317/496099

Chris
  • 20,138
  • 2
  • 29
  • 43
  • You mean by that this property for auto generating tables in database from @Entity classes doesnt work in Eclipse JPA provider like it is working in Hibernate JPA provider? – Ppyyt Mar 04 '19 at 18:30
  • Not quite. Schema generation works just fine. I mean that Glassfish is injecting a property so it can control schema generation itself - this allows it to manage the schema's through its own server UI. This is fine, but it doesn't play nice with the eclipselink.ddl-generation property. So Glassfish is creating the tables, but cannot extend them, and the property it then injects into EclipseLink (it is a container managed context) overrides the setting you have put into your persistence.xml, preventing EclipseLink from extending them. – Chris Mar 04 '19 at 19:09
0

I have solved my problem by adding java class with EntityManager. Now tables are creating automatically form my java @Entity models into my database.

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class Test{
     @PersistenceContext
     private EntityManager em;

}
Ppyyt
  • 125
  • 2
  • 9