DerbyDB GlassFish simple example SEQUENCE doesn't exist - derby

I'm trying to run simple JPA example from book Beginning_Java_EE_6_with_GlassFish_3(DerbyDB + eclipselink + maven).
The problem is the same as in question
The same question
But my persistence configuration presented bellow:
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
version="2.0">
<persistence-unit name="chapter02PU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/__default</jta-data-source>
<class>com.apress.javaee6.chapter02.Book</class>
<properties>
<property name="elipselink.target-database" value="DERBY"/>
<property name="elipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="elipselink.logging.level" value="ALL"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/chapter02DB;create=true"/>
<property name="javax.persistence.jdbc.user" value="APP"/>
<property name="javax.persistence.jdbc.password" value="APP"/>
</properties>
</persistence-unit>
</persistence>
here is the main class:
package com.apress.javaee6.chapter02;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
public class Main {
public static void main(String[] args) {
Book book = new Book.BookBuilder()
.title("The Chitchiker's description to Galaxy")
.price(12.5F)
.description("Since fiction comedy book")
.isbn("1-84023-666")
.numOfPage(354)
.illustration(false)
.build();
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("chapter02PU");
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
entityManager.persist(book);
transaction.commit();
entityManager.close();
entityManagerFactory.close();
}
}
And the Book.class
#Entity
#NamedQuery(name = "findAllBooks", query = "select b from Book b")
public class Book {
public Book() {
}
#Id #GeneratedValue
private Long id;
#Column(nullable = false)
private String title;
private Float price;
#Column(length = 2000)
private String description;
private String isbn;
private Integer numOfPage;
private Boolean illustration;
So I also looked up several questions(how to setup JTA datasource) about Glassfish and JDBC Resource, but they are presented in GlassFish console(jdbc/__default).
So finally when I run my program with command
mvn exec:java -Dexec.mainClass="com.apress.javaee6.chapter02.Main"
My pom.xml is
<?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.apress.javaee6</groupId>
<artifactId>chapter02</artifactId>
<version>1.0</version>
<name>chapter02</name>
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.0</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derbyclient</artifactId>
<version>10.10.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<version>10.10.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<inherited>true</inherited>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
I get an error
[ERROR] Internal Exception: java.sql.SQLSyntaxErrorException: Table/View 'SEQUEN
CE' does not exist.
[ERROR] Error Code: 20000
[ERROR] Call: UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?
[ERROR] bind => [2 parameters bound]
[ERROR] Query: DataModifyQuery(name="SEQUENCE" sql="UPDATE SEQUENCE SET SEQ_COUN
T = SEQ_COUNT + ? WHERE SEQ_NAME = ?")
[ERROR] -> [Help 1]
[ERROR]
I search for related questions but I can't recognize where my mistake is. Can someone help me?
UPD1
I enabled logs in derbyDB:
Database Class Loader started - derby.database.classpath=''
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 166), (SESSIONID = 0), (DATABASE = chapter02DB), (DRDAID = {1}), Committing
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 166), (SESSIONID = 0), (DATABASE = chapter02DB), (DRDAID = {1}), Rolling back
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 167), (SESSIONID = 1), (DATABASE = chapter02DB), (DRDAID = {1}), Committing
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 167), (SESSIONID = 1), (DATABASE = chapter02DB), (DRDAID = {1}), Committing
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 167), (SESSIONID = 1), (DATABASE = chapter02DB), (DRDAID = ????????.????-4255337276300706295{1}), Committing
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 167), (SESSIONID = 1), (DATABASE = chapter02DB), (DRDAID = ????????.????-4255337276300706295{1}), Rolling back
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 167), (SESSIONID = 1), (DATABASE = chapter02DB), (DRDAID = ????????.????-4255337276300706295{1}), Rolling back
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 168), (SESSIONID = 2), (DATABASE = chapter02DB), (DRDAID = {2}), Committing
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 168), (SESSIONID = 2), (DATABASE = chapter02DB), (DRDAID = {2}), Rolling back
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 169), (SESSIONID = 3), (DATABASE = chapter02DB), (DRDAID = {2}), Committing
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 169), (SESSIONID = 3), (DATABASE = chapter02DB), (DRDAID = {2}), Committing
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 170), (SESSIONID = 3), (DATABASE = chapter02DB), (DRDAID = ????????.????-4254774326347276936{2}), Begin compiling prepared statement: UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ? :End prepared statement
Sun Jan 18 23:56:39 MSK 2015 Thread[DRDAConnThread_2,5,main] (XID = 170), (SESSIONID = 3), (DATABASE = chapter02DB), (DRDAID = ????????.????-4254774326347276936{2}), Error compiling prepared statement: UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ? :End prepared statement

