How to detach an entity (JPA 2.0/EclipseLink/JBoss) - jpa

I need to detach some entity objects from the database to make them unmanaged. I use EclipseLink persistence provider, which method EntityManager.detach() is exactly one I need. The problem is that JBoss throws at runtime following exception (when execution passes to detach()):
javax.ejb.EJBTransactionRolledbackException: Unexpected Error
java.lang.NoSuchMethodError: javax.persistence.EntityManager.detach(Ljava/lang/Object;)V
Other methods like persist, merge, find work fine. I tried Hibernate and know that its Session provides a special method evict(), which detaches entity, but EclipseLink has no such method.
Example of using detach():
#PersistenceContext(unitName="Course7-ejbPU")
protected EntityManager manager;
(...)
Query query;
List<Message> resultList;
query = manager.createNamedQuery("Message.getUserInputMessageList");
query.setParameter("login", login);
query.setMaxResults(5);
resultList = query.getResultList();
for (Message message : resultList)
if (message.getContent().length() > 50)
{
manager.detach(message);
message.setContent(message.getContent().substring(0, 50) + "...");
}
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="Course7-ejbPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:/Course7ds</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.target-server" value="JBoss"/>
</properties>
</persistence-unit>
</persistence>
Library with provider data is included into ear archive.
EclipseLink version is 2.2.0 (tested with 2.3.2 - no difference), JBoss server version 5.1.0. Any suggestions will be appreciated.

This exception shows that you're not using JPA2, but JPA1. You should probably use a more recent version of JBoss, that ships with JPA2.

You compiled your code with JPA 2.0 classes, but you run it with JPA 1.0. This is why the JVM doesn't find the detach method.
In reaction to your comment: no, the detach method is not useless for JPA 1.0 user: it's just it has not been created yet. You can however erase all the L1 cache by calling clean() on the entitymanager, which will detach all your managed entities...
You can still be able to detach an entity by using persistence provider specific code.
It is not because the entity manager does not provide a function yet, that the jpa providers hasn't implemented it yet.
If you can couple a little bit your code to your jpa provider:
You can call the em.getDelegate() method that will return you an EclipseLink entity manager implementation (check in debug the returned value and cast it) which may perhaps give you the possibility to detach your entity.
The method may not be named detach() -> for Hibernate it's evict().

Related

How to create tables from entity classes in JPA on server Startup?

I am supposed to check for existence of tables on server startup. If they did not exist I have to create them using entity classes. Is this even possible?
I am using Eclipse and my server is wildfly10. I am connecting to Oracle 11g xe but I don't think it is database that is causing issues
Anyway, this is what I have done so far
My persistence.xml:
<?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="Lab5">
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>
</persistence-unit>
</persistence>
Without above property my database query throws Exception. But what is really strange, is that with this property query returns empty list, but table does not exist in database.
Also, I noticed warning showing on server startup
HHH000431: Unable to determine H2 database version, certain features may not work
I tried various fixes for this warning found on stackoverflow and other sites but they did not help. I don't even know if this is related to my problem
JPA 2.1 made DDL generation a part of the specification. You can use the "javax.persistence.schema-generation.database.action" persistence property with a "create" value to have 2.1 providers create the database schema for you during deployment. JPA can be used to generate or even run custom scripts should you need to modify the table creation/tear down and population process.
The problem you are likely encountering is your persistence unit does not specify a datasource, leaving it up to the container to figure out which you want to connect to, and Wildfly must be defaulting to an H2 database. The tables will be setup in this database, but not the Oracle XE database you are expecting - which is why queries return no values and yet in your console, the tables don't even exist.
You need to setup a datasource to your database in the server, and then point your persistence unit to it using the <non-jta-data-source> or <jta-data-source>. Your persistence.xml is incomplete, so I would urge you to look at a demo persistence unit first.
This will likely depend on the implementation you're including with your project. Assuming you're using hibernate for persistence, you'll need to add the following:
...
<persistence-unit ...>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
...
<properties>
<property name = "hibernate.hbm2ddl.auto" value="create" />
...
</properties>
</persistence-unit>
Note that this will assume that your connection is formed with a user having the required privileges in the db for table creation. Take a look at the hibernate docs here.

In JBoss/WildFly should I enable JTA on data source to use with JPA?

