Search This Blog

Showing posts with label EJB3. Show all posts
Showing posts with label EJB3. Show all posts

Thursday, 24 December 2009

Fast Way to Create a Web Service from an EJB3 Entity

Normally I would create an EJB session bean to expose my Entity queries from my Entity beans, but I thought it would be easier to just create a Java Service Facade and expose that as a web service as shown below.

1. In my project create an "Entity from table" using the new object gallery item in the EJB node. In this example we use the classic DEPT table.
2. Then edit your Dept entity to create another named query as shown below, as well as a toString method to display the bean.
....

@Entity
@NamedQueries({
@NamedQuery(name = "Dept.findAll", query = "select o from Dept o"),
@NamedQuery(name = "Dept.findByDeptno", query = "select o from Dept o where o.deptno = :deptno")
})
public class Dept
implements Serializable

...

@Override
public String toString()
{
return "Dept [" + deptno + ", " + dname + ", " + loc + "]";
}

3. From the EJB new object gallery select "Java Service Facade", in the wizard I only exposed these methods as well as a main method to test it with.
  • public List getDeptFindAll() public
  • public List getDeptFindByDeptno(Long deptno)
4. Add code to the main as follows to ensure the Java Service Facade works.

public static void main(String [] args)
{
final JavaServiceFacade javaServiceFacade = new JavaServiceFacade();
// TODO: Call methods on javaServiceFacade here...
List<Dept> deps = javaServiceFacade.getDeptFindAll();
for (Dept d: deps)
{
System.out.println(d);
}
}

5. Run it to verify it works well. Inside the persistence.xml I edited the following property (eclipselink.logging.level=INFO) to reduce the amount of output we get at runtime.

<persistence-unit name="Model-Test" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>model.Dept</class>
<properties>
<property name="eclipselink.jdbc.driver"
value="oracle.jdbc.OracleDriver"/>
<property name="eclipselink.jdbc.url"
value="jdbc:oracle:thin:@//beast.au.oracle.com:1523/linux11gr2"/>
<property name="eclipselink.jdbc.user" value="scott"/>
<property name="eclipselink.jdbc.password"
value="3E20F8982C53F4ABA825E30206EC8ADE"/>
<property name="eclipselink.logging.level" value="INFO"/>
<property name="eclipselink.target-server" value="WebLogic_10"/>
</properties>
</persistence-unit>

Output:

... [EL Warning]: 2009-12-24 11:48:40.203--Failed to get InitialContext for MBean registration: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
Dept [10, ACCOUNTING, NEW YORK]

Dept [20, RESEARCH, DALLAS]
Dept [30, SALES, CHICAGO]

Dept [40, OPERATIONS, BOSTON]

6. Now finally create a web service from the "JavaServiceFacade.java". To do this I simply invoked the "Create Java Web Service" from the object gallery or even easier use it's context menu option "Create Web Service".

Now you can test it using the "Test Web Service Wizard" option from the web service itself as now it is a Web Service as per step #6.

Friday, 1 February 2008

Incremental deployment of EJB JAR file fails in ASC

If you try to redeploy an EJB JAR file in ASC you will get a runtime error as follows.

