OHibernate

General

Category
Free
Tag
ORM
License
N/A
Min SDK
15 (Android 4.0.3–4.0.4 Ice Cream Sandwich)
Registered
Nov 26, 2016
Favorites
1
Link
https://github.com/10uroi/OHibernate
See also
DbQuery
Orma
droitatedDB
DroidModel
EasyliteOrm

Additional

Language
Java
Version
1.0.9 (Jan 10, 2017)
Created
Nov 21, 2016
Updated
Mar 12, 2019
Owner
Onur Ciner (10uroi)
Contributor
Onur Ciner (10uroi)
1
Activity
Badge
Generate
Download
Source code

Announcement

OHibernate

1.0.9

SQLite Connection for Android. ORM tool for Android devices. First ORM with Geometric-Spatial data support for Android operating systems.

There is relational table support. OneToOne and OneToMany are fully supported. OHQL support for simple inquiries.

Log

v1.0.8 -> Solved single quotes problem. v1.0.9 -> Folder created automatically.

##Installation

To use the library, first include it your project using Gradle

    allprojects {
        repositories {
            jcenter()
            maven { url "https://jitpack.io" }
        }
    }

and:

    dependencies {
            compile 'com.github.10uroi:OHibernate:1.0.9'
    }

##How to use

###Attached into the "MainActivity" class

 public class MainActivity extends AppCompatActivity {
 
     //I set the database path and the name
     public static final String DATABASE_EXTERNAL = Environment.getExternalStorageDirectory().getPath(); //external
     public static final String DATABASE_SUB = "/folderName/";
     public static final String DATABASE_DB_PATH = DATABASE_EXTERNAL + DATABASE_SUB;
     public static final String DATABASE_DATA_NAME = "databaseName.sqlite";
 
 
 
     @Override
  protected void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  OHibernateConfig.DB_PATH = DATABASE_DB_PATH; // DATABASE PATH
  OHibernateConfig.DB_NAME = DATABASE_DATA_NAME; // DATABASE NAME
  
  }
 }

###We are creating the "ExampleBean" class

    @Entity(TABLE_NAME = "example", TABLE_OPERATION = Entity.TABLE_OPERATION_TYPE.DROP_AND_CREATE)
 public class ExampleBean {

  @Id(PRIMARY_KEY_AUTOINCREMENT = true)
  private Integer id;

  private String name;

  private String surname;

  private int age;

  @Column(NAME = "active") //column customization
  private boolean status;

  @Column(DATETIME = true) //column customization
  private String datetime;
  
  //GETTER - SETTER
 }

###We are creating the "ExampleBeanDAO" class

 public class ExampleBeanDAO {

  //Automatic Transactions
 
  OHibernate<ExampleBean> oHibernate = new OHibernate<>(ExampleBean.class);

  public void insert(ExampleBean exampleBean){
   try {
    oHibernate.insert(exampleBean); // Returns the id of the object
   } catch (Exception e) {
    e.printStackTrace();
    Log.e("Error",e.getMessage());
   }
  }

  public void update(ExampleBean exampleBean){
   try {
    oHibernate.update(exampleBean);
   } catch (Exception e) {
    e.printStackTrace();
    Log.e("Error",e.getMessage());
   }
  }

  public void delete(ExampleBean exampleBean){
   try {
    oHibernate.delete(exampleBean);
   } catch (Exception e) {
    e.printStackTrace();
    Log.e("Error",e.getMessage());
   }
  }

  public ExampleBean select(Integer id){
   try {
    return (ExampleBean) oHibernate.where("id",id).select();
   } catch (Exception e) {
    e.printStackTrace();
    Log.e("Error",e.getMessage());
   }
   return null;
  }

  public ArrayList<ExampleBean> selectAll(){
   try {
    return (ArrayList<ExampleBean>) oHibernate.selectAll();
   } catch (Exception e) {
    e.printStackTrace();
    Log.e("Error",e.getMessage());
   }
   return null;
  }

  public ArrayList<ExampleBean> selectAll(String surname){
   try {
    return (ArrayList<ExampleBean>) oHibernate.where("surname",surname).limit(5).selectAll(); // custom
   } catch (Exception e) {
    e.printStackTrace();
    Log.e("Error",e.getMessage());
   }
   return null;
  }

  public ExampleBean selectCustom(String name,String surname){
   try {
    return (ExampleBean) oHibernate.where("name",name).and().where("surname",surname, LIKE_TYPE.BOTH).select(); // custom
   } catch (Exception e) {
    e.printStackTrace();
    Log.e("Error",e.getMessage());
   }
   return null;
  }
 }