In JBoss/WildFly, when configuring a data source, there is a JTA option, which is disabled by default:
<datasource jta="false" jndi-name="java:/wt/testds" pool-name="testds" enabled="true" use-ccm="false">
...
</datasource>
Now I want to associate this data source with JPA using JTA transaction type:
<?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="test" transaction-type="JTA">
<jta-data-source>java:/wt/testds</jta-data-source>
</persistence-unit>
</persistence>
Do I also need to enable JTA on the data source?
Yes, of course you need to enable JTA on a datasource if you want to have jta transactions!
Your XML/JBoss/Wildfly config file will look like this:
<datasource jta="true" ...
In our webapp persistence-unit, the datasource looks like this:
<jta-data-source>java:jboss/datasources/CoreDS</jta-data-source>
The transaction-type="JTA" isn't necessary, at least not in my setup (Wildfly 8.1).
In your Java code you can go like this to use transactions:
#TransactionManagement(TransactionManagementType.CONTAINER) // class level
public class ...
...
#PersistenceContext(unitName = "CoreJPA")
EntityManager em;
#Resource
private EJBContext ejbContext;
...
#TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) // method level
public void doSomething(...)
And if you need to rollback, you do this:
try {
...
} catch (Throwable t) {
log.error("Exception in create work order: " + t.getMessage());
ejbContext.setRollbackOnly();
throw t;
}
There are a lot of resources about this that can be found using Google.
I've just experienced a problem related to this issue.
I was running a container managed transaction involving approximately 20,000 inserts into a MySQL database.
The transaction failed randomly, sometimes after around 3,500 inserts, other times after around 6,000 inserts, etc.
After investigation I found that the JTA option on the WildFly datasource definition was set to false.
Changing this setting to true fixed the problem, so I would agree with #user3472929 that JTA should be set to true in the datasource definition unless you have some specific reason not to.
I think you should use jta. And if you set jta to false in the container configuration file, jta will be disabled for JPA, so the transaction-type for JPA will be "RESOURCE_LOCAL", which has some nasty side effect. By the way, jta is true in the container configuration file by default.

Lazy loading of #ManyToOne relation fails with GlassFish 4 / EclipseLink

GlassFish 4 (actually its JPA implementation, i.e. EclipseLink) fails to lazy load a #ManyToOne JPA relation from our Java EE 7 application. Default/eager loading is ok, but not lazy loading.
The relation in the 'Student' entity is:
#ManyToOne(fetch = FetchType.LAZY)
#JoinColumn(name = "addr_id")
private Address address;
The (simplified) persistence.xml looks like:
<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_1.xsd"
version="2.1">
<persistence-unit name="foo-PU" transaction-type="JTA">
<jta-data-source>jdbc/foo-DS</jta-data-source>
<class>foo.domain.Student</class>
<class>foo.domain.Address</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.target-database" value="PostgreSQL"/>
<property name="eclipselink.logging.level" value="FINE"/>
</properties>
</persistence-unit>
</persistence>
The application uses several API: PrimeFaces, JSF 2.2, CDI 1.1, JPA 2.1.
Also note that the EntityManager are not obtained by injection into session EJB, but manually created using Persistence.createEntityManagerFactory(...) then emf.createEntityManager(...).
The error message is:
WARNING: Reverting the lazy setting on the OneToOne or ManyToOne attribute [address] for the entity class [class foo.domain.Student] since weaving was not enabled or did not occur.
My understanding is that, for some reason, the dynamic weaving of entities is not enabled. For a Java EE application it should be, as suggested by http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Performance/Weaving.
For the record, if we try to force the weaving using this:
<property name="eclipselink.weaving" value="true"/>
in the persistence.xml, then we get another error message:
SEVERE: Error Rendering View[/student/studentList.xhtml]
javax.el.ELException: /student/studentList.xhtml #24,81 value="#{studentController.selectedCode}": Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: WebappClassLoader (delegate=true; repositories=WEB-INF/classes/)
Internal Exception: javax.persistence.PersistenceException: Exception [EclipseLink-28022] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Value [true] for the property [eclipselink.weaving] is incorrect when global instrumentation is null, value should either be null, false, or static.
at com.sun.faces.facelets.el.TagValueExpression.getValue(TagValueExpression.java:114)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:194)
at javax.faces.component.ComponentStateHelper.eval(ComponentStateHelper.java:182)
at javax.faces.component.UIOutput.getValue(UIOutput.java:174)
at javax.faces.component.UIInput.getValue(UIInput.java:291)
at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getValue(HtmlBasicInputRenderer.java:205)
(...)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
Any idea how to fix this lazy-loading issue? Why is the dynamic weaving not enabled by default ?
Thanks.
If you set eclipselink.weaving to true it means that you want to weave an entity classes dynamically. To make this work you need to run a jvm with a proper javaagent. First download an agent
wget -O /tmp/eclipselink.jar \
https://repo1.maven.org/maven2/org/eclipse/persistence/eclipselink/2.7.7/eclipselink-2.7.7.jar
and then run your app using following snippet
java -javaagent:/tmp/eclipselink.jar ....
But if you set eclipselink.weaving to static then you inform jpa that you want to weave the entity classes statically. Of course you have to trigger the waving by your own for example using this maven plugin https://github.com/craigday/eclipselink-staticweave-maven-plugin

