EclipseLink/Examples/JPA/SyBase

From Eclipsepedia

Jump to: navigation, search

How do we integrate EclipseLink with Sybase?

Lets start with the persistence unit configuration for a simple Java SE application managed example. We require access to the JConnect 5.5 JDBC driver jar or Sybase Adaptive Server Enterprise JDBC driver - usually your /sybase/jConnect-6_0/classes/jconn3.jar

<?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="sybase_remote" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <class>com.oacis.example.jpa.model.BugNumber</class>
    <properties>
      <property name="javax.persistence.jdbc.driver" value="com.sybase.jdbc3.jdbc.SybDriver"/> 
      <property name="eclipselink.target-database" value="Sybase"/> 
      <property name="javax.persistence.jdbc.url" value="jdbc:sybase:Tds:server:5100"/>
      <property name="javax.persistence.jdbc.user" value="user"/>
      <property name="javax.persistence.jdbc.password" value="pass"/>
      <property name="eclipselink.logging.level" value="ALL"/> 
      <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
      <property name="eclipselink.ddl-generation.output-mode" value="both"/>
      <!-- enable SQL parameter binding visibility logging to override ER 329852 -->
      <property name="eclipselink.logging.parameters" value="true"/>
      <!-- Hibernate DDL Generation - for reference -->   
      <!-- http://docs.jboss.org/hibernate/core/3.3/reference/en/html/sessionconfiguration.html#configuration-logging -->   
      <!-- http://netbeans.org/kb/docs/web/hibernate-jpa.html -->   
      <property name="hibernate.show_sql" value="true"/>   
      <property name="hibernate.hbm2ddl.auto" value="create-drop"/> 
    </properties>
  </persistence-unit>
</persistence>

Minimal Source

  • No Transaction code (only a query at thes point) no MVC separation via Locator, ApplicationContext or any EE container managed @Session or @PersistenceContext annotions.
  • Note: for a complete Java EE 6 distributed application example involving JPA refer to my Distributed tutorial page
public class SEClient {
 
    // Application managed EMF and EM
    private EntityManagerFactory emf  = null;
    private EntityManager entityManager = null;
    // Reference the database specific persistence unit in persistence.xml
    public static final String PU_NAME_CREATE = "sybase_remote";    
    private List@lt;bugnumber@gt; bugs;
 
