Get Date of Database with JPQL - jpa

I have the next code:
package co.com.patios.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
#Entity
public class Date {
private java.util.Date dateActual;
#Id
#Column(name = "date")
#Temporal(TemporalType.DATE)
public java.util.Date getDateActual() {
return dateActual;
}
public void setDateActual(java.util.Date dateActual) {
this.dateActual = dateActual;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
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_1.xsd">
<persistence-unit name="PatiosDS">
<jta-data-source>java:jboss/datasources/PatiosDS</jta-data-source>
<class>co.com.patios.entity.Date</class>
<class>co.com.patios.entity.EntradaVehiculoPatio</class>
<class>co.com.patios.entity.EstadoVehiculo</class>
<class>co.com.patios.entity.MarcaVehiculo</class>
<class>co.com.patios.entity.ModeloVehiculo</class>
<class>co.com.patios.entity.Patio</class>
<class>co.com.patios.entity.Usuario</class>
<class>co.com.patios.entity.Vehiculo</class>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.default_schema" value="public" />
<property name="hibernate.transaction.jta.platform"
value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" />
</properties>
</persistence-unit>
My problem is the annotation #Entity, that show the next message
Table "Date" cannot be resolved
where I have the next line "co.com.patios.entity.Date" in the my file persistence.xml
I want to know, what could be the problem ?
Note: The code run correct, but show the message
Table "Date" cannot be resolved
and it show in eclipse like error

Related

Unable to map List<String> by Hibernate in PostgreSql

I would like to do easy think - map list of String in database using hibernate.
I am able to do it when my hibernate.hbm2ddl.auto is set to create or create-drop.
If i change it to update it throws me an error during building Session Factory.
I checked that columns in db exists.
During debbuging it i found that in SchemaMigratorImpl.doMigrationToTargets Hibernate method
in field namespace primaryKey for table ITEM_POSITIONS is corrupted.
Does anyone know what i am doing wrong?
Stacktrace:
Exception in thread "main" javax.persistence.PersistenceException: [PersistenceUnit: HelloWorldPU] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:877)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:805)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:58)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:55)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
at util.HibernateUtil.getEntityManagerFactory(HibernateUtil.java:43)
at TestApp.main(TestApp.java:12)
Caused by: org.hibernate.exception.SQLGrammarException: Error accessing column metadata: ITEM_POSITIONS
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:95)
at org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl.convertSQLException(InformationExtractorJdbcDatabaseMetaDataImpl.java:71)
at org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl.getForeignKeys(InformationExtractorJdbcDatabaseMetaDataImpl.java:631)
at org.hibernate.tool.schema.extract.internal.TableInformationImpl.foreignKeys(TableInformationImpl.java:88)
at org.hibernate.tool.schema.extract.internal.TableInformationImpl.getForeignKey(TableInformationImpl.java:99)
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.findMatchingForeignKey(SchemaMigratorImpl.java:338)
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.applyForeignKeys(SchemaMigratorImpl.java:318)
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigrationToTargets(SchemaMigratorImpl.java:157)
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigration(SchemaMigratorImpl.java:59)
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:129)
at org.hibernate.tool.hbm2ddl.SchemaUpdate.execute(SchemaUpdate.java:97)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:481)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:444)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:802)
... 5 more
Caused by: org.postgresql.util.PSQLException: ERROR: column t1.tgconstrname does not exists
Pozycja: 113
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:374)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeQuery(AbstractJdbc2Statement.java:254)
at org.postgresql.jdbc2.AbstractJdbc2DatabaseMetaData.getImportedExportedKeys(AbstractJdbc2DatabaseMetaData.java:3373)
at org.postgresql.jdbc2.AbstractJdbc2DatabaseMetaData.getImportedKeys(AbstractJdbc2DatabaseMetaData.java:3566)
at org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl.getForeignKeys(InformationExtractorJdbcDatabaseMetaDataImpl.java:580)
... 16 more
Configuration:
Java 8
PostgreSql: postgresql-12.1-3-windows-x64
pom.xml:
<?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>hibernatemapping</groupId>
<artifactId>hibernatemapping</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.1-901.jdbc4</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>5.0.0.Final</version>
</dependency>
</dependencies>
</project>
persistence.xml:
<persistence
version="2.1"
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_2_1.xsd">
<persistence-unit name="HelloWorldPU">
<class>domain.Item</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.use_sql_comments" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/hibernate_db?useSSL=false"/>
<property name="javax.persistence.jdbc.user" value="postgres"/>
<property name="javax.persistence.jdbc.password" value="admin"/>
</properties>
</persistence-unit>
</persistence>
HibernateUtil Class which is responsible for creating EntityManager:
package util;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.cfg.Environment;
import org.hibernate.service.ServiceRegistry;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.util.Properties;
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static EntityManagerFactory entityManagerFactory;
public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
try {
Configuration configuration = new Configuration();
// Hibernate settings equivalent to hibernate.cfg.xml's properties
Properties settings = new Properties();
settings.put(Environment.DRIVER, "org.postgresql.Driver");
settings.put(Environment.URL, "jdbc:postgresql://localhost:5432/hibernate_db?useSSL=false");
settings.put(Environment.USER, "postgres");
settings.put(Environment.PASS, "admin");
settings.put(Environment.SHOW_SQL, "true");
settings.put(Environment.CURRENT_SESSION_CONTEXT_CLASS, "thread");
settings.put(Environment.HBM2DDL_AUTO, "auto-update");
configuration.setProperties(settings);
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()
.applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
} catch (Exception e) {
e.printStackTrace();
}
}
return sessionFactory;
}
public static EntityManagerFactory getEntityManagerFactory(){
if (entityManagerFactory == null) {
entityManagerFactory = Persistence.createEntityManagerFactory("HelloWorldPU");
}
return entityManagerFactory;
}
}
Entity class:
package domain;
import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.*;
import javax.persistence.Entity;
import javax.persistence.*;
import java.util.*;
#Entity
public class Item {
#Id
#GeneratedValue(generator = "ID_GENERATOR")
#GenericGenerator(name = "ID_GENERATOR",
strategy = "enhanced-sequence",
parameters = {
#Parameter(name = "sequence_name",
value = "Item_sequence")
})
private Long id;
private String name;
#ElementCollection
#CollectionTable(name = "ITEM_POSITIONS")
#OrderColumn
#Column(name = "POSITIONS")
protected List<String> positions = new ArrayList<>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<String> getPositions() {
return positions;
}
public void setPositions(List<String> positions) {
this.positions = positions;
}
}
Test class:
import domain.Item;
import util.HibernateUtil;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import java.util.ArrayList;
import java.util.List;
public class TestApp {
public static void main(String[] args){
EntityManagerFactory emf = HibernateUtil.getEntityManagerFactory();
EntityManager em = emf.createEntityManager();
EntityTransaction transaction = em.getTransaction();
transaction.begin();
Item item = new Item();
item.setName("Item1");
List<String> positions = new ArrayList<>();
positions.add("Position1");
positions.add("Position2");
positions.add("Position3");
positions.add("Position4");
positions.add("Position5");
item.setPositions(positions);
em.persist(item);
transaction.commit();
em.close();
emf.close();
}
}
How to reproduce:
Run with property name="hibernate.hbm2ddl.auto" value="create"
Run with property name="hibernate.hbm2ddl.auto" value="update"
Set breakpoint in QueryExecutorImpl.receiveErrorResponse
Search invocation stack for SchemaMigratorImpl.doMigrationToTargets
I solved this issue.
I added this line to my pertsistence.xml
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
And i changed driver to:
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.5</version>
</dependency>

