tomee - how to use RESOURCE_LOCAL datasource - jpa

I have some classes (ejb, webservices, mdb, etc..) that can use JTA. For some classes I need RESOURCE_LOCAL (can't be injected). However I can't get tomee to reference the jndi name of RESOURCE_LOCAL. How do you setup tomee and RESOURCE_LOCAL? I can't seem to find one good example online, I would prefer not to put any usernames and passwords in my persistence.xml file.
tomee.xml has this:
<Resource id="MYDS" type="DataSource">
JdbcDriver com.mysql.jdbc.Driver
JdbcUrl jdbc:mysql://127.0.0.1:3306/maestro
UserName myusername
Password mypassword
JtaManaged false
</Resource>
persistence.xml looks like:
<?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 = "MYDS" transaction-type = "RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>MYDS</non-jta-data-source>
</persistence-unit>
</persistence>
I am using name MYDS in EntityManagerFactory lookup, but get this error:
Caused by: org.hibernate.engine.jndi.JndiException: Unable to lookup JNDI name [MYDS]
at org.hibernate.engine.jndi.internal.JndiServiceImpl.locate(JndiServiceImpl.java:117)
at org.hibernate.engine.jdbc.connections.internal.DatasourceConnectionProviderImpl.configure(DatasourceConnectionProviderImpl.java:115)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:260)
at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:94)
at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:111)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:234)
at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:206)
at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1887)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1845)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:852)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:845)
at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.withTccl(ClassLoaderServiceImpl.java:398)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:844)
at org.hibernate.jpa.HibernatePersistenceProvider.createEntityManagerFactory(HibernatePersistenceProvider.java:75)
... 36 more
Caused by: javax.naming.NameNotFoundException: Name [MYDS] is not bound in this Context. Unable to find [MYDS].
at org.apache.naming.NamingContext.lookup(NamingContext.java:817)
at org.apache.naming.NamingContext.lookup(NamingContext.java:160)
at org.apache.naming.NamingContext.lookup(NamingContext.java:828)
at org.apache.naming.NamingContext.lookup(NamingContext.java:160)

the solution seems to be this (still verifying):
(not very intuative or documented, adding openejb:Resource to JPA and JPA doesn't work, removing it from RESOURCE_LOCAL and RESOURCE_LOCAL doesn't work)
<?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 = "MYDS" transaction-type = "RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>openejb:Resource/MYDS</non-jta-data-source>
</persistence-unit>
<persistence-unit name="MYDSJPA" transaction-type = "JTA">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>MYDS</jta-data-source>
</properties>
</persistence-unit>
</persistence>

how do you get your persistence unit? Manually?
If using injection:
#PersistenceUnit EntityManagerFactory emf;
#PersistenceContect EntityManager em;
TomEE resolves the datasource for you from its short name (id in tomee.xml) otherwise you need to give it the full JNDI name which I think is java:openejb/Resource/MYDS

Related

Getting hibernate persistence-unit classes from both ORM and OGM

I'm trying to use both of hibernate's orm and ogm in the same application.
In my persistence.xml I have two persistence-unit:
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="orm-mysql">
...
<class>...</class>
...
<class>...</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
...
</persistence-unit>
<persistence-unit name="ogm-mongodb" transaction-type="JTA">
...
<class>...</class>
...
<class>...</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
...
</persistence-unit>
...
</persistence>
I want to use the hibernate orm native api with both orm and ogm, and so I need to add the annotated classes to two different MetadataSources.
Since I have the persistence units I thought that I can do this:
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("orm-mysql");
Set<ManagedType<?>> managedTypes = entityManagerFactory.getMetamodel().getManagedTypes();
for (ManagedType type : managedTypes) {
ormSources.addAnnotatedClass(type.getJavaType());
}
entityManagerFactory = Persistence.createEntityManagerFactory("ogm-mongo");
managedTypes = entityManagerFactory.getMetamodel().getManagedTypes();
for (ManagedType type : managedTypes) {
ogmSources.addAnnotatedClass(type.getJavaType());
}
The problem is that in the first iteration, of the orm classes, I get both the orm and ogm classes that are included in orm-mysql and ogm-mongo.
The second time around it's fine though, I get only the ogm-mongo classes.
Can't figure out what's wrong, any ideas?
In case anyone makes this stupid mistake as well, I had:
<persistence-unit name="orm-mysql">
...
<class>...</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.archive.autodetection" value="class"/>
...
</properties>
</persistence-unit>
Removing the hibernate.archive.autodetection property solved the problem.

EclipseLink does not work on Netbeans, is this normal?

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?

Can not create EntityManager factory bc JTA tx manager requiered

I can not build my entity manager factory and the error I get is "chosen transaction strategy requires access to the JTA Transaction Manager". Can anyone advise what i have to configure to get past this? FYI I'm using Jboss 6, no spring.
Here is my persistance.xml
<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="AvengersPU" transaction-type="JTA">
<jta-data-source>java:/jdbc/thor_ds</jta-data-source>
<class>avenger.Grouptable</class>
<class>avenger.MyUser</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
</properties>
</persistence-unit>
</persistence>
Here is partial stacktrace...
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: AvengersPU] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:915) [:3.6.6.Final]
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:57) [:3.6.6.Final]
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:48) [:1.0.0.Final]
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32) [:1.0.0.Final]
at avenger.Utils.getEntityManager(Utils.java:49) [:]
at avenger.UserBean.updateUserList(UserBean.java:136) [:]
at avenger.UserBean.startup(UserBean.java:43) [:]
... 68 more
Caused by: org.hibernate.HibernateException: The chosen transaction strategy requires access to the JTA TransactionManager
at org.hibernate.impl.SessionFactoryImpl.<init>(SessionFactoryImpl.java:390) [:3.6.6.Final]
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1872) [:3.6.6.Final]
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:906) [:3.6.6.Final]
... 74 more
Full stack trace is here: http://pastebin.ca/2166417
Here is an non ideal answer which I will use in the meanwhile.
specify a non jta datasource:
<non-jta-data-source>java:/jdbc/thor_ds</non-jta-data-source>
change transaction type from JTA to RESOURCE_LOCAL
<persistence-unit name="AvengersPU" transaction-type="RESOURCE_LOCAL">
Here is a discussion on the subject:

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).

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");