Thank you for visiting! If you liked the site, please add a bookmark, else add a critical comment! Would you like to visit Greece? Traditional Greek flag This real estate site is only available in Greek! The holy mountain

Tuesday 17 May 2011

Migrating an EJB 3 application to the new JDeveloper version

This new post discusses several exceptions which have occurred because of migration to the current version of JDev. As far as I can remember the application was working well with the previous version. It seems that code rots!
The first has to do with populating a primary key field with a sequence in an entity EJB:

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "BID_SEQUENCE" )
    @SequenceGenerator(name = "BID_SEQUENCE", sequenceName = "BID_SEQUENCE",
    allocationSize = 10)
       
If you get an:

 Exception Description: The sequence named [YOUR_SEQUENCE_NAME] is setup incorrectly. Its increment does not match its pre-allocation size.

 A way to avoid the exception is to  use 100 as a start value in sql create sequence statement. That will resolve the problem: by default the start value is 1, when Eclipselink attempts to use the first allocated sequence value it's negative 1 - 100 + 1, that causes the exception.

The second is more subtle to resolve, as the error message does not appear at once. If you get a mysterious error about a missing table which does not belong to the application db schema:

      Eclipse Persistence Services - 2.1.3.v20110304-r9073):
      org.eclipse.persistence.exceptions.DatabaseException Internal Exception:
      java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist
      Error Code: 942 Call: UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?
      bind => [50, SEQ_GEN] Query: DataModifyQuery
      (sql="UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?")

a way to get rid of the exception is editing the persistense.xml as follows:


<?xml version="1.0" encoding="UTF-8" ?>
<persistence 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"
             version="1.0">
  <persistence-unit name="Model">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/ActionBazaarDS</jta-data-source>
    <class>actionbazaar.persistence.Category</class>
    <class>actionbazaar.persistence.BillingInfo</class>
    <class>actionbazaar.persistence.Order</class>
    <class>actionbazaar.persistence.User</class>
    <class>actionbazaar.persistence.Bid</class>
    <class>actionbazaar.persistence.Bidder</class>
    <class>actionbazaar.persistence.Seller</class>
    <class>actionbazaar.persistence.Item</class>
    <class>actionbazaar.persistence.ContactInfo</class>
    <class>actionbazaar.persistence.ShippingInfo</class>
    <properties>
      <property name="eclipselink.target-server" value="WebLogic_10"/>
      <property name="javax.persistence.jtaDataSource"
                value="jdbc/ActionBazaarDS"/>
      <property name="eclipselink.target-database" value="Oracle11"/>
      <!--Addon in case of
      Eclipse Persistence Services - 2.1.3.v20110304-r9073):
      org.eclipse.persistence.exceptions.DatabaseException Internal Exception:
      java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist
      Error Code: 942 Call: UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?
      bind => [50, SEQ_GEN] Query: DataModifyQuery
      (sql="UPDATE SEQUENCE SET SEQ_COUNT = SEQ_COUNT + ? WHERE SEQ_NAME = ?")>
      -->
      <property name="eclipselink.ddl-generation" value="create-tables"/>
    </properties>
  </persistence-unit>
</persistence>


Thus, the new SEQUENCE table will be automatically created and the application will at last commit the insert successfully.
The morale of the story is that frequent migrations due to upgrades can cause trouble. Perhaps, applying gradual patches instead, i.e. via the jdev online extensions way and waiting for a new full release would be safer. Otherwise you might spend hours searching about solutions on the internet, experimenting with each one and certainly overheating the computer. Always make sure you are in a well ventilated room, with full air conditioning when trying to migrate, or else high temperatures will cause your code to decay so much faster! Certain laptops such as Hewlett Packard Pavilion, overheat and power off immediately without prompting for save, very frequently at warm climates. After all, you don't want your code to go rot, do you?