it was the mistake in persistance.xml
Instead of
<properties>
<property name="elipselink.target-database" value="DERBY"/>
<property name="elipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="elipselink.logging.level" value="ALL"/>
Should be
<properties>
<property name="eclipselink.target-database" value="DERBY"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.logging.level" value="ALL"/>
Enable transactional logs in derby DB really helps me (Dderby.language.logStatementText=true). From this logs I saw that no tables are created on start up

Related

Spring MVC sub mapping #RequestMapping throws 404

BE GENEROUS AND TELL US WHY I AM HAVING THIS SILLY ISSUE
Everything works fine with no mapping! but I have a simple issue. I have set a parent mapping # "/hello" and sub-mapped a method # "/showForm", Dispatcher is set to "/" but I get 404 (see code fig. 3). I couldn't find an answer to an issue SIMILAR to mine.
See code below & TAGS for my setup
Controller fig.1
#Controller
#RequestMapping("/hello")
public class HelloWorldController {
#RequestMapping("/showForm")
public String showForm() {
return "hello-world";
}
web.xml fig.2
<servlet>
<servlet-name>yktech</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>yktech</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
Tomcat Error fig.3
Type Status Report
Message /yktech/hello/WEB-INF/view/hello-world.jsp
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
EDIT: FORGOT THE BEAN FIG. 4
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="WEB-INF/view/"/>
<property name="suffix" value=".jsp"/>
</bean>
EDIT: STRUCTURE TREE ( MESSY I KNOW )
enter image description here
edit 4 , index.jsp
<html>
<body>
<h2>Welcome to my homepage
</h2>
Show form
Show STUDENT form
</body>
</html>
edit 5 pom.xml
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.yktech</groupId>
<artifactId>yktech</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>yktech</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.17.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<finalName>yktech</finalName>
</build>
</project>
EDIT 6 , SERVER LOG , MAPPING URLS
INFO: Loading XML bean definitions from ServletContext resource [/WEB-INF/yktech-servlet.xml]
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/processForm] onto handler 'helloWorldController'
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/processForm.*] onto handler 'helloWorldController'
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/processForm/] onto handler 'helloWorldController'
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/processFormVersionTwo] onto handler 'helloWorldController'
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/processFormVersionTwo.*] onto handler 'helloWorldController'
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/processFormVersionTwo/] onto handler 'helloWorldController'
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/showForm] onto handler 'helloWorldController'
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping registerHandler
INFO: Mapped URL path [/hello/showForm.*] onto handler 'helloWorldController'
Jul 30, 2018 10:19:30 PM org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping
registerHandler
INFO: Mapped URL path [/hello/showForm/] onto handler 'helloWorldController'
... CARRIES ON SHOWS NO ERRORS....
Try this:
Requests to /hello will be handled by whatever() while request to /hello/showForm will be handled by showForm()
#Controller
#RequestMapping("/hello")
public class HelloWorldController {
#RequestMapping("/")
public String whatever() {
return "whatever";
}
#RequestMapping("/showForm")
public String showForm() {
return "hello-world";
}
Issue is in the view resolver bean,
the prefix does not have a "/" in the beginning causing Spring to call any parented mapping requests from root rather than from /WEB-INF/view

lookup method - Injecting a prototype bean into a singleton bean issue

I am developing code for the Injecting a prototype bean into a singleton bean code, so far I developed code like below and when I run the main method, I see the below error is coming.
Jan 04, 2017 2:59:41 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#6ed322: startup date [Wed Jan 04 14:59:41 IST 2017]; root of context hierarchy
Jan 04, 2017 2:59:41 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Constructor:: RequestProcessor instance created!
Constructor:: RequestProcessor instance created!
Request ID : 1212
Exception in thread "main" java.lang.AbstractMethodError: com.injection.testing.RequestProcessor.createValidator()Lcom/injection/testing/RequestValidator;
at com.injection.testing.RequestProcessor.handleRequest(RequestProcessor.java:12)
at com.injection.testing.MainDemo.main(MainDemo.java:13)
spring.xml
<!-- Lookup way -->
<bean id="requestProcessor" class="com.injection.testing.RequestProcessor" >
<lookup-method name="getValidator" bean="validator" />
</bean>
<bean id="validator" class="com.injection.testing.RequestValidator" scope="prototype" />
RequestProcessor.java
public abstract class RequestProcessor {
private RequestValidator validator;
public RequestProcessor(){
System.out.println("Constructor:: RequestProcessor instance created!");
}
public void handleRequest(String requestId){
System.out.println("Request ID : "+ requestId);
RequestValidator validator = createValidator(); //here Spring will create new instance of prototype bean
validator.validate(requestId);
}
public RequestValidator getValidator() {
return validator;
}
public void setValidator(RequestValidator validator) {
this.validator= validator;
}
protected abstract RequestValidator createValidator();
}
RequestValidator.java
public class RequestValidator {
private List<String> errorMessages = new ArrayList<String>();
public RequestValidator() {
System.out.println("Constructor:: RequestValidator instance created!");
}
// Validates the request and populates error messages
public void validate(String requestId){
System.out.println("RequestValidator :"+requestId);
}
public List<String> getErrorMessages() {
return errorMessages;
}
}
MainDemo.java
public class MainDemo {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
//RequestValidator requestValidator = (RequestValidator) context.getBean("validator");
RequestProcessor processor = (RequestProcessor) context.getBean("requestProcessor");
processor.handleRequest("1212");
System.out.println("------------------------");
processor.handleRequest("1213");
}
}
pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>3.2.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
Update your spring.xml to shown below
<bean id="requestProcessor" class="com.injection.testing.RequestProcessor">
<lookup-method name="createValidator" bean="validator" />
</bean>
Basically you need to specify the name of the abstract method name in lookup-method xml attribute.
I got below output after the corrected configuration
Jan 04, 2017 4:11:20 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext#13c675d: startup date [Wed Jan 04 16:11:20 IST 2017]; root of context hierarchy
Jan 04, 2017 4:11:20 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [spring.xml]
Constructor:: RequestProcessor instance created!
Request ID : 1212
Constructor:: RequestValidator instance created!
RequestValidator :1212
Request ID : 1213
Constructor:: RequestValidator instance created!
RequestValidator :1213