In java EE 6: When could I change properties on EntityManager?

I want to use proxy authentication to an Oracle Express database in a simple web app with java EE 6 on Glassfish. Eclipselink 2.3.0 doc here says:
If a JEE and JTA managed EntityManager is used, specifying a proxy user/password can be more difficult, as the EntityManager and JDBC connection is not under the applications control. The persistence unit properties can still be specified on the EntityManager. As long as this is done before the EntityManager has established a database connection, this will still work.
My code is as below, but I guess the database connection is already established when the #Postconstruct init() method is called, since the last property is not changed from default true to false.
Also, I get ORA-00942: table or view does not exist from the save() method. The user pool in the jdbc connection pool defined in glassfish-resources.xml has not acces to the tables, as has proxy user user_a.
#Stateless
public class Boundary {
#PersistenceContext EntityManager em;
#PostConstruct
private void init() {
em.setProperty("eclipselink.oracle.proxy-type", OracleConnection.PROXYTYPE_USER_NAME);
em.setProperty(OracleConnection.PROXY_USER_NAME, "pool[user_a]");
em.setProperty(OracleConnection.PROXY_USER_PASSWORD, "pool");
em.setProperty(OracleConnection.PROXY_ROLES, "pool_user");
em.setProperty("eclipselink.jdbc.exclusive-connection.mode", "Always");
em.setProperty("eclipselink.jdbc.exclusive-connection.is-lazy", "false");
}
public void save() {
em.merge(new AnEntity());
}
}
I have not much experience here, but it would be really useful if proxy authentication could be made that simple, so I spent some time trying.
In a JavaEE managed context, is it possible to set properties on an EntityManager before it gets connected?
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="ProxyUserPU" transaction-type="JTA">
<jta-data-source>SecondTry</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="eclipselink.target-database" value="Oracle"/>
<property name="eclipselink.jdbc.exclusive-connection.is-lazy" value="true"/>
</properties>
</persistence-unit>
</persistence>
I don't think PostConstruct is the correct place for this.
A new EntityManager is bound to the persistence context on every transaction boundary, so you need to set these property at the start of every transactional SessionBean method. You could probably also use SessionEvents in EclipseLink to configure the properties.
If you properties are fixed, you could also put them in your persistence.xml.
See,
http://wiki.eclipse.org/EclipseLink/Examples/JPA/Auditing#Use_Oracle_proxy_authentication_to_allow_a_shared_connection_pool_and_a_user_context

Persistence.createEntityManagerFactory() in Java EE ignores JTA source

I have a perfectly working application client deployed to a GlassFish v2 server inside an ear with some EJBs, Entities, etc. I'm using eclipselink.
Currently I have in my persistence.xml:
<persistence-unit name="mysource">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/mysource</jta-data-source>
<class>entities.one</class>
<class>entities.two</class>
...
<properties>
<property name="eclipselink.target-server" value="SunAS9"/>
<property name="eclipselink.logging.level" value="FINE"/>
</properties>
</persistence-unit>
And this works fine when I inject the EntityManager into the EJB:
#PersistenceContext(unitName="mysource")
private EntityManager em;
Now I have a requirement to dynamically switch persistence units/databases.
I figure I can get an EntityManager programatically:
em = Persistence.createEntityManagerFactory("mysource").createEntityManager();
but I get the following error:
Unable to acquire a connection from driver [null], user [null] and URL [null]
Even "overriding" javax.persistence.jtaDataSource" to "jdbc/mysource" in a Map and calling createEntityManagerFactory("mysource", map) doesn't make a difference.
What am I missing?
You are trying to circumvent the container with creating an entity manager programmatically and this means you'll most probably create a non-JTA data source (as it's outside the container, the transaction type should be RESOURCE_LOCAL), thus your original config is useless.
Try injecting an entity manager with a different unitName property or create a RESOURCE_LOCAL transaction type persistence unit.