Relational tables

Example OneToMany

User Address
@Entity(TABLE_NAME = "users", TABLE_OPERATION = Entity.TABLE_OPERATION_TYPE.CREATE)
 public class User {
  @Id(PRIMARY_KEY_AUTOINCREMENT = true)
  private Integer id;

  private String firstName;

  private String lastName;

  @OneToMany(JoinColumn = "user_id", Cascade = CascadeType.ALL, Fetch = FetchType.EAGER)
  private ArrayList<Address> addresses;

  ...
  //Getter - Setter
}
   </code>
 </pre>
</td>
<td style="padding:0; margin:0; border:none; width:50%;">
  <pre lang="java"><code class="language-java">@Entity(TABLE_NAME = "addresses",TABLE_OPERATION = Entity.TABLE_OPERATION_TYPE.CREATE)
public class Address {

  @Id(PRIMARY_KEY_AUTOINCREMENT = true)
  private Integer id;

  private String county;

  @Column(NAME="phone_number")
  private Long phoneNumber;

  private Integer user_id;

  ...
  //Getter - Setter
}
  </code></pre>
</td>

  
### DB Tables
users addresses
id firstname lastname
</td>
<td style="padding:0; margin:0; border:none;">
  id
</td>
<td style="padding:0; margin:0; border:none;">
  county
</td>
<td style="padding:0; margin:0; border:none;">
  phone_number
</td>
<td style="padding:0; margin:0; border:none;">
  user_id
</td>
1 Onur Ciner ┬► 1 Ankara 05554443322 1
</td>
<td style="padding:0; margin:0; border:none;">
 
</td>
 <td style="padding:0; margin:0; border:none;">

</td>
<td style="padding:0; margin:0; border:none;">
 └►
</td>
<td style="padding:0; margin:0; border:none;">
  2
</td>
<td style="padding:0; margin:0; border:none;">
  İstanbul
</td>
<td style="padding:0; margin:0; border:none;">
  05554443311
</td>
<td style="padding:0; margin:0; border:none;">
  1
</td>

OHQL (The OHibernate Query Language)

Single Select

User user = (User) new OQuery()
 .addEntity(User.class)  //=>Returns a String if entity is not added
 .Select("*")
 .From("users")    //=> "users"->table name
 .Where("id",2)
 .getSingleResult();   //=> Fetch user with id 2 in the users table

List Select

ArrayList<User> users = new OQuery()
 .addEntity(User.class)  //=>Returns a String if entity is not added
 .Select("*")
 .From("users")    //=> "users"->table name
 .list();     //=> Brings all users in the users table

Insert Query

new OQuery()
 .SetParameter("firstName","Onur")
 .SetParameter("lastName","Ciner")
 .Insert("users");   //=> "users"->table name

Insert Entity Query

Users user = new Users();     //=>The object is created
user.setFirstName("Onur");
user.setLastName("Ciner");
new OQuery().InsertEntity("users",user); //=> "users"->table name

Update Query

new OQuery()
 .SetParameter("firstName", "Selçuk")
 .SetParameter("lastName", "Uzunsoy")
 .Where("id", 15)    //=> User with id 15 will be updated
 .Update("users");     //=> "users"->table name

Update Entity Query

Users user = OQuery().Select...;   //=> Object brought
user.setFirstName("Onur");
user.setLastName("Ciner");
new OQuery().UpdateEntity("users",user); //=> "users"->table name

Delete Query

new OQuery()
 .Where("id",15)  //=> User with id 15 will be deleted
 .Delete("users"); //=> "users"->table name

DeleteAll Query

new OQuery()
 .DeleteAll("users"); //=> All users in the users table will be deleted