Kodo EJB: Persistence layer Framework conforming to EJB3 specification

Source: Internet
Author: User
Tags execution include inheritance mysql table name version access mysql database
Specification Kodo is the persistence Layer framework project that BEA acquired after acquiring Solarmetric company, which previously only supported JDO standards, and February 13, 2006, BEA announced the release of the latest version of the Kodo project--kodo 4.0.0 Early Access 4,kodo 4.0.0 EA4 supports the EJB3 and JDO2 two standards, in this article, we will first learn and understand the Kodo EJB to learn how to use the Kodo EJB to complete the development work.

We'll talk about how to use the Kodo EJB for development in two different ways, one through command-line tools and the other with an ant task in eclipse.

For more on EJB3 and JDO2, please check out the relevant content in the last reference resource of the article.

Why use the Kodo EJB

In the Kodo EJB framework, mappings between objects and relational databases (objects-tables, Object Properties-fields, and so on) are provided using the latest feature-annotations (Annotation) in JDK5.0, and no additional configuration files are required.

As required by the EJB3 specification, Kodo EJB supports a lightweight, persistent layer framework in common Java applications, in addition to the need to use the EJB container to meet the needs of heavyweight enterprise applications. It's just that when we use the Kodo EJB in the EJB container, we need more work to meet the requirements of the EJB container.

Download, install Kodo

Preparatory work

Since Kodo is a framework based on annotation mechanisms, we must use JDK5.0 to complete the development effort. So before downloading, installing Kodo, make sure you have downloaded and installed JDK5.0.

To demonstrate the need, we choose the MySQL database as a persistent target database, please go to www.mysql.com download the latest MySQL database installation.

Download, install Kodo

Kodo's latest version is Kodo 4.0.0 Early Access 4, now you can go to http://www.solarmetric.com/ to download the trial version, download need to register, you will get 30 days of license.

Extract the downloaded compressed file into the C:/kodo4 directory (followed by using%kodo_home% to refer to this directory) and open%kodo_home%/bin/ Kodocmd.cmd file, set the Kododir to your Kodo installation directory, and set Jdkhome to the Java installation directory.

Kodo EJB Instance

Once the above work is done, we can develop the Kodo EJB application, and the following examples will focus on developing a lightweight Kodo EJB example that supports calls in Java applications, not the EJB container, about how to configure the use in the EJB container Kodo EJB is a big subject, and the author will discuss it in detail.

Below we will use a simple example to illustrate the typical steps to creating a Kodo EJB application, in which we will create a persisted object named book that will be persisted to the local MySQL database.

