Reputation: 125
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:
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:
And my war artifact structure looks like in IntellijIdea which I deploy to glassfish 5.0 server:
When i was checking server.log for glassfish I don't get any errors.
I will be very thankful for every help.
Upvotes: 2
Views: 742
Reputation: 125
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;
}
Upvotes: 0
Reputation: 21145
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
Upvotes: 1
Reputation: 2923
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.
Upvotes: 1