EJB Weblogic JPA java.lang.ClassCastException: weblogic.jndi.internal.WLEventContextImpl cannot be cast to javax.sql.DataSource

I'm trying to make an EJB-JPA project, in my EJB module , the persistence unit
<?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="DB">
<jta-data-source>jdbc/db</jta-data-source>
<class>com.demo.entity.TestDept</class>
<class>com.demo.entity.TestEmployee</class>
</persistence-unit>
</persistence>
I've tested the datasource on weblogic, it's working. The daos are like this one .
#Stateless
public class TestEmployeeDao implements TestEmployeeDaoRemote {
#PersistenceContext(unitName = PersistenceUnits.DB_DATA_SOURCE)
private EntityManager em;
//....
}
I inject the dao this way
#EJB
TestEmployeeDaoRemote testEmployeeDao;
I get this error
<Sep 5, 2016 5:06:30 PM EEST> <Warning> <Deployer> <BEA-149004> <Failures were detected while initiating deploy task for application "com.Demo-ear_ear_1.0-SNAPSHOT".>
<Sep 5, 2016 5:06:30 PM EEST> <Warning> <Deployer> <BEA-149078> <Stack trace for message 149004
weblogic.management.DeploymentException: java.lang.ClassCastException: weblogic.jndi.internal.WLEventContextImpl cannot be cast to javax.sql.DataSource
at weblogic.application.internal.BaseDeployment.throwAppException(BaseDeployment.java:123)
at weblogic.application.internal.BaseDeployment.prepare(BaseDeployment.java:239)
at weblogic.application.internal.EarDeployment.prepare(EarDeployment.java:61)
at weblogic.application.internal.DeploymentStateChecker.prepare(DeploymentStateChecker.java:158)
at weblogic.deploy.internal.targetserver.AppContainerInvoker.prepare(AppContainerInvoker.java:61)
Truncated. see log file for complete stacktrace
Caused By: java.lang.ClassCastException: weblogic.jndi.internal.WLEventContextImpl cannot be cast to javax.sql.DataSource
at weblogic.persistence.BasePersistenceUnitInfo.lookUpDataSource(BasePersistenceUnitInfo.java:308)
at weblogic.persistence.BasePersistenceUnitInfo.lookUpDataSources(BasePersistenceUnitInfo.java:298)
at weblogic.persistence.BasePersistenceUnitInfo.init(BasePersistenceUnitInfo.java:105)
at weblogic.persistence.BaseJPAIntegrationProvider.createPersistenceUnitInfo(BaseJPAIntegrationProvider.java:53)
at weblogic.persistence.AbstractPersistenceUnitRegistry.storeDescriptors(AbstractPersistenceUnitRegistry.java:420)
Truncated. see log file for complete stacktrace
>
I can't understand the problem. Any help?

JPA merge doesn't update table

