I got following local setting:
Eclipse Kepler
Maven 3
Glassfish 4
I want to run JSF with MySql.JSF 2.0 works fine on the server.The problem I got is the connection to the database.
I did all settings at the admin of glassfish and here is the persistence.xml I got so far:
<?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="actors" transaction-type="JTA">
<jta-data-source>jdbc/example</jta-data-source>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
Maven dependency for MySQL:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
Get a jdbc not found exception...
The question is how can I setup Glassfish to use the maven dependency so that I don't need to integrate the jdbc.jar manually ?
I am afraid according to the official documentation you have to do it this way
Related
We are trying to configure the Kumuluz JPA.
We would like to tailor the Persistence Unit programmatically and for that, we need a handle on the PersistenceUnit Properties. This is already pre-packaged inside the kumuluz jpa dependency and we have apparently no way of getting a handle on the properties at runtime.
Has anyone had the same problem of having to set the properties at runtime? Can you please share your methods?
You cannot access persistence.xml configuration in runtime, nor would it make any sense to do so, since persistence.xml is read by JPA provider only at the very beginning of the application start-up.
You can, however, configure persistence.xml during build time with maven configuration, by using Maven Resources Plugin. For example:
pom.xml:
<project>
...
<properties>
<db.action>create</db.action>
</properties>
...
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
</build>
...
</project>
persistence.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<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="kumuluzee-samples-jpa" transaction-type="JTA">
<jta-data-source>jdbc/CustomersDS</jta-data-source>
<class>com.kumuluz.ee.samples.jpa.Customer</class>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="${db.action}"/>
</properties>
</persistence-unit>
</persistence>
The issue is appearing when trying to make entityManager with entitiManagerFactory.
Application is running inside docker container and postgresql database is on localhost of the machine (not inside docker).
my 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
http://java.sun.com/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<!-- Define persistence unit -->
<persistence-unit name="mypersistenceunit">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>some.path.SimplifiedUserGroup</class>
<class>some.path.UserSettings</class>
<class>some.path.UserGroupSettings</class>
<class>some.path.UserGroup</class>
<class>some.path.AppUser</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/localdatabase" />
<property name="javax.persistence.jdbc.user" value="postgres" />
<property name="javax.persistence.jdbc.password" value="postgres" />
</properties>
</persistence-unit>
and the repository class:
public List<SimplifiedUserGroup> findAll() {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("mypersistenceunit");
entityManager = emf.createEntityManager();
return entityManager.createNamedQuery("UserGroup.findAll", SimplifiedUserGroup.class).getResultList();
}
there is an error:
javax.persistence.PersistenceException: Unable to locate persistence units
and then:
java.lang.IllegalArgumentException: Unrecognized JPA persistence.xml XSD version : ``
I tried several tutorials and read stackoverflow topics but nothing helps me - I tried with but didn't help. The same with versions 2.0, 2.1, 2.2.
I have such dependencies in my pom.xml:
dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.2.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.3.6.Final</version>
</dependency>
It would be great if I could create an entityManager and then connect to database (on localhost) and perform some queries...
Thanks!
PersistenceException :
If you use the EntityManagerFactory in a JavaEE environment you need to define the transaction-type to RESOURCE_LOCAL in your persistence.xml :
<persistence-unit name="mypersistenceunit" transaction-type="RESOURCE_LOCAL">
For more informations about the EntityManager and difference beetween transaction-type JTA and RESOURCE_LOCAL in the persistence.xml see this answer.
Unrecognized JPA persistence.xml XSD version :
In your persistence.xml :
I dont see the persistence enclosing tag </persistence> after </persistence-unit> at the end.
You are using an old URL for the XSD Schema Location, change your <persistence ... > to :
<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">
Informations from Oracle :
Starting with the 2.1 version, the Java Persistence API Schemas share the namespace, http://xmlns.jcp.org/xml/ns/persistence/. Previous versions used the namespace http://java.sun.com/xml/ns/persistence/.
I try to run a web application with hibernate, spring and jpa on netbeans 8.0.1, but now I'm stuck on this exception when compiling app...Here's the error below:
Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default- compile) on project SMSXxxxx: Compilation failure: Compilation failure:
error: Problem with Filer: Attempt to recreate a file for type com.equitel.smsmanager.entities.TextMessageContent_
error: Problem with Filer: Attempt to recreate a file for type com.smsmanager.entities.SmsUser_
Here is the persistence unit, I have only one in my project
<?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="SMSManagerPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/jboss/datasources/SMSManagerDS</jta-data-source>
<class>com.smsmanager.entities.Approval</class>
<class>com.smsmanager.entities.Changelog</class>
<class>com.smsmanager.entities.Contacts</class>
<class>com.smsmanager.entities.Dispatches</class>
<class>com.smsmanager.entities.MessageSchedule</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>
</properties>
</persistence-unit>
</persistence>
I have not managed to fix this error , could you help me fix this?
For some reasons, there is a dependency in pom.xml that in my case caused the problem. Look for it and delete it:
<dependency>
<groupId>unknown.binary</groupId>
<artifactId>hibernate-jpamodelgen-4.3.1.Final</artifactId>
<version>SNAPSHOT</version>
<scope>provided</scope>
</dependency>
This could be a bug in Netbeans.
https://netbeans.org/bugzilla/show_bug.cgi?id=183779
Check your pom.xml file maven-compiler-plugin is added or not. If it is not there add plugin as shown in below.
<plugin>
<inherited>true</inherited>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
You can find root cause by using mvn clean install -X in cmd prompt.
Check in your pom.xml file the version of java mentioned there and check which version of java is mentioned in the $JAVA_HOME
environment variable . Both should be the same.
Check this also
May be its Java Compiler issue.
May be issue with downloading the jar
The solution that worked for me:
Try to remove the :
<dependency>
<groupId>unknown.binary</groupId>
<artifactId>hibernate-jpamodelgen-4.3.1.Final</artifactId>
<version>SNAPSHOT</version>
<scope>provided</scope>
</dependency>
Then add a maven profile with activation and dependency
<profiles>
<profile>
<id>profile-JPA-Gen</id>
<activation>
<property>
<name>activate_gen</name>
<value>true</value>
</property>
</activation>
<dependency>
<groupId>unknown.binary</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>SNAPSHOT</version>
</dependency>
</profile> </profiles>
Run once mvn install -P profile-JPA-Gen only to generate the relevant classes, and then use mvn clean install.
In a Java EE project in Eclipse I've got a marker in my persistence.xml that cannot be deleted:
JAR file "lib/entities.jar" cannot be resolved
My persistence.xml file looks like:
<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="p2p">
<jta-data-source>java:/SixAoP2pDS</jta-data-source>
<jar-file>lib/entities.jar</jar-file>
<properties>
<property name="showSql" value="false"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
</properties>
</persistence-unit>
For the current location in my project structure the marker is correct. There is no jar with this name under this path. But after deployment of the ear everything is ok and works correctly.
So I don't want this error marker anymore. But when I exclude the file from validation the error reoccurs after a clean of the project.
Someone knows how to delete it?
restart your server or (be careful) kill duplicated process related to Jboss|Glassfish, or Netbeans|Eclipse.
I m trying to find good reference for how to do JPA project (with EJB 3.x).
Its been very difficult for me to find how to configure persistance unit according to jBoss. (in GlassFish its just a piece of cake).
is anyone of you know good reference
I just ported my app that I developed on NetBeans from Glassfish 3 to JBoss 6. I had to disable JTA for the persistance to work but since I don't need that at the moment something is better than nothing.
I am using container managed security and documented my configuration in this post.
I configured my JBoss 6 datasource by placing mysql-ds.xml into $JBOSS_HOME\server\default\deploy. Here are contents:
<datasources>
<local-tx-datasource>
<jndi-name>jdbc/thor_ds</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/thor</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>mjolnir</user-name>
<password>mjolnir</password>
<exception-sorter-class-name>
org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter
</exception-sorter-class-name>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
Unlike Glassfish, in JBoss 6, I had to declare each of my persistence classes explicitly. Here is 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="AvengersPU" transaction-type="RESOURCE_LOCAL">
<!--<persistence-unit name="AvengersPU" transaction-type="JTA">-->
<jta-data-source>java:/jdbc/thor_ds</jta-data-source>
<non-jta-data-source>java:/jdbc/thor_ds</non-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>
There is no difference between application servers in terms of JPA. Just do the same thing you did for Glasfish.
The only difference is configuring the DataSource - but there are enough google results for that.
After configuring your persistence.xml you should be ready to go as JPA is a Java EE standard and should be application server independent.