Unable to build entity manager factory - Rest Tomcat Jpa

I'm making a Restful web service using netbeans, tomcat, jpa and jax-rs. I have this error since I've add the <class /> tags to my persistence.xml for all of my classes (And I need them to make a select).
The error is : https://gist.github.com/anonymous/bb37c28cdb3dbdf721c5206bfa6369c3
And my persistence.xml is :
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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_1.xsd">
<persistence-unit name="NataRestServicePU" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>model.Media</class>
<class>model.MediaTypeDB</class>
<class>model.Message</class>
<class>model.Observation</class>
<class>model.Session</class>
<class>model.Species</class>
<class>model.User</class>
<class>model.UserType</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3307/natagora?zeroDateTimeBehavior=convertToNull"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.password" value="password"/>
<property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
</properties>
</persistence-unit>
</persistence>
And this is the main dao (that worked before, didn't change anything) but without all the CRUD methods (just the read for example)
package dao;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
public class MainDAO<T> implements IMainDAO<T> {
protected Class<T> clazz;
private final EntityManagerFactory factory;
private static final String PERSISTENCE_UNIT_NAME = "NataRestServicePU";
#PersistenceContext(unitName = "NataRestServicePU")
protected EntityManager entityManager;
public MainDAO(Class<T> type){
clazz = type;
factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
}
public void setClazz(Class<T> clazzToSet) {
this.clazz = clazzToSet;
}
#Override
public T read(int id){
try{
newEntityManager();
return entityManager.find(clazz,id);
}finally{
closeEntityManager();
}
}
protected void closeEntityManager(){
entityManager.close();
}
protected void newEntityManager(){
entityManager = factory.createEntityManager();
}
}

JPA EntityManagerFactory don't override persistence.xml file

When I lunch the app it is not take these properties but the default properties in glassfish-ressource.xml. I'm using JPA with nebeans automatical generated beans and entities. I want switch database at runtime.
Here his my session class
package facade;
import entities.Tpe;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.PersistenceContext;
/**
*
* #author Emmanuel
*/
#Stateless
public class TpeFacade extends AbstractFacade<Tpe> {
private EntityManager em;
#Override
public EntityManager getEntityManager() {
EntityManagerFactory emf=null;
Map properties = new HashMap();
properties.put("javax.persistence.transactionType", "JTA");
properties.put("javax.persistence.jdbc.driver", "jdbc:mysql://192.20.3.81:3306/piv?zeroDateTimeBehavior=convertToNull");
properties.put("javax.persistence.jdbc.url", "com.mysql.jdbc.Driver");
properties.put("javax.persistence.jdbc.database", "piv");
properties.put("javax.persistence.jdbc.user", "username");
properties.put("javax.persistence.jdbc.password", "password");
try {
emf = Persistence.createEntityManagerFactory("ConfigurationTPEPU", properties);
System.out.println("emfznezizzhzz "+emf.getProperties() );
} catch (Exception e) {
}
em = (EntityManager) emf.createEntityManager();
return em ;
}
public TpeFacade() {
super(Tpe.class);
}
}
and my persistence file content
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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_1.xsd">
<persistence-unit name="ConfigurationTPEPU" transaction-type="JTA">
<jta-data-source>java:app/connexion81</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
</persistence-unit>
</persistence>
the glassfish-ressources.xml content
<resources>
<jdbc-connection-pool allow-non-component-callers="false" associate-with-thread="false" connection-creation-retry-attempts="0" connection-creation-retry-interval-in-seconds="10" connection-leak-reclaim="false" connection-leak-timeout-in-seconds="0" connection-validation-method="auto-commit" datasource-classname="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" fail-all-connections="false" idle-timeout-in-seconds="300" is-connection-validation-required="false" is-isolation-level-guaranteed="true" lazy-connection-association="false" lazy-connection-enlistment="false" match-connections="false" max-connection-usage-count="0" max-pool-size="32" max-wait-time-in-millis="60000" name="mysql_piv_rootPool" non-transactional-connections="false" pool-resize-quantity="2" res-type="javax.sql.DataSource" statement-timeout-in-seconds="-1" steady-pool-size="8" validate-atmost-once-period-in-seconds="0" wrap-jdbc-objects="false">
<property name="serverName" value="localhost"/>
<property name="portNumber" value="3306"/>
<property name="databaseName" value="db1"/>
<property name="User" value="root"/>
<property name="Password" value="gsmcom"/>
<property name="URL" value="jdbc:mysql://localhost:3306/petroivoire?zeroDateTimeBehavior=convertToNull"/>
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
</jdbc-connection-pool>
<jdbc-resource enabled="true" jndi-name="java:app/connexion81" object-type="user" pool-name="mysql_piv_rootPool"/>
</resources>
You should be understood that, for an #EJB the application server is here to takes care of the inetiation of the #EJB but also the injection of the persistence.
The configuration used is that already available in the Glassfish domain (domain/config/domain.xml) and not theses properties of glassfish-ressource.xml file.
This means, for override the first config you've to use two persistences and :
switch between them programmatically (when the both are injected).
Or use Persistence.createEntityManagerFactory( jndiDataSource2NdUnitName ).createEntityManager(); where jndiDataSource2NdUnitName is the 2nd unit name.

Why EntityManager have null value in my dao layer?

I'm working on a Java EE 7 maven project I'm using wildfly 8.2 everything is okey the problem is when I create an entity manager using #PersistenceContext inside managedbeans (backing beans) that I use with my jsf the contanier create a entit manager object and it's work but when I try to use the entity maanger inside my DAO Layer it's not work the em stay have a null value and I don't know why this my code in my dao layer can someone helpe me ? .
dao interface :
public interface ICategoryDao {
Category addCategory(Category category);
void deleteCategory(Long codeCategory);
Set<Category> getAllCategories();
Category updateCategory(Category category);
}
dao impl :
#Named("categoryDao")
public class CategoryDao implements ICategoryDao{
private Logger log = Logger.getLogger(CategoryDao.class);
#PersistenceContext(unitName="BooksStore")
private EntityManager em ;
#Override
public Category addCategory(Category category) {
if(em==null)
{
log.info("em is null ");
return category;
}
em.getTransaction().begin();
em.persist(category);
em.getTransaction().commit();
log.info("CategoryDao : Object persisted." );
return category;
}
#Override
public void deleteCategory(Long codeCategory) {
// TODO Auto-generated method stub
}
#Override
public Set<Category> getAllCategories() {
// TODO Auto-generated method stub
return null;
}
#Override
public Category updateCategory(Category category) {
// TODO Auto-generated method stub
return null;
}
}
this is my beans.xml
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" bean-discovery-mode="all">
</beans>
My persistance.xml file :
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 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_1.xsd">
<persistence-unit name="BooksStore" transaction-type="JTA">
<jta-data-source>java:/bookstore</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>
</persistence-unit>
</persistence>
Do you have a persistence.xml file ?
This is an example for persistence.xml file :
<?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="School_Manager" transaction-type="RESOURCE_LOCAL">
<class>YourEntityFQN</class>
<properties>
attribute name on persistence.xml file must be "BooksStore" in your case .
In class tag you need to specify all entity that you want use for this persistence-unit .

EclipseLink JPA Primary Key with Custom Default Value

I have a table in my SQL Server database where the primary key field is defined with NEWID() as the default value. The expectation is client need not pass the primary key field value and the SQL server will handle it.
While defining my model class at JPA I have to define this ID field with a generation type. I tried IDENTITY, TABLE and SEQUENCE Generator. Unfortunately I am getting an error as
Exception Description: Error preallocating sequence numbers.
The sequence table information is not complete..
My Persistence. XML is as below
<?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="LOB_Webservice" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.xyz.lob.model.jpa.OrderDetail</class>
<class>com.xyz.lob.model.jpa.OrderHeader</class>
<shared-cache-mode>NONE</shared-cache-mode>
<properties>
<property name="jboss.as.jpa.providerModule" value="org.eclipse.persistence"/>
<property name="javax.persistence.jdbc.driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:sqlserver://localhost:1433;databaseName=LOB_INT" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="*******" />
<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.sharedCache.mode" value="None"/>
<property name="eclipselink.jdbc.cache-statements" value="false" />
<property name="eclipselink.query-results-cache" value="false"/>
<property name="eclipselink.logging.exceptions" value="true"/>
<property name="eclipselink.weaving" value="static"/>
</properties>
</persistence-unit>
My Model class is as below
#Entity
public class OrderHeader implements Serializable {
#Id
#Basic(optional = false)
#GeneratedValue(strategy=GenerationType.AUTO)
#Column(name="OrderId")
private String orderId;
...
}
Hi #Joe2013 Not sure if is still an issue but when using Table Generators AND you did not specified for Eclipse Link to generate the schema based on your object model, you must manually create the table AND also insert the rows for the corresponding generators and their initial values.
Otherwise it will not work and you will get the error you mentioned.
I have provided custom and default Sequence ID Generator examples as below.
Use Custom Sequence ID Generator (EclipseLink only)
Define Custom Sequence class
package org.phstudy.sequence;
public class MyNewIDSequence extends Sequence implements SessionCustomizer {
private static final long serialVersionUID = -6308907478094680131L;
public MyNewIDSequence() {
super();
}
public MyNewIDSequence(String name) {
super(name);
}
public void customize(Session session) throws Exception {
MyNewIDSequence sequence = new MyNewIDSequence("mynewid");
session.getLogin().addSequence(sequence);
}
#Override
public Object getGeneratedValue(Accessor accessor, AbstractSession writeSession, String seqName) {
DataReadQuery query = new DataReadQuery("select NEWID()");
query.setResultType(DataReadQuery.VALUE);
return writeSession.executeQuery(query);
}
#Override
public Vector getGeneratedVector(Accessor accessor, AbstractSession writeSession, String seqName, int size) {
return null;
}
#Override
public void onConnect() { }
#Override
public void onDisconnect() { }
#Override
public boolean shouldAcquireValueAfterInsert() {
return false;
}
#Override
public boolean shouldUsePreallocation() {
return false;
}
#Override
public boolean shouldUseTransaction() {
return false;
}
}
Register custom sequence in persistence.xml
<persistence ...>
<persistence-unit ...>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
...
<property name="eclipselink.session.customizer" value="org.phstudy.sequence.MyNewIDSequence"/>
...
</persistence-unit>
</persistence>
Add Sequence Annotation with custom sequence name
#Entity
public class CustomSequence {
#Id
#GeneratedValue(generator = "mynewid")
private String id;
...
}
Use Default Sequence ID Generator (JPA, EclipseLink, Hibernate)
Please enable automatic schema generation or create table for storing ID manually when using Table, Sequence or IDENTITY ID Generation.
opt#1. Enable automatic schema generation
<persistence ...>
<persistence-unit ...>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<properties>
...
<property name="eclipselink.ddl-generation" value="create-tables"/>
...
</persistence-unit>
</persistence>
opt#2. Create table for storing ID manually
Define Sequence in your entity
#SequenceGenerator(name="Emp_Gen", sequenceName="Emp_Seq")
#Id #GeneratedValue(generator="Emp_Gen")
private int getId;
SQL script to create sequence
CREATE SEQUENCE Emp_Seq
MINVALUE 1
START WITH 1
INCREMENT BY 50 //allocation size