{"id":22,"date":"2009-11-25T17:53:33","date_gmt":"2009-11-25T17:53:33","guid":{"rendered":"http:\/\/salientsoft.co.uk\/?p=22"},"modified":"2009-12-17T13:19:12","modified_gmt":"2009-12-17T13:19:12","slug":"eclipsejpa-outside-containerglassfish-example","status":"publish","type":"post","link":"https:\/\/salientsoft.co.uk\/?p=22","title":{"rendered":"Eclipse\/JPA outside container\/Glassfish example"},"content":{"rendered":"<p align=\"center\"><strong><span style=\"text-decoration: underline;\">JPA Eclipse + GlassFish + Oracle XE \u2013 Outside EJB Container<\/span><\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\">Introduction<\/span><\/strong><\/p>\n<p>This example details the configuration of JPA running in Eclipse, using EclipseLink and GlassFish 2.1 and Oracle XE.<br \/>\nEclipselink therefore overrides the built in TopLink Essentials JPA in GlassFish 2.1.<\/p>\n<p>\u00a0<\/p>\n<p><strong><span style=\"text-decoration: underline;\">Installation<\/span><\/strong><\/p>\n<ol>\n<li>Install the latest Java JDK 1.6 release<\/li>\n<li>Install the Eclipse Galilieo Java EE package (includes EclipseLink)<\/li>\n<li>Install GlassFish 2.1<\/li>\n<li>Install the GlassFish Eclipse Plugin (see web notes about downloading\/installing this)<\/li>\n<li>Download and install Oracle XE<\/li>\n<\/ol>\n<p><strong>\u00a0<\/strong><\/p>\n<p>\u00a0<strong><span style=\"text-decoration: underline;\">Database Creation<\/span><\/strong><\/p>\n<p>In the Oracle XE database home page, click on Administration, then Manage Database Users, create database user. For testing purposes, you can give the user all roles and privileges, however you would not do this in a production environment. Note that in Oracle, creating a database user also creates an empty database schema, as the two terms are synonymous. A single Oracle XE database installation can have any number of schemas (users), all as distinct logical databases in the same database instance (container).<\/p>\n<p>\u00a0<\/p>\n<p>\u00a0<strong><span style=\"text-decoration: underline;\">Connection Pool \/ JDB Resource Creation<\/span><\/strong><\/p>\n<p>Open the GlassFish administration web page (by default this is at <a href=\"http:\/\/localhost:4848\/\">http:\/\/localhost:4848<\/a>) and log in (default is admin\/adminadmin). Open Resources, JDBC, and click on Connection Pools &#8211; the text, not the adjacent arrow. This will display the connection pools in the RHS pane. Click on New. Name the pool JPATestPool. Select a resource type of <strong>javax.sql.datasource, <\/strong>and select Oracle as the database vendor. Click Next, then in the additional properties, set the following values :-<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td width=\"284\" valign=\"top\">\n<p align=\"center\"><strong>Property Name<\/strong><\/p>\n<\/td>\n<td width=\"284\" valign=\"top\">\n<p align=\"center\"><strong>Value<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td width=\"284\" valign=\"top\">User<\/td>\n<td width=\"284\" valign=\"top\">Your XE database username<\/td>\n<\/tr>\n<tr>\n<td width=\"284\" valign=\"top\">Password<\/td>\n<td width=\"284\" valign=\"top\">Your XE database password<\/td>\n<\/tr>\n<tr>\n<td width=\"284\" valign=\"top\">URL<\/td>\n<td width=\"284\" valign=\"top\">jdbc:oracle:thin:@localhost:1521:xe<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u00a0<br \/>\nClick Save to save the new connection pool.<\/p>\n<p>Click on JDBC Resources in the left hand pane (again, the wording not the arrow), and click new to create a new resource. Select the conncetion pool just created in the dropdown list. For the JNDI name, enter jdbc\/JPATestPool and click OK to save. Note that the JNDI name given must match the name given in the &lt;non-jta-data-source&gt; element value in persistence.xml.<\/p>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\">Project Creation<\/span><\/strong><\/p>\n<ol>\n<li>Create a new JPA project with the wizard. Select \u201cGlassFish v2.1 Java EE 5\u201d as the target runtime.<\/li>\n<li>Select either Default, Utility, or minimal for the configuration \u2013 this determines which facets are added to the project. Facets can be added later via the \u201cProject Facets\u201d page in the project properties.<br \/>\nAll of the above configurations worked.<\/li>\n<li>Following this, a connectivity driver (for Oracle in this case) needs adding to Eclipse and then to the project as follows :-<br \/>\nDownload <strong>ojdbc14.jar<\/strong> from Oracle.com here :-<br \/>\n<a href=\"http:\/\/www.oracle.com\/technology\/software\/tech\/java\/sqlj_jdbc\/htdocs\/jdbc_10201.html\">http:\/\/www.oracle.com\/technology\/software\/tech\/java\/sqlj_jdbc\/htdocs\/jdbc_10201.html<\/a><\/li>\n<li>Copy the file to the glassfish\\lib folder under the GlassFish installation directory and Restart GlassFish.<br \/>\nN.B. the jar file must be placed in this location<\/li>\n<li>Click on Window, preferences, and select Data Management, Connectivity, Driver Definitions in the left pane.<\/li>\n<li>Click Add, and assuming Oracle XE is in use, select \u201cOracle Thin Driver\u201d for Oracle 10.<\/li>\n<li>Click on the Jar List tab. <strong>ojdbc14.jar <\/strong>should be listed. Browse for the above location where the actual jar was placed (&lt;GlassFish_Home&gt;\\glassfish\\lib) and select the jar from there.<br \/>\nThe properties tab allows defaults to be set up for database connectivity, but these values will be added in GlassFish when the data source is created.<\/li>\n<li>\u00a0Select Java Build Path in the project properties for the project you created above. Pick the libraries tab, and click Add Library. Click \u201cAdd Library\u201d and select \u201cConnectivity Driver Definitions\u201d from the list. Select \u201cOracle thin Driver\u201d from the drop down. If it is not present (i.e. because you did not create it correctly as above), you can click the button\/icon to the right of the drop down and add the connectivity driver to Eclipse from there. Once done, you select it in the drop down then click finish to add it to the project.<\/li>\n<\/ol>\n<p>\u00a0<\/p>\n<p>\u00a0<strong><span style=\"text-decoration: underline;\">Creating the Source Code<\/span><\/strong><\/p>\n<p>Create the following java classes in Eclipse by right clicking the \u201csrc\u201d folder of the project and selecting New,\u00a0 Class. For the package name, enter uk.co.salientsoft.jpatest.domain. For the UserInfo class (which is the persisted class), the combined package and classname must match the value given in the &lt;class&gt; element in persistence.xml.<\/p>\n<p><strong><span style=\"text-decoration: underline;\">Class: JPATest<\/span><\/strong><\/p>\n<pre>package uk.co.salientsoft.jpatest.domain;\r\nimport javax.persistence.*;\r\npublic class JPATest {\r\n\r\n\u00a0private static final String PERSISTENCE_UNIT_NAME = \"JPATest\";\r\n\u00a0private EntityManagerFactory factory;\r\n\u00a0\r\n\u00a0public static void main(String[] args) {\r\n\u00a0\u00a0JPATest jpaTest = new JPATest();\r\n\u00a0\u00a0jpaTest.addEntries();\r\n\u00a0}\r\n\r\n\u00a0public JPATest() {\r\n\u00a0\u00a0factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);\r\n\u00a0}\r\n\u00a0\r\n\u00a0public void addEntries() {\r\n\u00a0\u00a0EntityManager em = factory.createEntityManager();\r\n\r\n\u00a0\u00a0\/\/ Begin a new local transaction so that we can persist a new entity\r\n\u00a0\u00a0em.getTransaction().begin();\r\n\r\n\u00a0\u00a0\/\/ Read the existing entries\r\n\u00a0\u00a0Query q = em.createQuery(\"select u from UserInfo u\");\r\n\r\n\u00a0\u00a0\/\/ Do we have entries?\r\n\u00a0\u00a0boolean createNewEntries = (q.getResultList().size() == 0);\r\n\r\n\u00a0\u00a0\/\/ No, so lets create new entries\r\n\u00a0\u00a0if (createNewEntries) {\r\n\r\n\u00a0\u00a0\u00a0UserInfo userInfo;\u00a0\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\r\n\u00a0\u00a0\u00a0userInfo = new UserInfo();\r\n\u00a0\u00a0\u00a0userInfo.setUserName(\"Bilbo.Baggins\");\r\n\u00a0\u00a0\u00a0em.persist(userInfo);\r\n\u00a0\u00a0\u00a0userInfo = new UserInfo();\r\n\u00a0\u00a0\u00a0userInfo.setUserName(\"Frodo.Baggins\");\r\n\u00a0\u00a0\u00a0em.persist(userInfo);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\/\/ Commit the transaction, which will cause the entity to\r\n\u00a0\u00a0\/\/ be stored in the database\r\n\u00a0\u00a0em.getTransaction().commit();\r\n\u00a0\u00a0\r\n\u00a0\u00a0\/\/ It is always good practice to close the EntityManager so that\r\n\u00a0\u00a0\/\/ resources are conserved.\r\n\u00a0\u00a0em.close();\r\n\u00a0}\u00a0\r\n}<\/pre>\n<p><strong><span style=\"text-decoration: underline;\">Class: UserInfo<\/span><\/strong><\/p>\n<pre>package uk.co.salientsoft.jpatest.domain;\r\nimport java.io.Serializable;\r\nimport javax.persistence.*;\r\n\r\n\/**\r\n\u00a0* Entity implementation class for Entity: UserInfo\r\n\u00a0*\r\n\u00a0*\/\r\n@Entity\r\npublic class UserInfo implements Serializable {\r\n\u00a0\r\n\u00a0@Id\r\n\u00a0@GeneratedValue(strategy=GenerationType.AUTO)\r\n\u00a0private long userID;\r\n\u00a0private String userName;\r\n\u00a0private static final long serialVersionUID = 1L;\u00a0\r\n\u00a0public UserInfo() {\r\n\u00a0\u00a0super();\r\n\u00a0}\r\n\u00a0\r\n\u00a0public long getUserID() {\r\n\u00a0\u00a0\u00a0return this.userID;\r\n\u00a0}\r\n\r\n\u00a0public void setUserID(long userID) {\r\n\u00a0\u00a0this.userID = userID;\r\n\u00a0}\r\n\u00a0\u00a0\u00a0\r\n\u00a0public String getUserName() {\r\n\u00a0\u00a0\u00a0return this.userName;\r\n\u00a0}\r\n\r\n\u00a0public void setUserName(String userName) {\r\n\u00a0\u00a0this.userName = userName;\r\n\u00a0}\r\n}\r\n\u00a0<\/pre>\n<p><strong><span style=\"text-decoration: underline;\">Persistence.xml<\/span><\/strong><\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\r\n&lt;persistence version=\"1.0\"\r\n\u00a0xmlns=\"<a href=\"http:\/\/java.sun.com\/xml\/ns\/persistence\">http:\/\/java.sun.com\/xml\/ns\/persistence<\/a>\"\r\n   xmlns:xsi=\"<a href=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\">http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\r\n<\/a>\u00a0xsi:schemaLocation=\"<a href=\"http:\/\/java.sun.com\/xml\/ns\/persistence\">http:\/\/java.sun.com\/xml\/ns\/persistence<\/a>\r\n   <a href=\"http:\/\/java.sun.com\/xml\/ns\/persistence\/persistence_1_0.xsd\">http:\/\/java.sun.com\/xml\/ns\/persistence\/persistence_1_0.xsd<\/a>\"&gt;\r\n\u00a0&lt;persistence-unit name=\"JPATest\" transaction-type=\"RESOURCE_LOCAL\"&gt;\r\n\u00a0\u00a0&lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;\/provider&gt;\r\n\u00a0\u00a0&lt;non-jta-data-source&gt;jdbc\/JPATestPool&lt;\/non-jta-data-source&gt;\r\n\u00a0\u00a0&lt;class&gt;uk.co.salientsoft.jpatest.domain.UserInfo&lt;\/class&gt;\r\n\u00a0\u00a0&lt;properties&gt;\r\n\u00a0\u00a0\u00a0&lt;property name=\"eclipselink.target-server\" value=\"None\" \/&gt;\r\n\u00a0\u00a0\u00a0&lt;property name=\"eclipselink.logging.level\" value=\"FINEST\" \/&gt;\r\n\u00a0\u00a0\u00a0&lt;property name=\"eclipselink.session-name\" value=\"JPATestSession\" \/&gt;\r\n\r\n\u00a0\u00a0\u00a0&lt;!-- EclipseLink should create the database schema automatically --&gt;\r\n\u00a0\u00a0\u00a0&lt;property name=\"eclipselink.ddl-generation\" value=\"drop-and-create-tables\" \/&gt;\r\n\u00a0\u00a0\u00a0&lt;property name=\"eclipselink.ddl-generation.output-mode\" value=\"database\" \/&gt;\r\n\r\n\u00a0\u00a0&lt;\/properties&gt;\r\n\u00a0&lt;\/persistence-unit&gt;\r\n&lt;\/persistence&gt;<\/pre>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong><span style=\"text-decoration: underline;\">Notes\u00a0 on Persistence.xml<\/span><\/strong><\/p>\n<pre>\u00a0&lt;persistence-unit name=\"JPATest\" transaction-type=\"RESOURCE_LOCAL\"&gt;<\/pre>\n<p>The persistence unit defines a related grouping of classes, metadata etc. which are being persisted .<\/p>\n<p>\u00a0NOTE \u2013 ensure that the value matches the one passed to createEntityManagerFactory in the code, or the call causes a stack trace citing no persistence provider found \u2013 all the available persistence providers are tried and listed and they all return null. They are all tried to see if they support the given persistence unit\u00a0 (i.e. are they listed as the provider for that persistence unit in persistence.xml) hence the error.<\/p>\n<p>\u00a0Transaction-type needs to be \u201dRESOURCE_LOCAL\u201d if jpa is used outside the EJB container, in which case the datasource is defined as a &lt;non-jta-data-source&gt;. Note that the datasource in this case is still defined in GlassFish.<\/p>\n<p>The alternative for jpa usage inside the conainer , e.g. from a session bean, would be to defined transaction-type as \u201cJTA\u201d and the datasource as a &lt;jta-data-source&gt;.<\/p>\n<pre>&lt;property name=\"eclipselink.target-server\" value=\"None\" \/&gt;<\/pre>\n<p><strong>\u00a0<\/strong><\/p>\n<p><strong>Note<\/strong>\u00a0 that with JPA inside the container, the above property would have the following value :-<\/p>\n<pre>&lt;property name=\"eclipselink.target-server\" value=\"SunAS9\" \/&gt;<\/pre>\n<p>\u00a0<\/p>\n<p>When set to &#8220;SunAS9&#8221;, you <strong>must<\/strong> use a JTA data source and JTA transaction type or you get an error. When you use &#8220;None&#8221; you can use &lt;non-jta-data-source&gt; and &#8220;RESOURCE_LOCAL&#8221;, but note that you can still refer to the data source defined in GlassFish, or you can define one explicitly in persistence.xml. You could define one explicitly in persistence.xml as follows :-<\/p>\n<pre> &lt;property name=\"eclipselink.jdbc.url\" value=\"jdbc:oracle:thin:@localhost:1521:xe\"\/&gt;\r\n\u00a0&lt;property name=\"eclipselink.jdbc.user\" value=\"jpatest\"\/&gt;\r\n\u00a0&lt;property name=\"eclipselink.jdbc.password\" value=\"jpatest\"\/&gt;\r\n\u00a0&lt;property name=\"eclipselink.jdbc.driver\" value=\"oracle.jdbc.OracleDriver\"\/&gt;<\/pre>\n<p>\u00a0<\/p>\n<p>The mechanism used to drop and create the tables automatically (\u201cdrop-and-create-tables\u201d value) is jpa provider dependant (and may not always be available). When it is used with EclipseLink as above, a stack trace with a number of errors is dumped when it tries to drop non existant tables etc., and due to other issues. If you check in Oracle XE and can see the tables and the data, the errors can be ignored. The drop and create only needs to be done once to create the tables. You can remove this property from persistence.xml to prevent the tables being dropped and created each time.\u00a0Note that this can also be done directly from Eclipse via the project context menu, as detailed <strong><a title=\"http:\/\/salientsoft.co.uk\/?p=306\" href=\"http:\/\/salientsoft.co.uk\/?p=306\">here<\/a><\/strong>.<br \/>\n\u00a0<\/p>\n<p><strong><span style=\"text-decoration: underline;\">Running the application<\/span><\/strong><\/p>\n<ol>\n<li>In Eclipse, select Window, Show View, Servers. If GlassFish is not listed, right click the server window and select New Server. Then Select GlassFish 2.1 Java EE 5. Click Next, and browse for the &lt;GlassFish_Home&gt;\\glassfish directory (leave the JRE as the default).<\/li>\n<li>Right click the server in the servers view, then select start to start it if it is not running.<\/li>\n<li>Right click on JPATest.java and select Run as, then Java application.<\/li>\n<li>The application should then run and populate the database. Log in to the XE database home page as the user\/schema you created, and click on the object browser.<\/li>\n<li>Click on the UserInfo table and you should see the rows which were created.<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>JPA Eclipse + GlassFish + Oracle XE \u2013 Outside EJB Container Introduction This example details the configuration of JPA running in Eclipse, using EclipseLink and GlassFish 2.1 and Oracle XE. Eclipselink therefore overrides the built in TopLink Essentials JPA in GlassFish 2.1. \u00a0 Installation Install the latest Java JDK 1.6 release Install the Eclipse Galilieo [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[9,10,182,15],"_links":{"self":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/22"}],"collection":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=22"}],"version-history":[{"count":31,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/22\/revisions"}],"predecessor-version":[{"id":24,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/22\/revisions\/24"}],"wp:attachment":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}