I have an application using playFramework, Spring & Hibernate like this:
Play-Spring-Data-JPA-APP
I want to implement multiple datasources with AbstractRoutingDataSource, so I have my custom class:
package mx;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
*
*/
public class MyRouting extends AbstractRoutingDataSource {
#Override
protected Object determineCurrentLookupKey() {
System.out.println("Logic to determine the datasource");
return "key";
}
}
But when I try to set the datasource in my application.conf like this:
db.routingDS.dataSourceClassName = mx.MyRouting
I get a
[error] play - Error while stopping the application
play.api.PlayException: Cannot load plugin[An exception occurred during Plugin [com.edulify.play.hikaricp.HikariCPPlugin] initialization]
at play.api.WithDefaultPlugins$$anonfun$plugins$1$$anonfun$apply$9.apply(Application.scala:154) ~[play_2.10-2.3.10.jar:2.3.10]
at play.api.WithDefaultPlugins$$anonfun$plugins$1$$anonfun$apply$9.apply(Application.scala:130) ~[play_2.10-2.3.10.jar:2.3.10]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library.jar:0.13.5]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library.jar:0.13.5]
at scala.collection.immutable.List.foreach(List.scala:318) ~[scala-library.jar:0.13.5]
Caused by: play.api.Configuration$$anon$1: Configuration error[java.lang.ClassNotFoundException: mx.MyRouting]
at play.api.Configuration$.play$api$Configuration$$configError(Configuration.scala:94) ~[play_2.10-2.3.10.jar:2.3.10]
at play.api.Configuration.reportError(Configuration.scala:743) ~[play_2.10-2.3.10.jar:2.3.10]
at com.edulify.play.hikaricp.HikariCPDBApi$$anonfun$2.apply(HikariCPDBApi.scala:64) ~[play-hikaricp_2.10-2.0.6.jar:2.0.6]
at com.edulify.play.hikaricp.HikariCPDBApi$$anonfun$2.apply(HikariCPDBApi.scala:44) ~[play-hikaricp_2.10-2.0.6.jar:2.0.6]
at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:244) ~[scala-library.jar:0.13.5]
Caused by: java.lang.RuntimeException: java.lang.ClassNotFoundException: mx.MyRouting
at com.zaxxer.hikari.util.UtilityElf.createInstance(UtilityElf.java:120) ~[HikariCP-2.3.8.jar:na]
at com.zaxxer.hikari.pool.PoolUtilities.initializeDataSource(PoolUtilities.java:102) ~[HikariCP-2.3.8.jar:na]
at com.zaxxer.hikari.pool.BaseHikariPool.<init>(BaseHikariPool.java:156) ~[HikariCP-2.3.8.jar:na]
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:60) ~[HikariCP-2.3.8.jar:na]
at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:48) ~[HikariCP-2.3.8.jar:na]
Caused by: java.lang.ClassNotFoundException: mx.MyRouting
at java.net.URLClassLoader$1.run(URLClassLoader.java:372) ~[na:1.8.0_25]
at java.net.URLClassLoader$1.run(URLClassLoader.java:361) ~[na:1.8.0_25]
at java.security.AccessController.doPrivileged(Native Method) ~[na:1.8.0_25]
at java.net.URLClassLoader.findClass(URLClassLoader.java:360) ~[na:1.8.0_25]
at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_25]
(Im also using a db configuration with HikariCP)
I also tried to generate a jar with the class and the result is the same, Anyone has some idea about it? Thanks!
what you are trying to achieve is not possible thru play application config directly. The main problem is that by default play expects an URL and driver to create the Datasource (at least in play 2.3 and play 2.4).
In other words, play expects that always that you are going to create a datasource, you will be using a connection pool to a real database not to a RoutingDatasource.
Since you are using a JPA seed, you need to access the resources thru JNDI, so you need to add the datasource to the JNDI in some way.
This is how it can be achived:
In the application.conf only declare the datasources that will interact to the database. As many as you want:
# Database configuration
#
db.mydb1.driver=org.h2.Driver
db.mydb1.url="jdbc:h2:mem:play"
db.mydb1.jndiName=DB1DS
db.mydb2.driver=org.h2.Driver
db.mydb2.url="jdbc:h2:mem:play"
db.mydb2.jndiName=DB2DS
Now, in the persistence.xml ensure that you do not declare any datasource. (neither jta or not jta)
<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="default" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
</persistence>
Finally, in the Global.java or Global.scala, force into the JNDI the RoutingDatasource. The nasty part is that there's no documentation or post anyware of how Play manage the JNDI. Basically, Play creates an on memory JNDI using the class "InitialContext". This context is writable (thanks god) cause there are many application servers that protect their initial context.
//This is the routing Datasource in spring context
#Bean
public RoutingDatasource routingDataSource() {
RoutingDatasource rd = new RoutingDatasource();
java.util.HashMap ds = new java.util.HashMap();
ds.put("DB1DS","DB1DS");
ds.put("DB2DS","DB2DS");
rd.setTargetDataSources(ds);
return rd;
}
#Bean
public EntityManagerFactory entityManagerFactory() {
java.util.Map props = new java.util.HashMap();
try{
//This is the magic line.
((javax.naming.Context)(new javax.naming.InitialContext())).bind("routingDataSource", ctx.getBean("routingDataSource"));
}catch(Exception e){}
props.put("javax.persistence.jtaDataSource", "routingDataSource");
return Persistence.createEntityManagerFactory(DEFAULT_PERSISTENCE_UNIT,props);
}
And finally just as you mentioned, create your own RoutingDatasource:
public class RoutingDatasource extends AbstractRoutingDataSource {
#Override
protected Object determineCurrentLookupKey() {
return selectRandom("DB1DS","DBDS2");
}
}
Thats all. I hope it helps solving your problem.
Related
I am total newbie in Java and EE especially. I started an EE project that should provide REST API which will handle 2 entities in remote Oracle Database. I am using NetBeans because it is the only way how to accomplish anything in Enterprise Java (as I see it now).
What I've done:
I created JDBC pool in Glassfish (v4.1-13). I can ping the pool successfully. Then I created JDBC Resource for the pool.
I generated Entity classes for the two entities I need to handle.
<persistence version="2.1" xmlns...>
<persistence-unit name="semestralka-ejbPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/dbs</jta-data-source>
<class>cz.ctu.bitjv.kopecj24.semestralka.entities.Food</class>
<class>cz.ctu.bitjv.kopecj24.semestralka.entities.User</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.target-database" value="Oracle"/>
</properties>
</persistence-unit>
</persistence>
I have a stateless EJB which calls entity manager like this:
public FoodServiceBean()
{
this.facade = new FoodFacade(Food.class);
this.facade.setEntityManager(Persistence.createEntityManagerFactory("semestralka-ejbPU").createEntityManager());
}
Then, there is a REST service class that should list the entities from the database.
#Path("food")
public class FoodResource {
#Context
private UriInfo context;
private FoodServiceInterface service;
/**
* Creates a new instance of FoodResource
*/
public FoodResource() {
try {
InitialContext ic = new InitialContext();
service = (FoodServiceInterface) ic.lookup("java:global/semestralka/semestralka-ejb/FoodServiceBean");
} catch (NamingException ex) {...}
}
#GET
#Produces(MediaType.TEXT_PLAIN)
#Path("list")
public String getAll() {
List<Food> foods = service.listAllFood();
...
}
}
Unfortunately, once I request the getAll action (visit localhost:8080/semestralka-war/wr/food/list ) I get this exception:
Warning: StandardWrapperValve[cz.ctu.bitjv.kopecj24.semestralka.rest.ApplicationConfig]: Servlet.service() for servlet cz.ctu.bitjv.kopecj24.semestralka.rest.ApplicationConfig threw exception
javax.naming.NameNotFoundException: dbs not found
Here is a screenshot of the exception screen:
Double check the connection pool name in persistence unit and glassfish server. Also could you update your question with the entities.
I can see that your ejb calling from rest service is wrong. You need to add remote interface name with package path.
Lets say your package path is com.rs.www then your lookup string should be following one :
service = (FoodServiceInterface) ic.lookup("java:global/semestralka/semestralka-ejb/FoodServiceBean!com.rs.www.FoodServiceInterface");
Thanks.
Finally, I've found a solution. Problem was in my FoodServiceBean. I was trying to instantiate the facade in the EJB constructor but the EntityManager is injected after the constructor. So here is code of the Bean that helped me solve the issue.
#Stateless
#EJB(beanInterface=FoodServiceInterface.class, name="FoodServiceBean")
public class FoodServiceBean implements FoodServiceInterface {
#PersistenceContext(unitName="testPU")
private EntityManager em;
private FoodFacade facade;
public FoodServiceBean()
{
}
#PostConstruct
public void init() {
this.facade = new FoodFacade(Food.class);
this.facade.setEntityManager(em);
}
Please note that I changed the name of persistence unit just to be sure there are no typos.
Thanks for the help.
I'm creating a JSF web app on TomEE1.7.2 (over tomcat7, javaEE6).
I have JDBC to mysql5.6.23 connection setting which look like below,
and it is working just good, for only some hours.
My data access super class:
public class BaseDao {
#javax.ejb.EJB
MyEnvironmentService env;
#javax.persistence.PersistenceContext(unitName = "persistence-unit-stg")
protected javax.persistence.EntityManager em_stg;
#javax.persistence.PersistenceContext(unitName = "persistence-unit-prd")
protected javax.persistence.EntityManager em_prd;
protected javax.persistence.EntityManager em;
#javax.annotation.PostConstruct
private void init(){
switch (env.getName()){
case "stg":
em = em_stg;
break;
case "prd":
em = em_prd;
break;
default:
throw new RuntimeException("Oh no!");
}
}
}
My data access class:
#javax.ejb.Stateless
public class MyDao extends BaseDao{
public MyEntity find(Object id) {
return em.find(MyEntity.class, id);
}
}
My META-INF/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="persistence-unit-stg" transaction-type="JTA">
<jta-data-source>mysql-jdbc-jta-resource-stg</jta-data-source>
</persistence-unit>
<!-- almost same persistence-unit for prd -->
</persistence>
My WEB-INF/resource.xml:
<?xml version='1.0' encoding='UTF-8'?>
<resources>
<Resource id="mysql-jdbc-jta-resource-stg" type="javax.sql.DataSource">
JdbcDriver com.mysql.jdbc.Driver
JdbcUrl jdbc:mysql://db-stg.bar.someRegion.rds.amazonaws.com/someDBname
UserName username
password password
jtaManaged true
</Resource>
<!-- almost same Resource for prd -->
</resources>
I have 2 problems that I want to solve:
1.
When I start my web app on staging environment, leave it, and mysql "wait_timeout" comes, my web application gets
javax.transaction.RollbackException: Transaction is marked for rollback
and CANNOT reconnect to mysql again forever.
I've already tried some JDBC's
autoreconnect=true or autoreconnectForPool=true
things, did NOT work either, and I feel like it's not the best thing to do, in the first place (feature already deprecated?) .
2.
When I start my web app, I see exactly 3 connections connected to mysql,
and everytime, ONLY 1 of the connection is seem to be used and extends its timeout,
but other 2 are never used and killed just at the "wait_timeout".
I tried "initialSize 10", and it comes with 15 connections,
and again, ONLY 1 of the connection is used.
I guess there is a really simple solution for them,
because it says:
Internally, from TomEE 1.5.0, JDBC pools are managed via Tomcat-pool.
in this page http://tomee.apache.org/common-datasource-configurations.html
Well yes, I DO want to use this Tomcat JDBC pool ( or something alternative is fine.), it doesn't seem to be enabled.
May be I'm missing some xml settings or jar or whatever, but I have no idea.
please help me out.
I'm successfully using a resources.xml in my WEB-INF folder similar to this one in of my production projects:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<Resource id="mysql-jdbc-jta-resource-stg" type="javax.sql.DataSource">
JtaManaged = true
DataSourceCreator = tomcat
validationQuery = SELECT 1
initialSize = 2
removeAbandoned = true
removeAbandonedTimeout = 120
driverClassName = com.mysql.jdbc.Driver
url = jdbc:mysql://db-stg.bar.someRegion.rds.amazonaws.com/someDBname
username = your-username
password = your-pw
</Resource>
</resources>
One important difference is to use DataSourceCreator = tomcat. This ensures that TomEE creates a pool which takes care for connection validation ("SELECT 1") and removes stalled/outdated connections, thus freeing up resources in the background.
By contrast, the DataSourceCreator = dbpc (which is default when not set: "Default provided pools are DBCP...") behaved as you described/experienced it (timeouts,...). Check the related documentation for further differences.
Also note, that = is used in my working configuration. I discovered that in previous TomEE releases it seemed to work without =. However, I would recommend to configure with = consistently when your target application server is TomEE 1.7.2 or higher.
Further details on this can also be found in the DataSource Configuration page.
Hope it helps.
I am new to JPA and developing a webapp(J2EE) where the webapp is in Tomcat so I can't use #PersistenceContext. I decided to use a Helper class and everything was going fine. Then I decided to implement JNDI for connection pooling and I managed to get Datasource.
The Helper Class looks like the following:
try {
Context initCtx = new InitialContext();
entityManager = //class cast exception
(EntityManager)initCtx.lookup(
"java:/comp/env/jdbc/LCDS"
);
DataSource ds= (DataSource)initCtx.lookup(
"java:/comp/env/jdbc/LCDS"
);
System.out.println(ds.getConnection()+"Cool");
//jdbc:mysql://localhost:3306/XXXXXXX, UserName=root#localhost, MySQL-AB JDBC DriverCool
emf=(EntityManagerFactory) source.getConnection(); //class cast exception
emf = Persistence.createEntityManagerFactory("XXXX"); //working version
}
The error is:
ava.lang.ClassCastException: org.apache.tomcat.dbcp.dbcp.BasicDataSource cannot be cast to javax.persistence.EntityManager
I don't know where I am getting wrong. I am not able to get EntityManagerFactory or EntityManager via JNDI lookup. I tried #Resource(name="jdbc/LCDS") and #PersistenceUnit(name="jdbc/LCDS").
To use a JNDI datasource in JPA, this should be specified in the persistence.xml, something like:
<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">
<persistence-unit name="..." transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>java:/comp/env/jdbc/LCDS</non-jta-data-source>
...
Then you just have to create your EntityManagerFactory via Persistence#createEntityManagerFactory(String). If you want to recycle the EntityManagerFactory, this should be done outside of JNDI (e.g. as a ServletContext attribute). This is because Tomcat is not a Java EE server, only a servlet container: he is not able to inject the persistence unit.
UPDATE
JNDI access to persistence unit is not possible due to Tomcat limitations. See JPA Tomcat limitations. You will have to use emf = Persistence.createEntityManagerFactory("UNIT NAME").
Sorry for misleading answer. I've tested that on WebSphere Liberty, didn't have Tomcat at hand.
If you need that functionality check WebSphere Liberty, which is as fast and lightweight as Tomcat, but is fully Java EE Web profile compliant. It has lots of useful features like JPA, EJBLite, JAX-RS already available if needed, without fighting with additional libraries configuration.
UPDATE END
I've checked on WebSphere Liberty, you need to create reference to lookup your persistence unit via JNDI. You have two options to create that:
Use annotation at the class level
In any of your servlets you need to define annotation using the follownig:
#PersistenceUnit(name="JPATestRef", unitName="UnitName")
public class JPATester extends HttpServlet {
...
Use entry in web.xml
<persistence-unit-ref>
<persistence-unit-ref-name>JPATestRef</persistence-unit-ref-name>
<persistence-unit-name>UnitName</persistence-unit-name>
</persistence-unit-ref>
Then you access it using the following code:
try {
InitialContext ctx = new InitialContext();
System.out.println("looking EntityManagerFactory:");
EntityManagerFactory emf2 = (EntityManagerFactory) ctx.lookup("java:comp/env/JPATestRef");
System.out.println("emf:2" + emf2);
} catch (NamingException e) {
I have earlier worked with application-managed RESOURCE-LOCAL transaction but now I want to use container-managed JTA transaction. Everything seems to be ok while I am using #Stateless but as soon as I use #Stateful I get an exception as below
javax.ejb.EJBException: javax.persistence.TransactionRequiredException: joinTransaction has been called on a resource-local EntityManager which is unable to register for a JTA transaction.
I am using JBoss eap 6.2 with eclipselink2.5 and Java8 and Oracle.Here are my codes
#Stateful
#TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public class LoginDetailService {
#PersistenceContext(unitName="OracleDB", type=PersistenceContextType.EXTENDED)
protected EntityManager em;
public void addLoginDetails(String email, String pwd){
LoginDetail ld = new LoginDetail(email,pwd);
em.persist(ld);
}
#Remove
public void finished(){}
}
My Servlet code
#WebServlet("/signup")
public class SignUpServlet extends HttpServlet {
#EJB LoginDetailService bean;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String email = "EMAIL",
pwd = "PASSWORD";
bean.addLoginDetails(email, pwd); //exception occurs here
response.getWriter().println("Successful");
}
}
And my persistence.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="OracleDB" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:jboss/jdbc/OracleDB</jta-data-source>
<class>com.entity.Student</class>
<class>com.entity.LoginDetail</class>
<properties>
<property name="eclipselink.logging.level" value="FINEST"/>
</properties>
</persistence-unit>
</persistence>
Plz hekp and guide me where I am going wrong. Thanks
Finally after working a lot, I found the problem. Actually there was no issue with my code, it was because of the JBoss server. I tested the same application with Glassfish4 and it worked perfectly.
REASON
The annotation #EJB has no effect in JBoss. Though you will see that a JNDI binding has occurred with the bean but when you will try tp persist, it wont work.
SOLUTION
To make it work on JBoss instead of #EJB, you will have to do a JNDI lookup and carry out the transaction. But the lookup for some reason failed on my desktop but worked fine on laptop may be due to some weird server configuration.
Another and better solution which I feel is to use another server like Glassfish or WebLogic where #EJB works fine and not a single bit of extra coding.
I'm creating my first JPA application using NetBeans. I'm unable to make the persistence work. The connection to database works well, when I run the application the database tables got created. But when I try to create EntityManagerFactory:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("PISProjektPU");
I get:
INFO: javax.persistence.PersistenceException: [PersistenceUnit: PISProjektPU] Unable to build EntityManagerFactory
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:677)
at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:126)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:78)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at cz.vutbr.fit.pis.spravaTechniky.service.TestManager.<init>(TestManager.java:28)
at cz.vutbr.fit.pis.spravaTechniky.service.__EJB31_Generated__TestManager__Intf____Bean__.<init>(Unknown Source)
...
My persistence.xml file looks like this (generated by NetBeans, I didn't change anything):
<?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="PISProjektPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/myslq_spravaTechniky</jta-data-source>
<class>cz.vutbr.fit.pis.spravaTechniky.data.TestEntity</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
</properties>
</persistence-unit>
</persistence>
and it's located here:
src/conf/persistence.xml
build/web/WEB-INF/classes/META-INF/persistence.xml
I searched the forum and found some tips how to remove this error, but I was unable to make it work. I tried to add these two lines to Manifest.mf:
Meta-Persistence: META-INF/persistence.xml
JPA-PersistenceUnits: PISProjektPU
I tried to move the persistence.xml file to all possible locations. I also added all libraries that seemed like they might be useful, when I go to Properties/Libraries, I see:
Java EE 6 API Library
Hibernate
Java-EE-GlassFish-v3
EclipseLink(JPA 2.0)
EclipseLink-GlassFish-v3
Hibernate JPA
JSF 2.0
Java EE Web 6 API Library
Persistence
I'm sure I'm doing some stupid simple mistake, but after a day trying to make this work I am unable to see where is the problem. To be honest right now I'm just totally confused about where to put which file or how to configure everything, so I'm randomly trying different things. I will be thankful for any advice!
Edit:
Thanks for the suggestion. My test classes actually look like this:
Class TestManager:
#Stateless
public class TestManager {
#PersistenceContext
private EntityManager em;
public void save(TestEntity t) {
em.merge(t);
}
public void remove(TestEntity t) {
em.remove(em.merge(t));
}
public void create(TestEntity t) {
em.persist(t);
}
#SuppressWarnings("unchecked")
public List<TestEntity> findAll() {
return em.createQuery("SELECT t FROM TestEntity t").getResultList();
}
}
Class TestBean:
#Named(value="testBean")
#Dependent
public class TestBean {
#EJB
private TestManager testManager;
/** Creates a new instance of TestBean */
public TestBean() {
}
public List<TestEntity> getEntities() {
return this.testManager.findAll();
}
}
I'm calling the TestBean.getEntities method:
...
<h:dataTable value="#{testBean.entities}" var="entity">
...
This causes the following exception:
javax.ejb.EJBException
at com.sun.ejb.containers.BaseContainer.processSystemException(BaseContainer.java:5119)
at com.sun.ejb.containers.BaseContainer.completeNewTx(BaseContainer.java:5017)
at com.sun.ejb.containers.BaseContainer.postInvokeTx(BaseContainer.java:4805)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:2004)
at com.sun.ejb.containers.BaseContainer.postInvoke(BaseContainer.java:1955)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:198)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandlerDelegate.invoke(EJBLocalObjectInvocationHandlerDelegate.java:84)
at $Proxy141.findAll(Unknown Source)
at cz.vutbr.fit.pis.spravaTechniky.service.__EJB31_Generated__TestManager__Intf____Bean__.findAll(Unknown Source)
at cz.vutbr.fit.pis.spravaTechniky.back.TestBean.getEntities(TestBean.java:27)
...
I tried to replace the #PersistenceContext with #EJB, but got javax.ejb.EJBException: javax.ejb.CreateException: Could not create stateless EJB.
try to use this one
#EJB
EntityManager em;
em.persist(someobject);
instead of factory, if you need to use factory , i suggest you to repeat steps of setting up the persistance of entities in your IDE
Several things to correct here:
First, you are mixing Hibernate and EclipseLink in the same application (why??) you should just use one of them, choose between Hibernate or EclipseLink as both are implementations of the same standard: JPA (and you will need to choose the JPA API from one of those implementations).
Try to replace the #EJB annotation for a #Inject one. With the stack you are using that one should work.
Using the "dependent pseudo-scope" (by means of the #Dependent annotation) is the same as not establishing a scope at all (no annotation). Remove the #Dependent annotation from your bean class and place there a #RequestScoped (#SessionScoped, #ApplicationScoped or whatever scope your bean should have).
In the end I made JPA work just by using one of the example projects in NetBeans (File > New Project > Samples > Java Web > JSF JPA). I used this as a base for my project. I am not sure what I was doing wrong but at least everything worked fine then. Anyway, thanks for the help!