I'm facing this problem since a week, I hope someone will have a solution for me.
When I make a merge the line in database is not updated.
In the logs, I see the update which is send to Oracle
[EL Finest]: transaction: 2016-02-22 11:14:19.069--UnitOfWork(2109200496)--Thread(Thread[qtp1457417579-16,5,main])--Merge clone with references com.leray.test.User#610b894a
[EL Finer]: transaction: 2016-02-22 11:14:19.069--UnitOfWork(2109200496)--Thread(Thread[qtp1457417579-16,5,main])--begin unit of work flush
[EL Finest]: query: 2016-02-22 11:14:19.07--UnitOfWork(2109200496)--Thread(Thread[qtp1457417579-16,5,main])--Execute query UpdateObjectQuery(com.leray.test.User#610b894a)
[EL Finest]: connection: 2016-02-22 11:14:19.071--ServerSession(1907961337)--Connection(307503319)--Thread(Thread[qtp1457417579-16,5,main])--Connection acquired from connection pool [default].
[EL Finer]: transaction: 2016-02-22 11:14:19.071--ClientSession(925275938)--Connection(307503319)--Thread(Thread[qtp1457417579-16,5,main])--begin transaction
[EL Fine]: sql: 2016-02-22 11:14:19.071--ClientSession(925275938)--Connection(307503319)--Thread(Thread[qtp1457417579-16,5,main])--UPDATE USER SET LIB = ? WHERE (CODE = ?)
bind => [ModifiedLib, COD1]
[EL Finer]: transaction: 2016-02-22 11:14:19.099--UnitOfWork(2109200496)--Thread(Thread[qtp1457417579-16,5,main])--end unit of work flush
[EL Finer]: transaction: 2016-02-22 11:14:19.099--UnitOfWork(2109200496)--Thread(Thread[qtp1457417579-16,5,main])--resume unit of work
[EL Finer]: transaction: 2016-02-22 11:14:19.099--UnitOfWork(2109200496)--Thread(Thread[qtp1457417579-16,5,main])--begin unit of work commit
[EL Finer]: transaction: 2016-02-22 11:14:19.1--ClientSession(925275938)--Connection(307503319)--Thread(Thread[qtp1457417579-16,5,main])--commit transaction
[EL Finest]: connection: 2016-02-22 11:14:19.142--ServerSession(1907961337)--Connection(307503319)--Thread(Thread[qtp1457417579-16,5,main])--Connection released to connection pool [default].
[EL Finer]: transaction: 2016-02-22 11:14:19.143--UnitOfWork(2109200496)--Thread(Thread[qtp1457417579-16,5,main])--end unit of work commit
[EL Finer]: transaction: 2016-02-22 11:14:19.143--UnitOfWork(2109200496)--Thread(Thread[qtp1457417579-16,5,main])--resume unit of work
The same statement in SQL Developper is working fine with the same user.
I have this issue only for one table. I didn't saw something special for this table, a trigger exist but I don't think it could explain my problem.
My code is compile whith Java 1.8 and I'm using theses version JPA and Oracle.
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.1.1</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc7</artifactId>
<version>12.1.0.2</version>
</dependency>
The object generated with Eclipse is quite simple.
package com.leray.test;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
/**
* The persistent class for the User database table.
*
*/
#Entity
#NamedQuery(name="User.findAll", query="SELECT e FROM User e")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
#Id
private String code;
private String ecran;
private String lib;
private String mdp;
public User() {
}
public String getCode() {
return this.code;
}
public void setCode(String code) {
this.code = code;
}
public String getEcran() {
return this.ecran;
}
public void setEcran(String ecran) {
this.ecran = ecran;
}
public String getLib() {
return this.lib;
}
public void setLib(String lib) {
this.lib = lib;
}
public String getMdp() {
return this.mdp;
}
public void setMdp(String mdp) {
this.mdp = mdp;
}
}
My Persistence.xml
<?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="test">
<class>com.leray.test.User</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:#server:1521:siddb"/>
<property name="javax.persistence.jdbc.user" value="****"/>
<property name="javax.persistence.jdbc.password" value="****"/>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
<property name="eclipselink.logging.level" value="ALL"/>
<property name="eclipselink.logging.exceptions" value="true"/>
<property name="eclipselink.logging.session" value="true"/>
<property name="eclipselink.logging.thread" value="true"/>
<property name="eclipselink.logging.timestamp" value="true"/>
<property name="eclipselink.logging.connection" value="true"/>
<property name="eclipselink.weaving" value="false"/>
</properties>
</persistence-unit>
</persistence>
My code to call the merge
public final void miseAJour(Evuti evuti) {
em.getTransaction().begin();
em.merge(evuti);
em.flush();
em.getTransaction().commit();
}
Update 2016-02-23 : I've check with the DBA, I saw the update statement on Oracle side but he can't confirm parameters send by eclipselink. I still have no idea to solve the problem.
I've found the root cause and the solution.
The problem come from usage of CHAR instead of VARCHAR.
A quick sample if you want to try.
CREATE TABLE SHORT_TEST
( "CODE" CHAR(10 BYTE),
"LIB" CHAR(30 BYTE)
)
TABLESPACE "DATADBS" ;
INSERT INTO SHORT_TEST VALUES ('LIB1','COD1');
-- 1 line inserted
UPDATE SHORT_TEST SET LIB = 'LIB2' WHERE (CODE = 'COD1');
-- You get 1 line updated
-- Now, try this update which is used by eclipselink
UPDATE SHORT_TEST SET LIB = :1 WHERE (CODE = :2);
-- 0 line updated
When you use a query parameter on a char column, you have to respect the column size. In this sample, if you fill COD1 with six spaces, the query will work.
Unfortunately, I have to work with an old Oracle database and I've no right to change the structure.
By default eclipselink perform a trim on fields.
That's why for Oracle 'COD1' doesn't match with 'COD1 '.
Eclipselink offer a possibility to disabled the automatic trim.
https://wiki.eclipse.org/EclipseLink/FAQ/JPA#How_to_avoid_trimming_the_trailing_spaces_on_a_CHAR_field.3F
So, in my project, I've created a new class with this code.
package com.leray.test;
import org.eclipse.persistence.dynamic.DynamicHelper.SessionCustomizer;
import org.eclipse.persistence.sessions.Session;
public class JpaSessionCustomizer extends SessionCustomizer {
public void customize(Session session) {
session.getLogin().setShouldTrimStrings(false);
}
}
And I've added this property to persistence.xml
<property name="eclipselink.session.customizer" value="com.leray.test.JpaSessionCustomizer"/>
Now Eclipselink return datas with the end spaces and the update in Oracle is working.

