DBTools JPA

DBTools ORM for JPA

License

License

GroupId

GroupId

org.dbtools
ArtifactId

ArtifactId

dbtools-jpa
Last Version

Last Version

5.0.1
Release Date

Release Date

Type

Type

jar
Description

Description

DBTools JPA
DBTools ORM for JPA
Project URL

Project URL

https://github.com/jeffdcamp/dbtools-jpa
Source Code Management

Source Code Management

https://github.com/jeffdcamp/dbtools-jpa

Download dbtools-jpa

How to add to project

<!-- https://jarcasting.com/artifacts/org.dbtools/dbtools-jpa/ -->
<dependency>
    <groupId>org.dbtools</groupId>
    <artifactId>dbtools-jpa</artifactId>
    <version>5.0.1</version>
</dependency>
// https://jarcasting.com/artifacts/org.dbtools/dbtools-jpa/
implementation 'org.dbtools:dbtools-jpa:5.0.1'
// https://jarcasting.com/artifacts/org.dbtools/dbtools-jpa/
implementation ("org.dbtools:dbtools-jpa:5.0.1")
'org.dbtools:dbtools-jpa:jar:5.0.1'
<dependency org="org.dbtools" name="dbtools-jpa" rev="5.0.1">
  <artifact name="dbtools-jpa" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.dbtools', module='dbtools-jpa', version='5.0.1')
)
libraryDependencies += "org.dbtools" % "dbtools-jpa" % "5.0.1"
[org.dbtools/dbtools-jpa "5.0.1"]

Dependencies

compile (6)

Group / Artifact Type Version
com.google.code.findbugs : jsr305 jar 3.0.1
org.hibernate.javax.persistence : hibernate-jpa-2.1-api jar 1.0.0.Final
javax.inject : javax.inject jar 1
org.dbtools : dbtools-query jar 2.5.6
joda-time : joda-time Optional jar 2.9.2
junit : junit jar 4.12

test (2)

Group / Artifact Type Version
org.hibernate : hibernate-entitymanager jar 5.1.0.Final
org.apache.derby : derby jar 10.12.1.1

Project Modules

There are no modules declared in this project.

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.

Versions

Version
5.0.1
3.5.1
3.1.0