Why do I get an unexpected EntityManagerFactory when using JPA within WebLogic? - spring

I am trying to create a Spring MVC web app (Spring Framework 3.0.5). I am using IntelliJ IDEA 11.1.3 to deploy my app on a WebLogic Server (10.3.4). One of my web pages attempts to store some data in a database using JPA. My persistence.xml specifies:
<?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="LeaveSchedulerJPA" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.engilitycorp.leavetracker.jpa.UserRole</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:#localhost:1521:xe "/>
<property name="javax.persistence.jdbc.user" value="leavescheduler"/>
<property name="javax.persistence.jdbc.password" value="xxx"/>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
</properties>
</persistence-unit>
</persistence>
However, when I look in the debugger, my EntityManagerFactory is shown as an org.apache.openjpa.persistence.EntityManagerFactoryImpl, and when I call createEntityManager, I get an org.apache.openjpa.persistenceArgumentException that states that "A JDBC Driver or DataSource class name must be specified in the ConnectionDriverName property".
It appears to my newbie eye that the persistence.xml may not be getting processed. I've tried putting it in (project)/src/main/resources/META-INF and (project)/src/main/resources/META-INF/spring, with the same unfortunate result.
I am not committed to using Hibernate persistence; however, I do want to use something that implements JPA 2, and I am having a real hard time configuring my environment. For example, I have little idea how openjpa got involved in my app. I suppose it may be the default JPA provider for something (WebLogic?, IntelliJ IDEA?). Any help/suggestions would be much appreciated.
My web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/root-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
root-context.xml:
<?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-3.0.xsd">
</beans>
servlet-context.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure -->
<!-- Enables the Spring MVC #Controller programming model -->
<annotation-driven />
<!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory -->
<resources mapping="/resources/**" location="/resources/" />
<!-- Resolves views selected for rendering by #Controllers to .jsp resources in the /WEB-INF/views directory -->
<beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<beans:property name="prefix" value="/WEB-INF/views/" />
<beans:property name="suffix" value=".jsp" />
</beans:bean>
<context:component-scan base-package="com.engilitycorp.leavetracker" />
</beans:beans>