Please note that the following instructions are based on the Windows 2000 operating system, and you may need to make changes if you use a different operating system.

  1. set up the engineering catalogue

    Create a directory named Kodoexamples in the C: packing, all of our class files and configurations are placed in this directory.

  2. creating a persistent class

    The new persistent class book, for simplicity, has only two properties: ID and name, where the ID is the number of the book (the number is automatically generated by the MySQL database), and the Name property represents the book. The entire code, comments, and instructions for the persisted class are as follows:
    Ackage Org.vivianj.kodo.examples.beans; Import Javax.persistence.Basic; Import Javax.persistence.Column; Import javax.persistence.Entity; Import Javax.persistence.GeneratedValue; Import Javax.persistence.GenerationType; Import Javax.persistence.Id; Import javax.persistence.Inheritance; Import Javax.persistence.InheritanceType; Import javax.persistence.Table;  /** * Book is used to characterize the objects in the system, he has two property IDs-book numbers, the book number will be automatically generated by the MySQL database name-title///* Entity annotation indicates that the class is a persisted class, and the Name property is the unique name of the entity in the query, and the default is the class name /* (name = "book")//The Name property of the table annotation specifies the name of the data table that the persisted class corresponds to, the default table name and class name are consistent, and in order to enhance the portability of the code, we recommend that you use the uppercase English letter in the Name property /* The strategy of the inheritance annotation determines the relationship between the persisted object and the datasheet, optionally including Single_table, joined, and Table_per_class, where we use the Table_per_class/* ( Strategy = inheritancetype.table_per_class) Public CLASS Book {/* ID note indicates that the field is an identity field/* Generatedvalue annotation defines how the identity field is generated. Our demo system ID is automatically generated by the MySQL database field, so select generationtype.identity * * (strategy = generationtype.identity)/* The Name property of the column annotation defines the names of the data fields for the class attribute, and it is recommended that uppercase characters be used to maximize the independence of the system and the database.  (name = "id") public int ID; /* Basic Note indicates that the property is a basic property/* The Name property of the column annotation defines the names of the data fields for the class attribute, and to maximize the independence of the system and the database, it is recommended to use uppercase/* (name = "name") public S  Tring name = NULL;
  3. Preparing the database

    Create a new database named Kodo in the MySQL database.

  4. Preparing configuration Files

    Create a new Meta-inf directory in C:kodoexamples, and then create a new Kodo.xml and Persistence.xml file in that directory.

    A The Kodo.xml file provides the details needed to access the database, the authorization (License) content required to use Kodo, and the Kodo log management at run time. <?xml version= "1.0"? > <persistence> <persistence-unit name= "" > <properties> serial number of the!--Kodo, please enter the Kodo when you download or purchase license --> <property name= "Kodo. LicenseKey "value=" 093d-bf3f-c10e-0f8f-0f00 "/>"!--The following are the information that needs to be provided when accessing the database--> <property name= "Kodo. Connectionurl "value=" Jdbc:mysql://localhost/kodo "/> <property name=" Kodo. Connectiondrivername "value=" Org.gjt.mm.mysql.Driver "/> <property name=" Kodo. Connectionusername "value=" root "/> <property name=" Kodo. Connectionpassword "value=" root/> "!--set the log level--> <property name=" Kodo during the run. Log "value=" Defaultlevel=warn, Runtime=info, tool=debug "/> </properties> </persistence-unit> </persistence> B" per Sistence.xml provides the information needed for EJB entity management, such as determining which persistence Manager to use and the persistence classes that need to be managed. <?xml version= "1.0"? > <persistence> <persistence-unit name= "" > <provider> kodo.persistence.persistenceproviderimpl</ provider>!--the persistent class--> that needs to be managed by Kodo EJB <class> Org.vivianj.kodo.exAmples.beans.Book </class> </persistence-unit> </persistence> 
  5. compiling a persisted class

    Open a Command line window, enter the%kodo_home%/bin directory, execute the kodocmd.cmd command, and then use the set classpath=%classpath%;c:/the MySQL driver file Mysql-connector-java-3.1.8-bin.jar such a way to join the classpath.

    Executes the Javac C:kodoexamplesorgvivianjkodoxampleseans*.java compilation Persistence class.

  6. Strengthening (enhancer) persistence classes

    The Kodo (enhancer) is used to adjust the persistence class to support performance optimization, lazy loading, and so on.

    We can use commands such as Kodoc C:kodoexamplesorgvivianjkodoxampleseans*.java to enforce the persistence class.

  7. Building Database Tables

    The Kodo provides a dedicated Mappingtool tool that automatically generates the SQL statements needed to create a database or directly create a datasheet based on the persistence classes and their relationships.

    Demo example, we use Kodoc C:kodoexamplesorgvivianjkodoxampleseans*.java to complete the creation of the datasheet, after the execution of the command, we access the Kodo database in MySQL, You can see that a datasheet named books has been created inside.

  8. Test

Now that all the work has been done, we can write a piece of code to test the validity of the above work, create a new Test.java in the C:kodoexamples directory, and enter the following:

Import Javax.persistence.EntityManager; Import Javax.persistence.EntityManagerFactory; Import javax.persistence.Persistence; Import Javax.persistence.PersistenceContextType; Import Org.vivianj.kodo.examples.beans.Book; The public class Test {public  static void Main (string[] args) {/   * The Entity manager for the EJB/   entitymanagerfactory EMF = Pers Istence.createentitymanagerfactory (null);   Entitymanager em = emf     . Createentitymanager (persistencecontexttype.extended);   /* Start transaction   /Em.gettransaction (). Begin ();      /* Create a new Persistent object/book book = new book   ();   /* Set the Name property of the book Object * *   book.name = "Kodo entry";   /* Persistent Object   /em.persist (book);   /* End Transaction   /Em.gettransaction (). commit ();   Em.close ();   Emf.close ();  

Execute the test class and you will see that a new record has been added to the books table.

developing the Kodo EJB in Eclipse

The above steps are based on command line and are not very convenient to operate, so we need to consider integrating the Kodo development process with the Eclipse development tool.

Analyze the whole process of developing Kodo EJB above, only step 5 to strengthen (enhancer) persistence class and 6) Generate Data tables and fields that cannot be implemented directly in Eclipse, and view the Kodo help documentation. The ant task script for the corresponding command was found in the Kodo release package, so we can use ant to complete the integration of Eclipse and Kodo.

We still use the example above to demonstrate how to develop the KODO EJB in Eclipse, we open Eclipse, create a new Kodoexamples project, and add all the jar files below%kodo_home%ib to the project's reference. Add the JDBC driver jar file for the database you are using to the project's reference. Then please refer to the above instructions to complete the first 5 steps, below we describe how to complete the ant configuration file.

  1. Basic Configuration Content

    Under the current directory of the project, create a new Build.xml file and enter the following:

         
           
          
         "  !--  prepare a common classpath path?  
          
           
           
           
           
            
           
           
        
  2. Write the tasks required to complete step 6 of the Enhanced (enhance) persistence class

    The task corresponding to this step is the Kodo.ant.PCEnhancerTask class, under%kodo_home%srckodont we can see the source code of the Pcenhancertask class. First use Taskdef to join the new task type Kodoc, and then call the task to complete the work.

    Add the following in the Build.xml file:

          
         
         
           !-- Specify the persistence class that needs to be strengthened, you can use the wildcard character *? 
           
         !--refer to the Build_classpath as defined in the previous step as classpath? 
          
          
  3. Write the tasks required to build the database table to complete step 7

The task class provided for this task in the Kodo package Kodo.jdbc.ant.MappingToolTask a bit of a problem executing in eclipse, and I modified it, mainly to modify the ClassLoader that was used to perform the task, and now it's up to the requirements.

The new Kodo.jdbc.ant.MappingToolTask class in the current project (the class in the analogy Lib file in the directory has a higher execution level) and the class code can be found in the%kodo_home%/src/kodo/jdbc/ant directory. These two lines are found in the class source code.

if (! Mappingtool.run (conf, files, flags, loader)) throw new Buildexception (_loc.get ("bad-conf", "Mappingtooltask")); Modify it to: if (! Mappingtool.run (conf, files, flags, MappingTool.class.getClassLoader ())    throw new Buildexception (_loc.get (" Bad-conf "," Mappingtooltask "));    Now, we can add the following in the Build.xml file to complete the database table for generating the persistence class in the project:  
    
      
      

Now you can open the Ant view in Eclipse and then perform the enhance and Create-schema tasks, work on enforcing the persistence class and create database tables, and finally, you can use the same test code to complete the testing of the code.

Summary

Kodo is the new persistence layer framework for BEA, which masks the details of the developer's access to the database, provides developers with a simpler programming model, significantly less development, and currently Kodo supports EJB3 and JDO2 standards. Bea promises to open up Kodo's core code in the near future.

In this article, the author explains how to complete the development of Kodo EJB in detail with a simple example, and explains how to integrate Kodo to complete the development process in eclipse, and solve a small problem that still exists in the integration, hoping to help you learn and use Kodo better.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.