EclipseLink does not work on Netbeans, is this normal? - jpa

It'll be weeks that I'm stuck with EclipseLink. I can not persist an object in my database. I use netbeans 7.3. I encountered this problem when I started designing a web application. What follows is the approach I have adopted. It may be that I do without me realize a mistake.
After that netbeans has finished generating the project files I configured the jndi. Then I converted automatically with netbeans, the database tables in entity object.
here is the link
then, from these classes, I created their JPAController . (Always automatically with netbeans)
and finally, as a test, I just instantiate the description of "Outils" and leave the fields empty id. Since the latter automatically increment in the database, if the persistence is done well, I should have an id when I appear with out the console.
<body>
<h1>Hello World!</h1>
<%
Outils o = new Outils();
o.setDesignation("hammers");
EntityManagerFactory emf = Persistence.createEntityManagerFactory("Test_EclipseLinkPU");
UserTransaction utx = (UserTransaction) new InitialContext().lookup("java:comp/UserTransaction");
OutilsJpaController o_ctrl = new OutilsJpaController(utx, emf);
o_ctrl.create(o);
out.println("this is the id of hammer " + o.toString());
%>
</body>
and I get as result: Outils[ id=null ].
I have no error or on glassfish even less about the debugger.
Ps : Here are the 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="Test_EclipseLinkPU" transaction-type="JTA">
<jta-data-source>test_data_source</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties/>
</persistence-unit>
</persistence>
Thank you for your future is in your answers and listening for any additional information.