[Dec 24, 2007 7:41:48 AM] Application Deployer for hotdeployEAR STARTS.
[Dec 24, 2007 7:41:48 AM] Stopping application : hotdeployEAR
[Dec 24, 2007 7:41:48 AM] Stopped application : hotdeployEAR
[Dec 24, 2007 7:41:48 AM] Undeploy previous deployment
[Dec 24, 2007 7:41:48 AM] Initialize /u01/oracle/product/10.1.3.2/OracleAS_pink/j2ee/pas_oc4j/applications/hotdeployEAR.ear begins...
[Dec 24, 2007 7:41:48 AM] Initialize /u01/oracle/product/10.1.3.2/OracleAS_pink/j2ee/pas_oc4j/applications/hotdeployEAR.ear ends...
[Dec 24, 2007 7:41:48 AM] Starting application : hotdeployEAR
[Dec 24, 2007 7:41:48 AM] Initializing ClassLoader(s)
[Dec 24, 2007 7:41:48 AM] Initializing EJB container
[Dec 24, 2007 7:41:48 AM] Loading connector(s)
[Dec 24, 2007 7:41:49 AM] Starting up resource adapters
[Dec 24, 2007 7:41:49 AM] Processing EJB module: anothermodule.jar
[Dec 24, 2007 7:41:49 AM] application : hotdeployEAR is in failed state
[Dec 24, 2007 7:41:49 AM] Operation failed with error: Missing class: project1.SessionEJBBean Dependent class: com.evermind.server.ejb.deployment.BeanDescriptor Loader: oc4j:10.1.3 Code-Source: /u01/oracle/product/10.1.3.2/OracleAS_pink/j2ee/home/lib/oc4j-internal.jar Configuration: in META-INF/boot.xml in /u01/oracle/product/10.1.3.2/OracleAS_pink/j2ee/home/oc4j.jar This load was initiated at hotdeployEAR.root:0.0.0 using the Class.forName() method. The missing class is not available from any code-source or loader in the system.

The way to achieve this is to use admin_client.jar and then it will work fine. Incremental EJB deployment (updateEjbModule) is not supported in ASC.

Eg: (This will work fine)

D:\jdev\oas-admin-distribution\10132\j2ee\home>java -jar admin_client.jar deployer:oc4j:opmn://papicell-au2.au.oracle.com:6007/pas_oc4j oc4jadmin welcome1 -updateEJBModule -appName hotdeployEAR -ejbModuleName anothermodule.jar -file D:\jdev\jdevprod\10133\jdev\mywork\SR18362234.600\Project1\deploy\anothermodule.jar
07/12/24 07:39:20 Notification ==>Uploading file anothermodule.jar ...

07/12/24 07:39:21 Notification ==>Installation of File anothermodule.jar completed successfully.

D:\jdev\oas-admin-distribution\10132\j2ee\home>

Friday, 6 July 2007

Dependency injection is not possible from a Struts Action class

Thanks to Debu for pointing out that when trying to use dependency injection JAVA EE 5 support limits this only to managed classes such as EJBs, Interceptors, Servlets, etc. I was attempting to do this from a Struts Action class and was not able do so, now I know why.

See Debu's blog entry for more details:
http://debupanda.blogspot.com/2007/06/injecting-pojos-and-using-resource.html

In the end I was forced into using regular JNDI lookup as follows from JDeveloper 10.1.3.1 / OC4J 10.1.3.1 from a Struts Action class:


HttpSession session = request.getSession();
ResultPager bean = (ResultPager) session.getAttribute("pagerbean");

if (bean == null)
{
try
{
Context ctx = new InitialContext();
bean = (ResultPager) ctx.lookup("ResultPager");
bean.init(_pageSize, "AllObjectsRo.countAll", "AllObjectsRo.findAll");
session.setAttribute("pagerbean", bean);
}
catch (NamingException ex)
{
throw new ServletException(ex);
}

}

Thursday, 5 July 2007

Getting a query count of records in EJB3

I was working on a small pagination demo with EJB3 and JDeveloper 10.1.3.1. I thought creating a query to count the number of records in my result set would be as simple as "select count(*)" but in EJB3 and JPQL world you can't use "select count(*)" unless you create a native query. Here is my named queries I used to get a count of my result set. In JPQL it's done with a query as follows, so no need to use a native query after all.

select COUNT(o) from AllObjectsRo o

@NamedQueries({@NamedQuery(name="AllObjectsRo.countAll",
query="select COUNT(o) from AllObjectsRo o"),
@NamedQuery(name="AllObjectsRo.findAll",
query="select o from AllObjectsRo o")
})