Issues with Oracle JDBC with Hibernate... Sometimes

I'm having a bit of a unique issue.
I'm able to successfully connect and manage entities when running JUnit tests, but once I start my actual application, I get "Specified JDBC Driver oracle.jdbc.OracleDriver class not found."
What confuses me is that it is there. It works when running my JUnit Tests.
Any insights are appreciated!
hibernate.cfg.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="db">
<property name="hibernate.connection.driver_class">oracle.jdbc.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#host:port/db</property>
<property name="hibernate.connection.username">username</property>
<property name="hibernate.connection.password">password</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.default_schema">db</property>
<property name="show_sql">true</property>
<mapping resource="org/entity/RunResultEntity.hbm.xml"/>
<mapping resource="org/entity/TransactionResultEntity.hbm.xml"/>
<mapping resource="org/entity/FailureResultEntity.hbm.xml"/>
</session-factory>
</hibernate-configuration>
HibernateUtil.java
package org.util;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.*;
public class HibernateUtil {
private static SessionFactory sessionFactory = buildSessionFactory();
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration();
configuration.configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(
configuration.getProperties()
).buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
} catch (Throwable ex) {
// Exception thrown here!
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
public static void shutdown() {
// Close caches and connection pools
getSessionFactory().close();
}
}
pom.xml (dependency added to local repository)
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.3.0</version>
<scope>provided</scope>
</dependency>
log
Oct 09, 2014 3:02:58 PM org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
Oct 09, 2014 3:02:58 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.0.1.Final}
Oct 09, 2014 3:02:58 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Oct 09, 2014 3:02:58 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Oct 09, 2014 3:02:58 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Oct 09, 2014 3:02:58 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Oct 09, 2014 3:02:58 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: org/entity/RunResultEntity.hbm.xml
Oct 09, 2014 3:02:59 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: org/entity/TransactionResultEntity.hbm.xml
Oct 09, 2014 3:02:59 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: org/entity/FailureResultEntity.hbm.xml
Oct 09, 2014 3:02:59 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: db
Oct 09, 2014 3:02:59 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Initial SessionFactory creation failed.org.hibernate.HibernateException: Specified JDBC Driver oracle.jdbc.OracleDriver class not found
Exception in thread "main" java.lang.ExceptionInInitializerError
I found my problem. Lower in my pom.xml I had this little snippet
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.1.Final</version>
<classifier>tests</classifier>
</dependency>
The classifier was only giving access to my test suite. Removing the classifier fixed the issue.
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.1.Final</version>
</dependency>

Resources