Unable to carry our a unit test in NetBeans - java

I've been trying to carry out a simple unit test on a java class. The problem is that it is not a mere java class running by itself, but part of a large project that has to be deployed before running. The class itself is called from a .zul file (ZK framework) and a database connection is required.
Whenever I run the unit test I get something like this:
[EL Severe]: ejb: 2018-12-19 15:48:27.11--ServerSession(254896875)--Exception [EclipseLink-7060] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.ValidationException
Exception Description: Cannot acquire data source [mydb]
Internal Exception: javax.naming.NameNotFoundException; remaining name 'mydb'
The error message arises when the system calls a function that fetches data from the db. In particular, it arises because of this line:
period.setDataModel(findAllPeriod());
"period" is a component of the .zul file, while findAllPeriod() is the function that fetches the data.
According to most related topics I've read so far, it could be related to the persistence.xml. There are two such xmls in the project. The first contains the line
*<jta-data-source>mydb</jta-data-source>*
and it is the only file that even mentions mydb explicitly. The other persistence.xml contains the line above as a comment, as well as the following:
*<property name="javax.persistence.jdbc.url" value="${jdbc.url}"/>
<property name="javax.persistence.jdbc.user" value="${jdbc.user}"/>
<property name="javax.persistence.jdbc.driver" value="${jdbc.driver}"/>
<property name="javax.persistence.jdbc.password" value="${jdbc.password}"/>*
Could something be wrong with the persistence.xml? Or it's the ZK framework somehow responsible for the problem?
I use NetBeans 8.2, Glassfish 4.1, and ZK.
Thank you in advance

Related

What do values in persistence.xml mean in EJB?

I am new to Java and JBoss and JDeveloper. My legacy project has this persistence.xml file:
<persistence-unit name="DoDSRMGR">
<jta-data-source>java:/DoDSRDS</jta-data-source>
<class>dodsr.ManifestsPass1</class>
<class>dodsr.model.ManifestsPass2</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
<property name="javax.persistence.jtaDataSource" value="java:/DoDSRDS"/>
</properties>
</persistence-unit>
</persistence>
My questions are what do the values in the file mean and what are they for? Also, where does this file belong in the EAR file META-INF or the JAR file META-INF?
What is the significance of the name="DoDSRMGR" designation, is this the name of the bean when I call from a Java program or is it the application name? Also what is the "java:/DoDSRDS" do?
Is this the way to call the bean from a desktop application:
( DodsrUserSessionEJB) ctx.lookup("/dodsr/"+ejbName+"/remote");
<persistence-unit name="DoDSRMGR"> This line lets you put a name to a persistence unit. You use the persistent unit name when you want to instantiate an EntityManager in this way:
EntityManager eMgr = Persistence.createEntityManagerFactory("Your persistence unit name").createEntityManager();
An EntityManager is the object that helps you select, persist, update and remove your JPA entities from/into the database.
<jta-data-source>java:/DoDSRDS</jta-data-source> This line tells you how you are going to manage the persistence transactions (persist, update and remove entities). If you don't specify this line, every time you want to persist, update or remove an entity from the database, you have to first get a transaction instance and call begin() after you persist/update/remove your entity and after that you call the commit() method.
Since you already have the jta-data-source element in your XML you don't need to manually call the begin() and commit() methods. Your application server manages the transactionality via a transaction resource identified by the value "java:/DoDSRDS"
This XML file can be placed in either META-INF or WEB-INF folder.

JPA variable persistence units