    private void initialize(String puName) {
        Metamodel metamodel = null;
        try {
            // Initialize an application managed JPA emf and em via META-INF
            emf  = Persistence.createEntityManagerFactory(puName);
            System.out.println("Metamodel: " + emf.getMetamodel());            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public void finalize() {
        // close JPA
        try {
            if(null != entityManager) {
                entityManager.close();
                emf.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public void connect() {
        // JPA 1.0
        Query query = getEntityManager().createQuery("select object(c) from BugNumber c");
        try {
            bugs = query.getResultList();
        } catch (NoResultException nre) {
            System.out.println("_collatz: " + System.currentTimeMillis() + ": server was redeployed mid-session: computeGrid is null");
 
        }
    }
 
 public static void main(String[] args) {
  SEClient client = new SEClient();
  client.initialize(PU_NAME_CREATE);
  client.connect();
 }
 
 public EntityManagerFactory getEmf() {  return emf; }
 public void setEmf(EntityManagerFactory emf) {  this.emf = emf; }
 
 public EntityManager getEntityManager() {
        if(null == entityManager) {
            entityManager = emf.createEntityManager();
        }
        return entityManager;
 }
 
 public void setEntityManager(EntityManager entityManager) {  this.entityManager = entityManager; } 
}
 
@Entity
@Table(name="JPA_BUGNUMBER")
public class BugNumber implements Serializable {
    private static final long serialVersionUID = 3132063814489287035L;
 
    @Id
    // keep the sequence column name under 30 chars to avoid an ORA-00972   
    @SequenceGenerator(name="JPA_SEQUENCE_BUGN", sequenceName="JPA_BUGN_SEQ", allocationSize=15)
    @GeneratedValue(generator="JPA_SEQUENCE_BUGN")
    @Column(name="BUG_ID")    
    private Long id;
 
    @Version
    @Column(name="BUG_VERSION")
    private int version;
 
    @Basic
    private long number;
    private int priority;
    private boolean assignedStatus;
    private String assignedTo;
 
    public BugNumber() {            }
 
    public BugNumber(String aNumber) {
        number = Integer.parseInt(aNumber);
    }
 
    public char[] getCharDigits() {
        return Long.toString(number).toCharArray();
    }
 
    public List<integer> getIntDigits() {
        List<integer> digits = new ArrayList<integer>();
        for(Character aChar : getCharDigits()) {
            digits.add(new Integer(Character.getNumericValue(aChar.charValue())));            
        }
        return digits;
    }
 
    public int getLastDigit() {
        List<integer> numbers = getIntDigits();        
        return numbers.get(numbers.size() - 1);
    }
 
    public long getNumber() {        return number;    }
    public void setNumber(long number) {        this.number = number;    }
    public int getPriority() {        return priority;    }
    public void setPriority(int priority) {        this.priority = priority;    }
    public boolean isAssignedStatus() {        return assignedStatus;    }
    public void setAssignedStatus(boolean assignedStatus) {        this.assignedStatus = assignedStatus;    }
    public String getAssignedTo() {        return assignedTo;    }
    public void setAssignedTo(String assignedTo) {        this.assignedTo = assignedTo;    }
    public Long getId() {        return id;    }
    public void setId(Long id) {        this.id = id;    }
    public int getVersion() {        return version;    }
    public void setVersion(int version) {        this.version = version;    }
}

Console Log output:

[EL Finest]: 2011-05-03 09:46:19.777--ServerSession(27379847)--Thread(Thread[main,5,main])--Begin predeploying Persistence Unit sybase_remote; session file:/D:/wse/view_w36b/com.corp.example.jpa.SEClient/bin/_sybase_remote; state Initial; factoryCount 0
[EL Finest]: 2011-05-03 09:46:19.792--ServerSession(27379847)--Thread(Thread[main,5,main])--property=eclipselink.orm.throw.exceptions; default value=true
[EL Finest]: 2011-05-03 09:46:19.824--ServerSession(27379847)--Thread(Thread[main,5,main])--property=eclipselink.jpa.uppercase-column-names; default value=false
[EL Finer]: 2011-05-03 09:46:19.824--ServerSession(27379847)--Thread(Thread[main,5,main])--Searching for default mapping file in file:/D:/wse/view_w36b/com.corp.example.jpa.SEClient/bin/
[EL Finer]: 2011-05-03 09:46:19.839--ServerSession(27379847)--Thread(Thread[main,5,main])--Searching for default mapping file in file:/D:/wse/view_w36b/com.corp.example.jpa.SEClient/bin/
[EL Config]: 2011-05-03 09:46:19.964--ServerSession(27379847)--Thread(Thread[main,5,main])--The access type for the persistent class [class com.corp.example.jpa.model.BugNumber] is set to [FIELD].
[EL Config]: 2011-05-03 09:46:19.996--ServerSession(27379847)--Thread(Thread[main,5,main])--The alias name for the entity class [class com.corp.example.jpa.model.BugNumber] is being defaulted to: BugNumber.
[EL Config]: 2011-05-03 09:46:20.011--ServerSession(27379847)--Thread(Thread[main,5,main])--The column name for element [field assignedStatus] is being defaulted to: ASSIGNEDSTATUS.
[EL Config]: 2011-05-03 09:46:20.011--ServerSession(27379847)--Thread(Thread[main,5,main])--The column name for element [field priority] is being defaulted to: PRIORITY.
[EL Config]: 2011-05-03 09:46:20.011--ServerSession(27379847)--Thread(Thread[main,5,main])--The column name for element [field assignedTo] is being defaulted to: ASSIGNEDTO.
[EL Config]: 2011-05-03 09:46:20.011--ServerSession(27379847)--Thread(Thread[main,5,main])--The column name for element [field number] is being defaulted to: NUMBER.
[EL Finest]: 2011-05-03 09:46:20.011--ServerSession(27379847)--Thread(Thread[main,5,main])--End predeploying Persistence Unit sybase_remote; session file:/D:/wse/view_w36b/com.corp.example.jpa.SEClient/bin/_sybase_remote; state Predeployed; factoryCount 0
[EL Finer]: 2011-05-03 09:46:20.011--Thread(Thread[main,5,main])--JavaSECMPInitializer - transformer is null.
[EL Finest]: 2011-05-03 09:46:20.011--ServerSession(27379847)--Thread(Thread[main,5,main])--Begin predeploying Persistence Unit sybase_remote; session file:/D:/wse/view_w36b/com.corp.example.jpa.SEClient/bin/_sybase_remote; state Predeployed; factoryCount 0
[EL Finest]: 2011-05-03 09:46:20.011--ServerSession(27379847)--Thread(Thread[main,5,main])--End predeploying Persistence Unit sybase_remote; session file:/D:/wse/view_w36b/com.corp.example.jpa.SEClient/bin/_sybase_remote; state Predeployed; factoryCount 1
[EL Finest]: 2011-05-03 09:46:20.027--ServerSession(27379847)--Thread(Thread[main,5,main])--Begin deploying Persistence Unit sybase_remote; session file:/D:/wse/view_w36b/com.corp.example.jpa.SEClient/bin/_sybase_remote; state Predeployed; factoryCount 1
[EL Finer]: 2011-05-03 09:46:20.042--ServerSession(27379847)--Thread(Thread[main,5,main])--Could not initialize Validation Factory. Encountered following exception: javax.validation.ValidationException: Unable to find a default provider
[EL Finest]: 2011-05-03 09:46:20.042--ServerSession(27379847)--Thread(Thread[main,5,main])--property=eclipselink.logging.level; value=ALL; translated value=ALL
[EL Finest]: 2011-05-03 09:46:20.042--ServerSession(27379847)--Thread(Thread[main,5,main])--property=eclipselink.logging.level; value=ALL; translated value=ALL
[EL Finest]: 2011-05-03 09:46:20.042--ServerSession(27379847)--Thread(Thread[main,5,main])--property=javax.persistence.jdbc.user; value=dblfwk
[EL Finest]: 2011-05-03 09:46:20.042--ServerSession(27379847)--Thread(Thread[main,5,main])--property=javax.persistence.jdbc.password; value=xxxxxx
[EL Finest]: 2011-05-03 09:46:20.292--ServerSession(27379847)--Thread(Thread[main,5,main])--property=eclipselink.target-database; value=Sybase; translated value=org.eclipse.persistence.platform.database.SybasePlatform
[EL Finest]: 2011-05-03 09:46:20.292--ServerSession(27379847)--Thread(Thread[main,5,main])--property=javax.persistence.jdbc.driver; value=com.sybase.jdbc3.jdbc.SybDriver
[EL Finest]: 2011-05-03 09:46:20.292--ServerSession(27379847)--Thread(Thread[main,5,main])--property=javax.persistence.jdbc.url; value=jdbc:sybase:Tds:server:5000
[EL Info]: 2011-05-03 09:46:20.308--ServerSession(27379847)--Thread(Thread[main,5,main])--EclipseLink, version: Eclipse Persistence Services - 2.1
[EL Config]: 2011-05-03 09:46:20.308--ServerSession(27379847)--Connection(13366030)--Thread(Thread[main,5,main])--connecting(DatabaseLogin(
 platform=>SybasePlatform
 user name=> "user"
 datasource URL=> "jdbc:sybase:Tds:server:5000"
))
[EL Config]: 2011-05-03 09:46:20.433--ServerSession(27379847)--Connection(29194312)--Thread(Thread[main,5,main])--Connected: jdbc:sybase:Tds:server:5000
 User: user
 Database: Adaptive Server Enterprise  Version: Adaptive Server Enterprise/15.x/*/Fri *
 Driver: jConnect (TM) for JDBC (TM)  Version: jConnect (TM) for JDBC(TM)/6.0x* 2010
[EL Finest]: 2011-05-03 09:46:20.433--ServerSession(27379847)--Connection(29194312)--Thread(Thread[main,5,main])--Connection acquired from connection pool [default].
[EL Finest]: 2011-05-03 09:46:20.433--ServerSession(27379847)--Connection(29194312)--Thread(Thread[main,5,main])--Connection released to connection pool [default].
[EL Finest]: 2011-05-03 09:46:20.449--ServerSession(27379847)--Thread(Thread[main,5,main])--sequencing connected, state is NoPreallocation_State
[EL Finest]: 2011-05-03 09:46:20.449--ServerSession(27379847)--Thread(Thread[main,5,main])--sequence JPA_BUGN_SEQ: preallocation size 15
[EL Info]: 2011-05-03 09:46:20.48--ServerSession(27379847)--Thread(Thread[main,5,main])--file:/D:/wse/view_w36b/com.o.example.jpa.SEClient/bin/_sybase_remote login successful
[EL Finest]: 2011-05-03 09:46:20.761--ServerSession(27379847)--Thread(Thread[main,5,main])--Execute query DataModifyQuery(sql="DROP TABLE JPA_BUGNUMBER")
[EL Finest]: 2011-05-03 09:46:20.761--ServerSession(27379847)--Connection(29194312)--Thread(Thread[main,5,main])--Connection acquired from connection pool [default].
[EL Fine]: 2011-05-03 09:46:20.761--ServerSession(27379847)--Connection(29194312)--Thread(Thread[main,5,main])--DROP TABLE JPA_BUGNUMBER
[EL Fine]: 2011-05-03 09:46:20.824--ServerSession(27379847)--Thread(Thread[main,5,main])--SELECT 1
[EL Finest]: 2011-05-03 09:46:20.824--ServerSession(27379847)--Connection(29194312)--Thread(Thread[main,5,main])--Connection released to connection pool [default].
[EL Warning]: 2011-05-03 09:46:20.824--ServerSession(27379847)--Thread(Thread[main,5,main])--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.1: org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.sybase.jdbc3.jdbc.SybSQLException: Cannot drop the table 'JPA_BUGNUMBER', because it doesn't exist in the system catalogs.

Error Code: 3701
Call: DROP TABLE JPA_BUGNUMBER
Query: DataModifyQuery(sql="DROP TABLE JPA_BUGNUMBER")
[EL Finest]: 2011-05-03 09:46:20.824--ServerSession(27379847)--Thread(Thread[main,5,main])--Execute query DataModifyQuery(sql="CREATE TABLE JPA_BUGNUMBER (BUG_ID NUMERIC(19) IDENTITY NOT NULL, ASSIGNEDSTATUS BIT default 0 NOT NULL, ASSIGNEDTO VARCHAR(255) NULL, NUMBER NUMERIC(19) NULL, PRIORITY INTEGER NULL, BUG_VERSION INTEGER NULL, PRIMARY KEY (BUG_ID))")
[EL Finest]: 2011-05-03 09:46:20.824--ServerSession(27379847)--Connection(29194312)--Thread(Thread[main,5,main])--Connection acquired from connection pool [default].
[EL Fine]: 2011-05-03 09:46:20.839--ServerSession(27379847)--Connection(29194312)--Thread(Thread[main,5,main])--CREATE TABLE JPA_BUGNUMBER (BUG_ID NUMERIC(19) IDENTITY NOT NULL, ASSIGNEDSTATUS BIT default 0 NOT NULL, ASSIGNEDTO VARCHAR(255) NULL, NUMBER NUMERIC(19) NULL, PRIORITY INTEGER NULL, BUG_VERSION INTEGER NULL, PRIMARY KEY (BUG_ID))
[EL Finest]: 2011-05-03 09:46:20.933--ServerSession(27379847)--Connection(29194312)--Thread(Thread[main,5,main])--Connection released to connection pool [default].
[EL Finer]: 2011-05-03 09:46:20.949--ServerSession(27379847)--Thread(Thread[main,5,main])--Canonical Metamodel class [com.corp.example.jpa.model.BugNumber_] not found during initialization.
[EL Finest]: 2011-05-03 09:46:20.949--ServerSession(27379847)--Thread(Thread[main,5,main])--End deploying Persistence Unit sybase_remote; session file:/D:/wse/view_w36b/com.corp.example.jpa.SEClient/bin/_sybase_remote; state Deployed; factoryCount 1
Metamodel: MetamodelImpl@31706449 [ 6 Types: , 1 ManagedTypes: , 1 EntityTypes: , 0 MappedSuperclassTypes: , 0 EmbeddableTypes: ]
[EL Finer]: 2011-05-03 09:46:21.136--ServerSession(27379847)--Thread(Thread[main,5,main])--client acquired: 3916915
[EL Finer]: 2011-05-03 09:46:21.152--ClientSession(3916915)--Thread(Thread[main,5,main])--acquire unit of work: 19647819
[EL Finest]: 2011-05-03 09:46:21.152--UnitOfWork(19647819)--Thread(Thread[main,5,main])--Execute query ReadAllQuery(referenceClass=BugNumber sql="SELECT BUG_ID, ASSIGNEDSTATUS, ASSIGNEDTO, NUMBER, PRIORITY, BUG_VERSION FROM JPA_BUGNUMBER")
[EL Finest]: 2011-05-03 09:46:21.152--ServerSession(27379847)--Connection(29194312)--Thread(Thread[main,5,main])--Connection acquired from connection pool [default].
[EL Fine]: 2011-05-03 09:46:21.152--ServerSession(27379847)--Connection(29194312)--Thread(Thread[main,5,main])--SELECT BUG_ID, ASSIGNEDSTATUS, ASSIGNEDTO, NUMBER, PRIORITY, BUG_VERSION FROM JPA_BUGNUMBER
[EL Finest]: 2011-05-03 09:46:21.152--ServerSession(27379847)--Connection(29194312)--Thread(Thread[main,5,main])--Connection released to connection pool [default].