DBTools for JPA

DBTools for JPA is an library that work with JPA/Hibernate Classes that are generated by DBTools Gen

Usage

The following are some examples DBTools can be used:

  • Use manager classes to perform CRUD operations on tables (2 options: With injection or without injection)

    @Inject
    IndividualManager individualManager;  // simply "Inject" your manager (it has access to the EntityManager)
    
    public void onSaveClicked() {
        // save
        individualManager.save(individual);
    }
    
  • Add data to the database

    // create a new domain object
    Individual individual = new Individual();
    individual.setName("Jeff Campbell");
    individual.setPhone("801-555-1234");
    individual.setIndividualType(IndividualType.HEAD); // enum table example
    
    individualManager.save(individual);
    
  • Update data to the database

    Individual individual = individualManager.findByRowId(1);
    individual.setPhone("801-555-0000");
    individualManager.save(individual);
    
  • Delete data from the database

    Individual individual = individualManager.findByRowId(1);
    individualManager.delete(individual);
    
    individualManager.delete(1); // delete by primary key id
    
    // delete all
    individualManager.deleteAll();
    

DBTools Manager classes have a bunch of built-in methods that make working with tables even easier. Here is a few examples:

  • Get records

    Individual individual = individualManager.findByRowId(1);
    
    // find FIRST individual who has "555" in their phone number
    Individual individual = individualManager.findBySelection(Individual.C_PHONE + " LIKE ?", new String[]{"555"}); 
    
    
    List<Individual> allIndividuals = individualManager.findAll();
    List<Individual> allOrderedIndividuals = individualManager.findAllOrderBy(Individual.C_NAME);
    
    // find all those who have "555" in their phone number
    List<Individual> specificIndividuals = individualManager.findAllBySelection(Individual.C_PHONE + " LIKE ?, new String[]{"555"}); 
    

Setup

  1. Add dbtools-jpa dependency and maven-dbtools-plugin (DBTools Generator) plugin pom.xml file. (latest version in Maven Central Repo: http://search.maven.org/#search%7Cga%7C1%7Ca%3A%22dbtools-gen%22)

    org.dbtools dbtools-jpa org.dbtools maven-dbtools-plugin org.project.template.domain ${basedir}/src/main/database true true derby true
  2. For new projects, create initial schema.xml files (Default: new files will be created in src/main/database)

    mvn dbtools:init

  3. Define your database in src/main/database/schema.xml file. Example file:

           <table name="INDIVIDUAL">
               <field name="_id" jdbcDataType="BIGINT" increment="true" primaryKey="true" notNull="true"/>
               <field name="INDIVIDUAL_TYPE_ID" jdbcDataType="INTEGER" varName="individualType" foreignKeyTable="INDIVIDUAL_TYPE" foreignKeyField="_id" foreignKeyType="ENUM" enumerationDefault="HEAD"/>
               <field name="NAME" jdbcDataType="VARCHAR" size="255" notNull="true"/>
               <field name="BIRTH_DATE" jdbcDataType="TIMESTAMP"/>
               <field name="PHONE" jdbcDataType="VARCHAR" size="255"/>
               <field name="EMAIL" jdbcDataType="VARCHAR" size="255"/>
           </table>
       </database>
    
  4. Use DBTools Generator to generate DatabaseManager and all domain classes. Execute gradle task:

    mvn dbtools:genclasses

    ... or ...

    From IntelliJ: double-click "Maven Projects" > "Plugins" > dbtools > dbtools:genclasses

DBTools Generator will create the following files for each table (example for the Individual table):

    individual/
           Individual.java (extends IndividualBaseRecord and is used for developer customizations) (NEVER overwritten by generator)
           IndividualBaseRecord.java (contains boiler-plate code for doing CRUD operations and contains CONST names of the table and all columns (used to help writing queries)) (this file is ALWAYS overwritten by generator)

           IndividualManager.java (extends IndividualBaseManager and is used for developer customizations (such as adding new findByXXX(...) methods) (NEVER overwritten by generator)
           IndividualBaseManager.java (contains boiler-plate code for doing CRUD operations) (this file is ALWAYS overwritten by generator)

Other Projects

DBTools Query - https://github.com/jeffdcamp/dbtools-query

License

Copyright 2015 Jeff Campbell

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.