<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>JPA and EJB For Us</title>
	<atom:link href="http://jpaandejb.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://jpaandejb.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Sun, 04 Sep 2011 13:53:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='jpaandejb.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>JPA and EJB For Us</title>
		<link>http://jpaandejb.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://jpaandejb.wordpress.com/osd.xml" title="JPA and EJB For Us" />
	<atom:link rel='hub' href='http://jpaandejb.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Oracle Application Server 10.1.3.5 &#8211; Dependency Injection</title>
		<link>http://jpaandejb.wordpress.com/2009/10/19/oracle-application-server-10-1-3-5-dependency-injection/</link>
		<comments>http://jpaandejb.wordpress.com/2009/10/19/oracle-application-server-10-1-3-5-dependency-injection/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 06:08:10 +0000</pubDate>
		<dc:creator>bovinemagnet</dc:creator>
				<category><![CDATA[EJB]]></category>
		<category><![CDATA[J2EE]]></category>
		<category><![CDATA[Java Code]]></category>
		<category><![CDATA[OC4J 10.1.3.5]]></category>
		<category><![CDATA[Version 5]]></category>
		<category><![CDATA[10.1.3]]></category>
		<category><![CDATA[10.1.3.5]]></category>
		<category><![CDATA[2.4]]></category>
		<category><![CDATA[2.5]]></category>
		<category><![CDATA[Dependency Injection]]></category>
		<category><![CDATA[DI]]></category>
		<category><![CDATA[EJB3]]></category>
		<category><![CDATA[Null Pointer]]></category>
		<category><![CDATA[OC4J]]></category>
		<category><![CDATA[Servlet]]></category>

		<guid isPermaLink="false">http://jpaandejb.wordpress.com/?p=18</guid>
		<description><![CDATA[Issues with Oracle Application Server OC4J 10.1.3.5 and Dependence Injection<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpaandejb.wordpress.com&amp;blog=9700094&amp;post=18&amp;subd=jpaandejb&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One of the cool things about EJB&#8217;s is the fact that you no longer need to do certain things, like go to the JNDI context, and search for your EJB.  You can let the @EJB tag do it for you (for example).</p>
<p>Writing a simple application to talk to a local EJB should be as simple as</p>
<p><pre class="brush: java;">
@EJB
 private SimpleStatelessEJBLocal simpleStateless;
 ...
 simpleStateless.someLocalMethod();
 ...
</pre></p>
<p>But what I found was a null pointer exception.<br />
So taking the application down to the simplest app.</p>
<ul>
<li>One servlet.</li>
<li>One statless session bean.</li>
<li>Using Jdeveloper 10.1.3.5 (I can&#8217;t use 11g as I need oc4j 10.1.3.5, I know I can get around it.. more on that later)</li>
<li>One local OC4J 10.1.3.5 instance.</li>
</ul>
<p>Deploying this application and after much playing around, the null pointer was gone, and this error remained.</p>
<p><pre class="brush: java;">

[Oracle Containers for J2EE 10g (10.1.3.5.0) ]

9/10/19 16:52:14.779 SimpleTwoWEB: Servlet error
java.lang.ClassCastException: com.evermind.server.ejb.StatelessSessionDefaultLocalHomeImpl cannot be cast to au.edu.rmit.its.qas.ejb.SimpleStatelessEJBLocal

</pre></p>
<p>This puzzled me for an hour or so, but through an obscure post on <a title="coderanch" href="http://www.coderanch.com/t/455629/EJB-Certification-SCBCD/certification/EJB-DI-JSP" target="_blank">coderanch</a> I found the problem.  It seems that JDeveloper 10.1.3.x creates web projects in web 2.4 compliance, but in order to run DI you need to run web container version 2.5.  Testing this out, I changed the web.xml from this:</p>
<p><pre class="brush: xml;">

&lt;web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd&quot; version=&quot;2.4&quot; xmlns=&quot;http://java.sun.com/xml/ns/j2ee&quot;&gt;

</pre></p>
<p>changing to&#8230;</p>
<p><pre class="brush: xml;">

&lt;web-app xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns=&quot;http://java.sun.com/xml/ns/javaee&quot;   xmlns:web=&quot;http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;  xsi:schemaLocation=&quot;http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&quot;  id=&quot;WebApp_ID&quot; version=&quot;2.5&quot;&gt;

</pre></p>
<p>And like magic is worked.</p>
<p>Now according to two OTN Discussions:</p>
<p><em>If you have a look at the J2EE 5 spec, the classes where DI is supported are listed on page 64.</p>
<p><strong>Component classes supporting injection</strong><br />
Servlet: servlets, servlet filters, event listeners<br />
JSP: tag handlers, tag library event listeners<br />
JSF: scoped managed beans<br />
JAX-WS: service endpoints, handlers<br />
EJB: beans, interceptors<br />
Java EE platform: main class (static), login callback handler</em></p>
<p><a title="OTN Link 1" href="http://forums.oracle.com/forums/thread.jspa?threadID=434481" target="_blank"><em>OTN  Link 1</em></a></p>
<p><em><a title="OTN Link 2" href="http://forums.oracle.com/forums/thread.jspa?threadID=447310" target="_blank">OTN Link 2</a><br />
</em></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jpaandejb.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jpaandejb.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jpaandejb.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jpaandejb.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jpaandejb.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jpaandejb.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jpaandejb.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jpaandejb.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jpaandejb.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jpaandejb.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jpaandejb.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jpaandejb.wordpress.com/18/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jpaandejb.wordpress.com/18/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jpaandejb.wordpress.com/18/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpaandejb.wordpress.com&amp;blog=9700094&amp;post=18&amp;subd=jpaandejb&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jpaandejb.wordpress.com/2009/10/19/oracle-application-server-10-1-3-5-dependency-injection/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2639aef319ec353e6fb3c2e210ac598b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bovinemagnet</media:title>
		</media:content>
	</item>
		<item>
		<title>Loading an Image into a database using JPA</title>
		<link>http://jpaandejb.wordpress.com/2009/10/07/loading-an-image-into-a-database-using-jpa/</link>
		<comments>http://jpaandejb.wordpress.com/2009/10/07/loading-an-image-into-a-database-using-jpa/#comments</comments>
		<pubDate>Wed, 07 Oct 2009 03:59:21 +0000</pubDate>
		<dc:creator>bovinemagnet</dc:creator>
				<category><![CDATA[Java Code]]></category>
		<category><![CDATA[JPA]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[blob]]></category>
		<category><![CDATA[byte]]></category>
		<category><![CDATA[clob]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[DB]]></category>
		<category><![CDATA[derby]]></category>
		<category><![CDATA[document]]></category>
		<category><![CDATA[EclipseLink]]></category>
		<category><![CDATA[EJB3]]></category>
		<category><![CDATA[Entity]]></category>
		<category><![CDATA[Image]]></category>
		<category><![CDATA[insert]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JPG]]></category>
		<category><![CDATA[lob]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[OpenJPA]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[PDF]]></category>

		<guid isPermaLink="false">http://jpaandejb.wordpress.com/?p=5</guid>
		<description><![CDATA[How to import an image into a database via JPA/EJB3<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpaandejb.wordpress.com&amp;blog=9700094&amp;post=5&amp;subd=jpaandejb&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Loading an image (or any file)  into a database via JPA is quite easy actually, here is some code to help you out.  Please note that this is quick dirty code, and you might consider a lot of changes were you to use it.  But the idea is there, and will allow you  see how it goes.</p>
<p>Firstly one needs to create their entity class</p>
<p><pre class="brush: java;">

@Entity
public class ImageExample implements Serializable {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  private Long id;

  private String MimeType;  // Useful to store the mime type incase you want to send it back via a servlet.

  // We mark up the byte array with a long object datatype, setting the fetch type to lazy.
  @Lob
  @Basic(fetch=FetchType.LAZY) // this gets ignored anyway, but it is recommended for blobs
  protected  byte[]  imageFile;
</pre></p>
<p>Using a simple bit of Java code taken from the Java Developers Almanac, we read the file and return it as a byte array.</p>
<p><pre class="brush: java;">
public byte[] readImageOldWay(File file) throws IOException
{
  Logger.getLogger(Main.class.getName()).log(Level.INFO, &quot;[Open File] &quot; + file.getAbsolutePath());
  InputStream is = new FileInputStream(file);
  // Get the size of the file
  long length = file.length();
  // You cannot create an array using a long type.
  // It needs to be an int type.
  // Before converting to an int type, check
  // to ensure that file is not larger than Integer.MAX_VALUE.
  if (length &gt; Integer.MAX_VALUE)
  {
    // File is too large
  }
  // Create the byte array to hold the data
  byte[] bytes = new byte[(int) length];
  // Read in the bytes
  int offset = 0;
  int numRead = 0;
  while (offset &lt; bytes.length &amp;&amp; (numRead = is.read(bytes, offset, bytes.length - offset)) &gt;= 0)
  {
    offset += numRead;
  }
  // Ensure all the bytes have been read in
  if (offset &lt; bytes.length)
  {
    throw new IOException(&quot;Could not completely read file &quot; + file.getName());
  }
  // Close the input stream and return bytes
  is.close();
  return bytes;
}
</pre></p>
<p>We create an entity object and use the entity manager.</p>
<p><pre class="brush: java;">
/*
 * Put a JPG image into the db
 */
ImageExample myExample = new ImageExample();
myExample.setMimeType(&quot;image/jpg&quot;);
file = new File(&quot;images/mushroom.jpg&quot;);
try
{
  // Lets open an image file
  myExample.setImageFile(mainCourse.readImageOldWay(file));
}
catch (IOException ex)
{
  Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}

</pre></p>
<p>Then we persist the entity object.</p>
<p><pre class="brush: java;">
  fatController.create(myExample);
</pre></p>
<p>This works easily as well for PDF documents</p>
<p><pre class="brush: java;">
/*
 * Put a PDF into the db
 */
 ImageExample myExampleTwo = new ImageExample();
 myExampleTwo.setMimeType(&quot;application/pdf&quot;);
 file = new File(&quot;images/studentexample.pdf&quot;);
 try
 {
   // Lets open an image file
   Logger.getLogger(Main.class.getName()).log(Level.INFO, &quot;[Call Read]&quot;);
   myExampleTwo.setImageFile(mainCourse.readImageOldWay(file));
   Logger.getLogger(Main.class.getName()).log(Level.INFO, &quot;[Property set]&quot;);
 }
 catch (IOException ex)
 {
   Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
 }
 fatController.create(myExampleTwo);
 Logger.getLogger(Main.class.getName()).log(Level.INFO, &quot;The number of objects is : &quot; + fatController.getImageExampleCount());
</pre></p>
<p>You can easily prove that the JPA objects were persisted to the database by reading the objects back from the database.</p>
<p><pre class="brush: java;">
/*
 * Lets read the images from JPA, and output them to the filesystem
 */
 // Get the list of images stored in the database.
 List&lt;ImageExample&gt; images = fatController.findImageExampleEntities();
 File outfile = null;
 // Go through the list returned, looking for PDF/JPG files.
 for (int i = 0; i &lt; images.size(); i++)
 {
   if (images.get(i).getMimeType().equalsIgnoreCase(&quot;application/pdf&quot;))
   {
   // write out the pdf file
     outfile = new File(&quot;out/test&quot;+images.get(i).getId()+&quot;.pdf&quot;);
     try
     {
       mainCourse.writeFile(outfile, images.get(i).getImageFile());
     }
     catch (IOException e)
     {
       Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, e);
     }
   }
   else if (images.get(i).getMimeType().equalsIgnoreCase(&quot;image/jpg&quot;))
   {
     // write out the pdf file
     outfile = new File(&quot;out/test&quot;+images.get(i).getId()+&quot;.jpg&quot;);
     try
     {
       mainCourse.writeFile(outfile, images.get(i).getImageFile());
     }
     catch (IOException e)
     {
       Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, e);
     }
   }
   else
   {
     Logger.getLogger(Main.class.getName()).log(Level.SEVERE, &quot;Unknown file type&quot;);
   }
 }
</pre></p>
<p>If you are wondering what code I used to write it to a file, it was simple like this.</p>
<p><pre class="brush: java;">
public void writeFile(File file, byte[] data) throws IOException
 {
   OutputStream fo = new FileOutputStream(file);
   // Write the data
   fo.write(data);
   // flush the file (down the toilet)
   fo.flush();
   // Close the door to keep the smell in.
   fo.close();
 }
</pre></p>
<p>Of course, if you were to use this in production you would add more error checking, and probably follow some better best practices.  I have also skipped a lot of steps, assuming you know what you are doing <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/jpaandejb.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/jpaandejb.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/jpaandejb.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/jpaandejb.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/jpaandejb.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/jpaandejb.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/jpaandejb.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/jpaandejb.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/jpaandejb.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/jpaandejb.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/jpaandejb.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/jpaandejb.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/jpaandejb.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/jpaandejb.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=jpaandejb.wordpress.com&amp;blog=9700094&amp;post=5&amp;subd=jpaandejb&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://jpaandejb.wordpress.com/2009/10/07/loading-an-image-into-a-database-using-jpa/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/2639aef319ec353e6fb3c2e210ac598b?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">bovinemagnet</media:title>
		</media:content>
	</item>
	</channel>
</rss>