WebLogic 10.3.4 is Java EE 5 compliant and is shipped with JPA 1.0 implementations: OpenJPA and TopLink.
According to WebLogic documentation ( http://docs.oracle.com/cd/E17904_01/web.1111/e13720/using_toplink.htm#CIHDJHHI ) it can be used also with JPA 2.0 but only after applying a patch. Simply follow the instructions (patching seems to be quite simple but I didn't test it).
Probably you can also use your own JPA 2.0 provider without patching, as user1654209 wrote in first answer. But JPA 1.0 classes supplied with WebLogic can get in the way, because they are loaded by higher level classloader and have priority over classes packaged in your WAR file. To prevent such behaviour you have two options:
pack your application's WAR within an EAR archive with META-INF/weblogic-application.xml file containing following lines (you must also include standard META-INF/application.xml file):
<?xml version="1.0" encoding="UTF-8"?>
<weblogic-application xmlns="http://xmlns.oracle.com/weblogic/weblogic-application/1.0/weblogic-application.xsd">
<prefer-application-packages>
<package-name>javax.persistence.*</package-name>
</prefer-application-packages>
</weblogic-application>
add WEB-INF/weblogic.xml file to your WAR archive with following lines:
<?xml version='1.0' encoding='UTF-8'?>
<weblogic-web-app xmlns="http://www.bea.com/ns/weblogic/weblogic-web-app">
<container-descriptor>
<prefer-web-inf-classes>true</prefer-web-inf-classes>
</container-descriptor>
</weblogic-web-app>

You need to configure an entityManagerFactory bean in your context.xml. Heres is an exemple using eclipselink as the JPA provider
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
id="dataSource">
<property name="driverClassName" value="${database.driverClassName}" />
<property name="url" value="${database.url}" />
<property name="username" value="${database.username}" />
<property name="password" value="${database.password}" />
</bean>
<bean
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
id="entityManagerFactory">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter">
<bean id="jpaAdapter"
class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter">
</bean>
</property>
</bean>

Just setting the provider isn't enough. You need to set the database connection data, like jdbc url, username and password. Did you set it? You also need to set the jdbc class name, as said, and the jdbc driver needs to be in your classpath.
If you are running this on a container and want to configure the datasource in weblogic, you can just refer to a jndi datasource from your hibernate cfg, but you will need to enter in the weblogic console and create a datasource and a connection pool there.
Look at hibernate docs on how to set the JNDI name in persistence.xml

Related

DelegatingFilterProxy retrieving property values

I am having a web.xml with the below filter configuration.
<filter>
<filter-name>smplAuthenticationFilter</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
<init-param>
<param-name>targetFilterLifecycle</param-name>
<param-value>true</param-value>
</init-param>
</filter>
Bean definition for smplAuthenticationFilter is like below:
<?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-3.0.xsd">
<bean id="smplAuthenticationFilter" class="com.mycompany.ct.common.security.smpl.servletfilters.SmplActiveDirectoryServletFilter">
<property name="databaseLoggerTarget" value="java:jdbc/smplDataSource" />
<property name="smplSecurityCredentials" value="${adapter.security.smpl.credentials}" />
<property name="smplSecurityPrincipal" value="${adapter.security.smpl.principal}" />
<property name="smplSearchBase" value="${adapter.security.smpl.searchbase}" />
<property name="smplProviderUrl" value="${adapter.security.smpl.providerurl}" />
<property name="applicationName" value="smpl" />
</bean>
</beans>
In this, I would like to know from where the property values for smplSecurityCredentials, smplSecurityPrincipal, smplSearchBase, smplProviderUrl are set?
In otherwords, how to find the value for variables like ${adapter.security.smpl.credentials}?
Your configuration file should also have some line such as:
<context:property-placeholder location="..."/>
The location (or locations in case of multiple files) will tell you where your properties are
This placeholder declaration may not necessarily reside in the same configuration file.
It must be placed in the same context definition though.
(You may have multiple configuration files for the same context, either by using imports or by having multiple values for contextConfigLocation in your web.xml)

XMLViewresolver gives a 404: Not found exception

I'm trying to get familiar with the XMLViewResolver, so I included the following into m *-servlet.xml:
<bean class="org.springframework.web.servlet.view.XmlViewResolver">
<property name="location">
<value>/WEB-INF/spring-views.xml</value>
</property>
<property name="order" value="0" />
</bean>
The Spring-views.xml looks like this (just a snippet):
<?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.5.xsd">
<bean id="insertEntryForm" class="org.springframework.web.servlet.view.JstlView">
<property name="url" value="/WEB-INF/views/admin/insertEntryForm.jsp"></property>
</bean>
....
When I try to load this jsp it gives me the following error:
"NetworkError: 404 Not Found - http://localhost:8080/LiteratureVisualization/admin/insertEntryForm.html"
How can I solve this problem?
I want to use XMLViewResolver, because I can map multiple folders (inside the views-directory) individually... or is there a different solution for this?
If you have a web.xml configuration file in your project, check that XML file is included in context parameters:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/application-context.xml
/WEB-INF/application-security.xml
/WEB-INF/spring-pdf-views.xml
</param-value>
</context-param>
I had the same problem integrating iText library and I had to include XML file here.

Spring MVC: I set the default page but the spring always should me the configuration files do not find

I try run a basic Java EE Spring project on eclipse(jboss 7.1.1 server, Spring 3.1.2 released), but when it always print that the configuration file do not find but I Actually put the configuration file in right place. I do not configure the welcome-file, but mvc:view-controller instead.
this is the web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
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">
<display-name>springupload</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/web-application-config.xml</param-value>
</context-param>
<!-- Loads the Spring web application context -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value/>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map all *.spring requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
This is the web-application-config.xml file
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Scans for application #Components to deploy -->
<context:component-scan base-package="com.pack" />
<!-- Imports the configurations of the different infrastructure systems of the application -->
<import resource="webmvc-config.xml" />
<!-- <import resource="webflow-config.xml" /> -->
<!-- <import resource="data-access-config.xml" /> -->
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver" id="multipartResolver">
<property name="maxUploadSize" value="1000000"></property>
</bean>
</beans>
This is webmvc-config.xml
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- Enables controllers mapped with #RequestMapping annotations, formatting annotations #NumberFormat #DateTimeFormat, and JSR 303 style validation -->
<mvc:annotation-driven/>
<mvc:resources mapping="/res/**" location="/, classpath:/META-INF/web-resources/" />
<mvc:view-controller path="/" view-name="hello"/>
<mvc:default-servlet-handler />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="jspre">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="htmlre">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".html"/>
</bean>
</beans>
The error you can see in the picture:
HTTP Status 404 - /springupload/WEB-INF/webmvc-config.xml
type Status report
message /springupload/WEB-INF/webmvc-config.xml
description The requested resource (/springupload/WEB-INF/webmvc-config.xml) is not available.
JBoss Web/7.0.13.Final
I really do not know why I configure the html and jsp page, while it should some configuration file as my start page?
Your configuration is not far from being OK.
One thing I notice is that the hello.html file is in your root WebContent folder. I suppose this is the view you want rendered when you access http://localhost:8080/springupload/ because of this line in the configuration:
<mvc:view-controller path="/" view-name="hello"/>
If this is so, then Spring is trying to resolve to /WEB-INF/hello.html because of the prefix and suffix on this viewResolver :
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="htmlre">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".html"/>
</bean>
However, you have two view resolvers with no order in them, and Spring is taking only the first one which resolves to /WEB-INF/hello.jsp, hence the 404 Not found
To wrap it up your solution is to move hello.html to /WEB-INF/ and to change your viewResolver configuration in webmvc-config.xml like so
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="jspre">
<property name="order" value="2" />
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" id="htmlre">
<property name="order" value="1" />
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".html"/>
</bean>
Last, you're not supposed to access directly content in the http://localhost:8080/WEB-INF/* URL, so everything you try here will result in a 404 Not found.
change your configuration file name to [dispatcher servlet name]-servlet.xml
http://static.springsource.org/spring/docs/3.0.x/reference/mvc.html

Tomcat unable to find jsp in war file

I recently switched one of my static html files to a Spring controller that uses a JSP to render its view. I use jetty to test locally and local testing shows the page rendering fine. Upon deploying to our test server, which uses Tomcat 6.0.26, I get the following exception:
javax.servlet.ServletException: Could not get RequestDispatcher for [/WEB-INF/jsp/index.jsp]: check that this file exists within your WAR
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:219)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1060)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:798)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:716)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
I have confirmed that the JSP exists in the war I deploy and the exploded directory that tomcat creates upon deployment. Here is what my web.xml and front-controller-servlet.xml files look like respectively (web.xml shorted slightly):
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- Initialize the Spring DispatcherServlet -->
<servlet>
<servlet-name>gwtrpc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Map the DispatcherServlet to only intercept RPC requests -->
<servlet-mapping>
<servlet-name>gwtrpc</servlet-name>
<url-pattern>*.gwtrpc</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>front-controller</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>front-controller</servlet-name>
<url-pattern>/search.html</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>front-controller</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
front-controller-servlet.xml:
<?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"
xmlns:security="http://www.springframework.org/schema/security"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/search.html">indexController</prop>
</props>
</property>
</bean>
<bean id="indexController" class="com.company.search.web.server.controller.IndexController" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp"/>
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
</bean>
</beans>
The difference between my dev instance and test instance is that the dev instance is deployed to the root (localhost/search.html) whereas the test instance is deployed to server.com/appname/search.html. Appname is the name of the *.war file I deploy. I have tried adding the full path as the prefix to the jps files (/appname/WEB-INF/jsp/) and a number of other combinations with no luck. I have verified that the jsp-api jars are in the tomcat lib directory. My tomcat install is the basic/default install.
The issue was caused by the default jsp servlet being removed from the default web.xml of the tomcat install. This had been done by a former co-worker and not to my knowledge. Replacing the test server's web.xml with the default web.xml (which included url-patterns/servlets for handling jsp files) corrected the issue.

UrlFilenameViewController does not return View (Spring-MVC)

According to Spring framework API
UrlFilenameViewController's purpose is:
Transforms the virtual path of a URL into a view name and returns that view
If i request for /info.xhtml, its logical view name is info
See UrlFilenameViewController documentation
Both web.xml and controller is mapped according to
/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8">
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>smac</display-name>
<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
/WEB-INF/controller-servlet.xml
<?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.5.xsd">
<bean class="org.springframework.web.servlet.view.InternalViewResolver">
<property name="prefix" value="/WEB-INF/output/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>
</beans>
My smac app deploys fine.
So, if i request for http://127.0.0.1:8080/smac/info.xhtml, Spring-MVC should return /WEB-INF/output/info.jsp. But i have seen in console the following:
No mapping found for HTTP request with URI [/smac/info.xhtml] in DispatcherServlet with name 'controller'
Could anyone help me ?
regards,
You need to configure a HandlerMapping. Default one is BeanNameUrlHandlerMapping which will only do what you want if you bind your controller bean under the name matching your URI. You probably want the SimpleUrlHandlerMapping instead:
<bean name="myController" class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/**/*.xhtml=myController
</value>
</property>
</bean>

Resources