(Not really an answer but I couldn't show this in a comment)
To increase logging your persistence.xml should look like this:
<?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_EclipseLinkPU" transaction-type="JTA">
<jta-data-source>test_data_source</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.logging.level.sql" value="FINE"/>
<property name="eclipselink.logging.parameters" value="true"/>
</properties>
</persistence-unit>
</persistence>

You need to commit or flush the transaction before anything will be written to the database. If you are using IDENTITY sequencing (I strongly recommend not using IDENTITY, use TABLE or SEQUENCE if db supports it), then the Id cannot be allocated until the insert occurs, so a persist() will not assign the Id (as it does for TABLE and SEQUENCE).
What does your create() method do? How is your Id mapped?

Related

How to use 2 PersistenceUnits in one Persistence.xml in a JavaEE application?

I am trying to access 2 different tables in my database.
Initially my persistence.xml was as follows:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="Persistence">
<jta-data-source>jdbc/classicmodels</jta-data-source>
<class>com.tugay.maythirty.model.Customers</class>
<class>com.tugay.maythirty.model.Products</class>
<class>com.tugay.maythirty.model.Employee</class>
<class>com.tugay.maythirty.model.Office</class>
</persistence-unit>
</persistence>
So I defined a DataSource in Glassfish following this great artical and my application was working fine. I was using #PersistenceContext annotation in my DAO classes with name="Persistence"
This basically is connected to a table called "classicmodels".
Then I needed to fetch some data from a table called "sakila". I added these lines to my persistence.xml:
<persistence-unit name="sakila">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.tugay.maythirty.model.Actor</class>
<class>com.tugay.maythirty.model.Film</class>
<class>com.tugay.maythirty.model.FilmActor</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/sakila"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="password"/>
</properties>
</persistence-unit>
And I have used this code in my DAO:
public List<Actor> getAllActors(){
EntityManagerFactory emf = Persistence.createEntityManagerFactory("sakila");
EntityManager em = emf.createEntityManager();
TypedQuery<Actor> actorTypedQuery = em.createQuery("Select a from Actor a",Actor.class);
return actorTypedQuery.getResultList();
}
When I deploy my application to GlassFish however I started getting this Exception:
java.lang.RuntimeException: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [Persistence] in the scope of the module called [may-thrity]. Please verify your application.
So it seems that now my Persistence Unit with name "Persistence" is gone.
What is it that I am doing wrong?
Have you tried explicitly declaring your persistence unit with
#PersistenceContext(unitName="Persistence")
instead of
#PersistenceContext(name="Persistence")
PersistenceContext doc

How do I configure OpenJPA SQL logging?

What is the OpenJPA configuration to view SQL query executed in a database? I would like to view the query with all parameters executed in log or console instead of viewing the JPQL query
<property name="openjpa.Log" value="SQL=Trace" />
Enables logging of all SQL statements, minus parameter values.
<property name="openjpa.ConnectionFactoryProperties" value="PrintParameters=true" />
Enables logging of SQL parameters.
Logging documentation
If you're using log4j, you can setup your log4j.properties file as follows, which will display both the native SQL query and any parameters:
log4j.rootLogger=WARN, CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-5p %t %d{ISO8601} %l - %m%n
log4j.category.openjpa.jdbc.SQL=TRACE
To configure Open JPA for Log4J, you need to do the following in persistence xml
Open JPA Configurations
{'property name=”openJpa.Log” value=”log4j”'}
Log4j properties for Open JPA configs
log4j.logger.openjpa.Query=TRACE
log4j.logger.openjpa.jdbc.SQL=TRACE
http://openjpa.apache.org/builds/1.0.1/apache-openjpa-1.0.1/docs/manual/ref_guide_logging_log4j.html
Hi I want to add running persistance.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="EHS_PU">
<jta-data-source>mysqlDataSource</jta-data-source>
<class>com.ap.entity.EHSDo</class>
<class>com.ap.entity.EventDo</class>
<properties>
<property name="openjpa.Log" value="log4j" />
<property name="openjpa.ConnectionFactoryProperties" value="PrintParameters=true" />
</properties>
</persistence-unit>
</persistence>
In addition to Rick's answer, there is also the "openjpa.ConnectionFactory2Properties" property for connection factories used for unmanaged connections. (more details here: https://openjpa.apache.org/builds/1.2.3/apache-openjpa/docs/ref_guide_conf_openjpa.html#openjpa.ConnectionFactory2Properties)

Is it possible to set persistence unit default catalog at runtime

For the moment I use the table annotation containing the catalog
#Table(catalog = "Mycatalog", schema = "MySchema", name = "MyTable")
But the catalog name should be made configurable.
The persistence.xml file can not be changed per deployment, and the datasource default database should be set to TempDB. (Another process is locking the catalog quite often and the driver keeps a connection open to the datasource default database) so I am limited to change the default catalog using properties passed to the EntityManagerFactory.
EntityManagerFactory emf = provider.createEntityManagerFactory(
"default", properties);
Is it possible to set the persistence unit default catalog in the properties?
I am using eclipselink as JPA provider.
I was able to use different Catalogs as long as they are in the XML configuration files.
First I removed all #Table annotations from my entity classes and created two orm files.
persistence.xml:
<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 persistence_1_0.xsd"
version="1.0">
<persistence-unit name="storeone" transaction-type="RESOURCE_LOCAL">
<mapping-file>META-INF/orm-storeone.xml</mapping-file>
</persistence-unit>
<persistence-unit name="storetwo" transaction-type="RESOURCE_LOCAL">
<mapping-file>META-INF/orm-storetwo.xml</mapping-file>
</persistence-unit>
</persistence>
orm-storeone-xml:
<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
version="1.0">
<entity class="com.package.EntityClass">
<table catalog="SomeCatalog" schema="SomeSchema" name="SomeTable" />
</entity>
</entity-mappings>
And then in the code where I create my EntityManager I am able to choose between the two catalogs.
String persistencUnitName = "storeone";
EntityManagerFactory emf = provider.createEntityManagerFactory(
persistencUnitName , map);
this way I can change between catalogs at runtime, (but I still cannot add new catalogs at runtime).

Show generated SQL in toplink in eclipse

I am using EclipseLink libraries in eclipse (at dev time) and deploy on TopLink, I need to show the generated sql statement.
I am using the following persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="myPUnit" transaction-type="JTA">
<provider>
oracle.toplink.essentials.PersistenceProvider
</provider>
<jta-data-source>jdbc/dcds</jta-data-source>
<properties>
<property name="toplink.cache.shared.default" value="false"/>
<property name="toplink.logging.level" value="FINE" />
</properties>
</persistence-unit>
</persistence>
I know it should show generated sql statements, but this is not the case.
To see the SQL for a JPA Query you can enable logging on FINE or lower.
To get the SQL for a specific Query at runtime you can use the DatabaseQuery API.
Session session = em.unwrap(JpaEntityManager).getActiveSession();
DatabaseQuery databaseQuery = ((EJBQueryImpl)query).getDatabaseQuery();
databaseQuery.prepareCall(session, new DatabaseRecord());
String sqlString = databaseQuery.getSQLString();
This SQL will contain ? for parameters. To get the SQL translated with the arguments you need a DatabaseRecord with the parameter values.
Session session = em.unwrap(JpaEntityManager).getActiveSession();
DatabaseQuery databaseQuery = ((EJBQueryImpl)query).getDatabaseQuery();
String sqlString = databaseQuery.getTranslatedSQLString(session, recordWithValues);
Source: How to get the SQL for a Query
As a workaround, find generated SQLs here:
app_serv_home\j2ee\home\log\oc4j\log.xml
see:
http://m-hewedy.blogspot.com/2010/11/workaround-to-find-generated-sql-on-oas.html

Exception javax.persistence.PersistenceException: No Persistence provider for EntityManager

We've been working on this for days and we are stumped. This is supposed to be an easy tutortial using TopLink. We are trying to get this to work before we do our real web app. This is the following exception we get:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named pu1:
The following providers:
oracle.toplink.essentials.PersistenceProvider
oracle.toplink.essentials.ejb.cmp3.EntityManagerFactoryProvider
Returned null to createEntityManagerFactory.
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:154)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
at client.Client.main(Client.java:45)
Java Result: 1
this happens after executing this line from our emf driver class:
emf = Persistence.createEntityManagerFactory("pu1");
I'm assuming the problem is in our persistence.xml file (which is in the correct folder (WEB-INF/classes/META-INF). Also netbeans generated the xml file for us which is:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="JPAExamplePU" transaction-type="JTA">
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
<jta-data-source>SomeDB</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="toplink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>
We were also thinking it may be an adding a library issue or something along that line. Any help is much appreciated. Thanks
You have the wrong persistence unit name. Use the one from the xml (i.e. the one defined with <persistence-unit name="..."):
emf = Persistence.createEntityManagerFactory("JPAExamplePU");