I'm trying to develop a website using java EE, which will be deployed to a remote server, i am trying to implement JPA into the application.
For testing purposes i'd like to create a variable persistence unit, so that on the local deployment, the application will use my local mySQL server, while on the remote deployment it will use the server's provided mySQL server.
the problem however is that i'm running on glassfish locally, and jboss remote, so i can't make the resource JNDI names for the datasources the same (since jboss requires "java:/" or "java:jboss/" as a prefix, while glassfish doesn't allow :'s in JNDI names)
another problem is that i'm not simply allowed to create 2 persistence units with the same name,
i've tried making 2 different persistence units, but then the deployment fails because one of the persistence units fails to resolve.
below is my persistence.xml at this time:
<persistence-unit name="LocalPU">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/website</jta-data-source>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="both"/>
</properties>
</persistence-unit>
<persistence-unit name="RemotePU">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:/website</jta-data-source>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="both"/>
</properties>
</persistence-unit>
so my question is, is it possible to have an entitymanager resolve to EITHER of these persistence units, but not require both persistence units to be available
EDIT:
about 5 minutes after posting this question, i found an article that suggests using environment variables
however this does not seem to work within glassfish,
this persistence.xml:
<persistence-unit name="LocalPU">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>${myds}</jta-data-source>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.ddl-generation.output-mode" value="both"/>
</properties>
</persistence-unit>
and the JVM-option -Dmyds=jndi/website results in the following error:
Exception while preparing the app : Invalid resource : ${myds}__pm
com.sun.appserv.connectors.internal.api.ConnectorRuntimeException: Invalid resource : ${myds}__pm
which leads me to believe environment variables can't be parsed within glassfish (???)
after following the tips provided in the first comment above, i have concluded that the question is irrelevant, and that my issues were caused by my misunderstanding of how JNDI names are displayed/parsed differently between glassfish, jboss, and JPA (in my case this has switched to hibernate, since openshift's Jboss servers turned out not to support eclipselink after all)
Glassfish, names the JNDI resource as jdbc/website, yet parses it as java:jdbc/website
Jboss on the other hand, requires the "java:" prefix to be defined explicitly, so in order to adress the same data source, it should be named java:jdbc/website
i'm not entirely sure how JPA/Java EE parse the JNDI names internally, but it seems as though either version works, so both java:jdbc/website AND jdbc/website would succesfully connect to the datasource defined in both the glassfish and the jboss environment.
at least i've been able to succesfully build to both jboss and glassfish using the java:jdbc/website datasource name, and i've had the same result with jdbc/website

'hibernate.dialect' must be set when no Connection available error

I am getting the following error when using Hibernate:
'hibernate.dialect' must be set when no Connection available
And I am using a datasource for database connection.
The issue could be that you haven't installed the client library for the database you are trying to connect to.
I have a Spring application that does not have a persistence.xml file and therefore no hibernate.dialect declaration.
Once I installed the MySQL Connector/J client library the error went away.
EDIT: I've also gotten this error when the database server wasn't running. Something that often happens now that I run my MySQL server through MAMP.
You will get this issue even if you have your configuration files having proper value but you don't configure it in the code.
I was explaining hibernate, I forgot to use configure() before buildSessionFactory() so I was getting the error.
You might want to recheck it.
Previous code which was giving me error
SessionFactory factory = new Configuration().buildSessionFactory();
Changed code No Error
SessionFactory factory = new Configuration().configure().buildSessionFactory();
This error is hibernate doing a poor job of telling you what went wrong with your attempted connection with database.
In my case it was as simple as having a wrong password in config file.
You need to set the property
hibernate.dialect
In the hibernate (persistence.xml or bean declaration) configuration, the value depends on your database, for example:
Postgres: org.hibernate.dialect.PostgreSQL82Dialect
Oracle: org.hibernate.dialect.Oracle10gDialect
All posible options are listen here
For example, a sample persistence.xml looks like:
<persistence-unit>
...
<properties>
...
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
...
</properties>
</persistence-unit>
Just encountered this issue. In my case it was the hibernate.dialect configuration.I added the following to SessionFatcory config in spring context file:
<bean id="mySessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="annotatedClasses">
<list>
<value>com.testapp.service.geolocation.LocationData</value>
<value>com.testapp.service.profiles.Profile</value>
</list>
</property>
<property name="hibernateProperties">
<value>hibernate.dialect=org.hibernate.dialect.HSQLDialect</value>
</property>
</bean>
I had this problem too. The reason was missing <property name="dataSource" ref="dataSource" /> element in <bean id="sessionFactory"> definition.
In some cases just using a wrong name for the database results in this Exception. Hibernate is apparently trying to determine the dialect before doing anything else, and as the DB cannot be reached, the error message comes from the part responsible for the dialect select. Bad error messaging on part of Hibernate.
In the Hibernate configuration dialog has a tab "options" it is possible to select some.
In this case I was using Oracle Enterprise Pack for Eclipse that already had configured connector, but was still getting the error. Select an option on the list was enough to solve.
I had the same errors.
My problem was that I put the hibernate.properties under one package instead of the src.
So my solution to my problem was moving hibernate.properties from package to src.

configure Hibernate to use a renamed persistence.xml

We have to rename persistence.xml to fool WebSphere 7 not to use its built-in OpenJPA.
It is pretty easy to do when you use Spring, you just instruct its entity manager factory to use another location for persistence.xml:
<property name="persistenceXmlLocation" value="META-INF/persistence-xxx.xml"/>
But now we want to use plain Hibernate/JPA without Spring, and couldn't find any way to specify the alternate persistence.xml location.
JPA2 spec doesn't say anything about it...
Any clues? Is it possible to instruct Hibernate to use a renamed persistence.xml?
======
It appears that it is NOT POSSIBLE to make Hibernate read a renamed persistence.xml file.
And not necessary in my case.
As far as I know, it's not possible to change the location of the persistence.xml file.
Also take a look at this document: Alternate JPA Providers in WebSphere Application Server, it seems like you should be able to use Hibernate as JPA provider by specifying it in the persistence.xml file, and embedding the required jars in your application.
Make sure your persistence.xml is specifying Hibernate as JPA provider:
<persistence>
<persistence-unit name="myapp">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
You should also be able to achieve this by creating a shared library and using it to configure WebSphere to use an alternative persistence provider.
Here you can find how to do it: Configuring the Java Persistence API (JPA) default persistence provider
EDIT
Given the information in the comments in this answer, it seems the problem can be solved by adding these properties in persistence.xml, as indicated in this post Websphere EntityManagerFactory creation problem:
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.WebSphereExtendedJTATransactionLookup" />
<property name="hibernate.transaction.factory_class"
value="org.hibernate.transaction.CMTTransactionFactory" />
This same information is also provided in the Alternate JPA Providers in WebSphere Application Server document.
the persistence.xml should exist in a META-INF directory, usually packaged alongside a jar file that contains your entity classes. What I do is I have the entity clases in a seperate project under eclipse, with a META-INF directory that contains the persistence.xml, package this a jar file, and include it in the applications project dependencies (ie. WEB-INF/lib), or, deploy it straight to the app server.

HSQLdb permissions regarding OpenJPA

I'm (still) having loads of issues with HSQLdb & OpenJPA.
Exception in thread "main" <openjpa-1.2.0-r422266:683325 fatal store error> org.apache.openjpa.persistence.RollbackException: user lacks privilege or object not found: OPENJPA_SEQUENCE_TABLE {SELECT SEQUENCE_VALUE FROM PUBLIC.OPENJPA_SEQUENCE_TABLE WHERE ID = ?} [code=-5501, state=42501]
at org.apache.openjpa.persistence.EntityManagerImpl.commit(EntityManagerImpl.java:523)
at model_layer.EntityManagerHelper.commit(EntityManagerHelper.java:46)
at HSQLdb_mvn_openJPA_autoTables.App.main(App.java:23)
The HSQLdb is running as a server process, bound to port 9001 at my local machine. The user is SA. It's configured as follows:
<persistence-unit name="HSQLdb_mvn_openJPA_autoTablesPU"
transaction-type="RESOURCE_LOCAL">
<provider>
org.apache.openjpa.persistence.PersistenceProviderImpl
</provider>
<class>model_layer.Testobjekt</class>
<class>model_layer.AbstractTestobjekt</class>
<properties>
<property name="openjpa.ConnectionUserName" value="SA" />
<property name="openjpa.ConnectionPassword" value=""/>
<property name="openjpa.ConnectionDriverName"
value="org.hsqldb.jdbc.JDBCDriver" />
<property name="openjpa.ConnectionURL"
value="jdbc:hsqldb:hsql://localhost:9001/mydb" />
<!--
<property name="openjpa.jdbc.SynchronizeMappings"
value="buildSchema(ForeignKeys=true)" />
-->
</properties>
</persistence-unit>
I have made a successful connection with my ORM layer. I can create and connect to my EntityManager.
However each time I use
EntityManagerHelper.commit();
It fail with that error, which makes no sense to me. SA is the Standard Admin user I used to create the table. It should be able to persist as this user into hsqldb.
edit: after hours of debugging I found out why this fails. This kind of error message also appears if you do not set required table entries (NOT NULL). It didn't indicate that for me. It seems the OpenJPA layer mistakes not being able to insert statements because of missing entries for permission problems. I simply accepted the first answer therefore. Thanks for reading :)
I have the impressoin that HSQL has no rights to write its datafile in the configured directory.
That happens to me all the time when I test my server manually as root/Administrator and that when starting it as a daemon/service it changes to a less privileged user. Then the files are owned by another user as the server is running as.
It could be other reasons : on Windows I had it when another process (another server instance) was still clinging on to the files, or even when eclipse in its infinite wisdom decided to index the database.

Categories

Resources