hibernate jpa unit tests autodection does not work - jpa

I have the following in persistence.xml
<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
<class>com.merc.model.log.EventLogging</class>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
<!-- Scan for annotated classes and Hibernate mapping XML files -->
<property name="hibernate.archive.autodetection" value="class"/>
</properties>
</persistence-unit>
If I comment out com.merc.model.log.EventLogging, I get Unknown entity exception.
Any ideas as to why autodetection would not work

This can be caused by the fact that by default autodetection works for classes inside the same classpath item where persistence.xml is located.
So, you have separate target folders for the code itself and for the tests (for example, if you use Maven with default configuration), and if this persistence.xml ends up in tests' target folder after compilation, classes from the main target folder wouldn't be detected.
You can use <jar-file> elements to point to other classpath items that should be searched for entities.
If you use Maven, you can do it in elegant way using resource filtering:
persistence.xml:
<jar-file>${project.build.outputDirectory}</jar-file>
pom.xml:
<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
</build>

Related

Kumuluzee JPA Persistence settings outside persistence unit

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>

How the persistence.xml file is different from hibernate.conf.xml file, in which scenario we should which file

i was going through the hibernate examples, in some examples persistence.xml was used in some examples hibernate.cfg.xml was used. what is difference between these two files and how these files can affect our project behavior and in which scenario which file will be the best.
i don't have any issue with either of these files, i just want know that is there any internal behavior change in project when we use these files.
in persistence.xml we have
<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" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="hibernate" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>com.test.jpa.Student</class>
<properties>
.....property tags
</properties>
</persistence-unit>
</persistence>
in hibernate_cfg.xml file we have
<hibernate-configuration>
<session-factory>
......property tags
</session-factory>
</hibernate-configuration>
both of my project run fine when i use these files, i just want to know the difference b/w these files.
persistence.xml is the configuratoin file from JPA standard and hibernate.conf.xml is the Hibernate specific file.
It's recommended to use JPA standard as far as possible to be independent from the underlying JPA implementation.

Recreation of entity classes

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.

Get Eclipse to select a maven profile when deploying to tomcat

Can anyone tell me how I can get a profile to be picked up when running a web project from inside eclipse. This is my latest attempt, which does not work (file-dev.xml not parsed). I have a war project which has a dependency on a jar project. Th jar project contains the spring beans.
in pom.xml of jar project:
<profiles>
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>dev</env>
</properties>
</profile>
<profile>
<id>uat</id>
<properties>
<env>uat</env>
</properties>
</profile>
</profiles>
Also in the pom, I have added this to the maven-jar-plugin
<configuration>
<argLine>-Dspring.profiles.active=dev</argLine>
</configuration>
in spring config xml file:
<import resource="file.xml" />
in file.xml
<import resource="file-dev.xml" />
in file-dev.xml
<beans profile="dev">
<bean id="myBean"> etc etc </myBean>
</beans>
running mvn help:active-profiles returns " - dev"
Any idea what I cma missing? I have not tried any maven build or install yet, I just want it working in the dev environment, so we can deploy to tomcat from within eclipse first.
Thanks in advance

Setting Glassfish 4 with jdbc automatically rather to work around manually

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