I'm making a Maven EJB module, and i have troubles when i run tests using JUnit and Embedded GlassFish container.
EJB module consists of JPA entity and EJB stateless session bean.
whole project can be found here
https://github.com/muhamed-hassan/service-layer
persistence.xml
<?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="custmgr-pu" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/customerCM_PU</jta-data-source>
<class>model.Customer</class>
<properties>
<property name="eclipselink.target-database" value="MySQL"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.logging.level" value="ALL"/>
</properties>
</persistence-unit>
</persistence>
I'm getting this kind of exception
`java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName custmgr-pu`
Note that i put persistence.xml file under src/main/resources/META-INF directory, because i'm using a maven ejb project.
Related
I am developing j2EE application it is web sotre I use Jboss7 , maven and wildfly my problem is that after every login database content will be deleted .. any suggestion to solve this ?
this is my persistance 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="PiDevECommerce-ejb">
<jta-data-source>java:jboss/datasources/MySQLDS</jta-data-source>
<class>ecommerce.entites.Admin</class>
<class>ecommerce.entites.Brand</class>
<class>ecommerce.entites.Category</class>
<class>ecommerce.entites.Coupon</class>
<class>ecommerce.entites.Customer</class>
<class>ecommerce.entites.Event</class>
<class>ecommerce.entites.Offre</class>
<class>ecommerce.entites.Product</class>
<class>ecommerce.entites.Publication</class>
<class>ecommerce.entites.ReclamationCoupon</class>
<class>ecommerce.entites.ReclamationOffre</class>
<class>ecommerce.entites.Review</class>
<class>ecommerce.entites.Store</class>
<class>ecommerce.entites.Storemanager</class>
<class>ecommerce.entites.Transaction</class>
<class>ecommerce.entites.TransactionPK</class>
<class>ecommerce.entites.User</class>
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
</persistence-unit>
</persistence>
The problem is in your configuration
<properties>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
</properties>
The correct value would be validate
This post answer your question completely
Click here
I am learning JPA.
I tried the Spring-JPA-Hibernate combination.
The application works and I get the results.
But though there is only 1 record in the database, it is taking about 4 to 5 minutes to load the page.
Not quite sure what I am doing wrong.
I Googled for the same but all I am getting are performance optimization suggestions but I think I am missing something more basic as there is only 1 record in my table.
Here are the details:
Persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<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="bike_unit" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:comp/env/jdbc/testdb123</jta-data-source>
<class>bike.Bike</class>
<class>bike.Company</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/bike"/>
<property name="javax.persistence.jdbc.user" value="abc"/>
<property name="javax.persistence.jdbc.password" value="abc"/>
</properties>
</persistence-unit>
</persistence>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<servlet>
<servlet-name>bikeDispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>bikeDispatcher</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/home.html</welcome-file>
</welcome-file-list>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<resource-env-ref>
<resource-env-ref-name>jdbc/testdb123</resource-env-ref-name>
<resource-env-ref-type>javax.sql.DataSource</resource-env-ref-type>
</resource-env-ref>
</web-app>
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/testdb123" auth="Container"
type="javax.sql.DataSource"
username="abc" password="abc"
driverClassName="org.apache.derby.jdbc.ClientDriver"
url="jdbc:derby://localhost:1527/bike" />
</Context>
I am using Spring 3 and Hibernate 3 jar files.
I tried replacing Hibernate with Eclipselink and the applications works perfect. Its only when I use JPA with Hibernate that I have slowness issues.
Here are the jars I used with Eclipselink and Hibernate:
Eclipselink:
eclipselink.jar
javax.persistence_2.1.0.v201304241213.jar
javax.persistence.source_2.1.0.v201304241213.jar
Hibernate:
ejb3-persistence.jar
hibernate-annotations.jar
hibernate-commons-annotations.jar
With Eclipselink, this is how my persistance.xml looks:
<?xml version="1.0" encoding="UTF-8" ?>
<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="bike_unit" transaction-type="RESOURCE_LOCAL">
<class>bike.Bike</class>
<class>bike.Company</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/bike" />
<property name="javax.persistence.jdbc.user" value="abc" />
<property name="javax.persistence.jdbc.password" value="abc" />
</properties>
</persistence-unit>
</persistence>
Not sure what I am missing.
I had this performance problem on Windows 7 Desktop.
However, I did not face this issue when tried on a Windows 8 Machine with identical code and database.
Did not find the root cause but solved my problem for now.
I wanna create a Java Application based on Hibernate-3 and Spring Framework. To get the process easy I found hibernate3-maven-plugin that is able to perform reverse-engineering of the existing database.
Here there's example of POM:
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>hibernate3-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<components>
<component>
<name>hbm2java</name>
<outputDirectory>src/main/java</outputDirectory>
<implementation>jdbcconfiguration</implementation>
</component>
<component>
<name>hbm2dao</name>
<outputDirectory>src/main/java</outputDirectory>
<implementation>jdbcconfiguration</implementation>
</component>
</components>
<componentProperties>
<revengfile>/src/main/resources/model.reveng.xml</revengfile>
<propertyfile>/src/main/resources/hibernate.properties</propertyfile>
<jdk5>true</jdk5>
<ejb3>true</ejb3>
</componentProperties>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.18</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib-nodep</artifactId>
<version>2.1_3</version>
</dependency>
</dependencies>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>hbm2java</goal>
<goal>hbm2dao</goal>
</goals>
</execution>
</executions>
</plugin>
Then I set up the context of Spring:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="gomer" />
</bean>
<bean id="entityManager" factory-bean="entityManagerFactory" factory-method="createEntityManager"/>
<bean id="user" class="ru.tomtrix.first.db.UserHome">
<property name="entityManager" ref="entityManager"/>
</bean>
</beans>
It perfectly generates an Entity file and a DAO file except the following. In DAO file there's a EntityManager:
#Stateless
public class UserHome {
private static final Log log = LogFactory.getLog(UserHome.class);
#PersistenceContext private EntityManager entityManager;
... and this field hasn't got a setter! Eventually Spring throws the exception:
Invalid property 'entityManager' of bean class [ru.tomtrix.first.db.UserHome]: Bean property 'entityManager' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
Of course it's not a good practice to write the setter manually. I think there is a way to inject a manager properly.
So how to do it without rewriting the generated file?
Corresponding information:
1) I'd like to create a stand alone application (and possibly run it in an Application Server like Tomcat)
2) model.reveng.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering SYSTEM "http://www.hibernate.org/dtd/hibernate-reverse-engineering-3.0.dtd">
<hibernate-reverse-engineering>
<table-filter match-name=".*" package="ru.tomtrix.first.db"/>
</hibernate-reverse-engineering>
3) persistence.xml:
<?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="gomer" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>User</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="1234"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/users"/>
</properties>
</persistence-unit>
</persistence>
The problem is you are missing a part of the configuration. You need to tell Spring that you want to (also) use annotations for configuration. For this add the <context:annotation-config /> to your configuration and remove the setting of the entityManager
Next to that remove the calling of the factory method spring will handle all that for you.
Added tip use the version-less schemas insead of
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="gomer" />
</bean>
<bean id="user" class="ru.tomtrix.first.db.UserHome" />
</beans>
Your code might be problematic when you deploy it to a fullblown app server and you might run into issues with Spring and the EJB container competing over control of the beans. The hibernate plugin generates #Stateless session beans which in general will be picked up by the app. server (depending on which one you use).
I need create a website in weblogic container, but i donĀ“t know how to connected jpa 2.0 to jndi weblogic connection and management with spring.
now i have one project but with errors, in this case my file setup are :
<?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="unitPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/fact</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
</properties>
</persistence-unit>
</persistence>
and the error is:
weblogic.deployment.EnvironmentException: Error processing persistence unit unitPU of module web: Error instantiating the Persistence Provider class org.eclipse.persistence.jpa.PersistenceProvider of the PersistenceUnit factory-web-copyPU: java.lang.ClassNotFoundException: org.eclipse.persistence.jpa.PersistenceProvider
any sample or any idea to help me to solved this error o create a project jpa + spring + weblogic i will be greatful
You haven't got the class: org.eclipse.persistence.jpa.PersistenceProvider in your classpath. That is what is causing the exception. So add the .jar file containing this class into your classpath or if you are using Maven add this dependency:
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>{artifact}</artifactId>
<version>{version}</version>
<scope>compile</scope>
</dependency>
And specify the appropriate artifactId.
See:
EclipseLink/Maven
I've created a spring project which I later deployed on glassfish, the ap is working at first. But when I added persistence.xml it took mins to be done with error.
<?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="sidoPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>myDatasource</jta-data-source>
<properties>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create-drop" />
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.SunONETransactionManagerLookup" />
<property name="hibernate.transaction.factory_class"
value="org.hibernate.transaction.JTATransactionFactory" />
</properties>
</persistence-unit>
</persistence>
When browse: http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd, it wouldn't load.
Any idea or work around on this issue?
I think it's related with the whole java.net, java.sun.com, project Kenai etc. being down because of the maintenance reasons.
https://blogs.oracle.com/theaquarium/entry/outage_on_java_net_apr