Skip to content

shivambmgupta/tm-orm-framework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TMORM-Framework

An java based framework for Object Relational Mapping. TMORM Framework is specifically designed for MySQL Database Management System.

Getting Started

With this framework, go SQL-free. This framework manages all data insertion, updation, retrieval, and deletion.

Prerequisites

The user needs to download the jar file and put it into the working directory or the lib path. That's it! The framework will take care of the test.

Usage and Examples

This framework comes with a GUI tool that eases all your efforts. All you have to do is use that tool, configure the name of and other details regarding the database. For example server address, port number, username and password(if any). The tool analyses the database and even generates the java classes for the tables (if the user wants). Or, the user could manually create his/her java classes.

Annotations

If the user solely depends upon the tools he/she may never need to use or worry about the annotations. But if he/she manually creates the class as per framework standard following annotations will be useful:

Annotation Description
MapTable This annotation is class level and must be used with the class. The value of this annotation is the table_name the user wants to map to the object.
MapColumn The annotation is only to be used with the fields of the class. The value of this annotation is the column name of the table user wants to map with the field.
PrimaryKey Annotation used with the field that is mapped to the primary key of the table.
NotNullable NotNullable is used with the field which is mapped to the column that cannot be nullified.
AutoGenerate This annotation is used with the field mapped to a column which is autogenerated.
ForeignKey Annotation used with the field that is mapped to the foreign key of the table.

TMORMapper

Following TMORM framework methods will be useful for insertion, updation, retrieval and deletion of the data:

Return Type Method Description
TMORMapper getInstance() A static method that gives you the instance of the framework. The Framework uses Singleton design technique.
void begin() Each and every transaction user want to do must start from begin()
void save(Object object) Does relational mapping of the received object and saves the information extracted into the database.
void remove(Object object) Removes the object from the database by looking for the field mapped to the primary key in the object.
void update(Object object) Updates all the details of the given object into the database.
Select select(Class<?> clazz) Lets you data retrival from the database.
void commit() Commits the transaction.

Select

Following are the methods of Select class:

Return Type Method Description
Select where(String value) Takes the value and adds 'WHERE' keyword followed by the value to the so far generated query string.
Select eq(Object value) Takes the value and adds '=' followed by the value to the so far generated query string.
Select le(Object value) Takes the value and adds '<=' followed by the value to the so far generated query string.
Select lt(Object value) Takes the value and adds '<' followed by the value to the so far generated query string.
Select gt(Object value) Takes the value and adds '>' followed by the value to the so far generated query string.
Select ge(Object value) Takes the value and adds '>=' followed by the value to the so far generated query string.
Select ne(Object value) Takes the value and adds '!=' followed by the value to the so far generated query string.
Select orderBy(String value) Takes the value and adds 'ORDER BY' followed by the value to the so far generated query string.
Select ascending() Takes the value and adds 'ASC' to the so far generated query string.
Select descending() Takes the value and adds 'DESC' to the so far generated query string.
Select and(String value) Takes the value and adds 'AND' to the so far generated query string.
Select or(String value) Takes the value and adds 'OR' to the so far generated query string.
List query() Return the java.util.List type of object.

Following snippet might be helpful

Sample class Student.java

/**
 *  The table student in the MySQL has the primary key combination of two fields: roll_number and clazz/
 */
 
/* Sample class representing Student */

package test;

import java.util.*;
import java.math.*;
import com.tm.orm.annotation.*;
import java.text.*;

@MapTable("student")
public class Student {
	
	@MapColumn("roll_number")
	@AutoGenerate
	@PrimaryKey 
	private int rollNumber;

	@MapColumn("name")
	public String name;

	@MapColumn("gender")
	public String gender;

	@MapColumn("class")
	@PrimaryKey
	private String clazz;

	@MapColumn("pancard")
	private String pancard;

	@MapColumn("age")
	public int age;

	@MapColumn("course_id")
	@ForeignKey
	private int courseID;

	@MapColumn("department_id")
	@ForeignKey
	public int departmentID;

	@MapColumn("percentage")
	private BigDecimal percentage;

	@MapColumn("indian")
	private boolean isIndian;

	@MapColumn("dob")
	public Date dateOfBirth;

  //Rest portion just have setter and getters of the above properties
}

Sample class: Sample.java

package test;

import com.tm.orm.handler.*;
import com.tm.orm.exception.ORMException;

public class Sample {
  public static void main(String args[]) 
  try {
      TMORMapper orm = TMORMapper.getInstance();
      Student student = new Student();  //Sample class having fields like name, rollNumber, age, gender, dateOfBrith, adharNumber
      
      /**
       * User needs to set all the fields mapped to a non-nullable column of the table before saving or updating the instance of the student.
       */
       
       student.setClazz("8A");
       student.name = "Shivam Gupta";
       student.gender = "Male";
       student.setPercentage(new BigDecimal(90.0));
       student.setPancard(args[0]);
       student.age = 70;
       student.setCourseID(9);
       student.departmentID = 3;
       student.setIsIndian(true);
       student.setDateOfBirth(new Date(1998, 7, 8));
       
       orm.begin();  //Sets the autocommit false
       orm.save(student); // orm.update(student) | orm.remove(student) | the use of all the these methods are self explanatory.
       orm.commit();
      
  } catch(ORMException exception) {
      exception.printStackTrace();
  }
}

Following snippet might be useful for data retrieval:

package test;

import java.util.List;
import com.tm.orm.handler.;
import com.tm.orm.exception.ORMException;

public class Sample {
  public static void main(String args[]) 
  try {
      TMORMapper orm = TMORMapper.getInstance();
      
       orm.begin();  //Sets auto commit false
       
       List<Student> list = orm.select(Student.class).query();
       List<Student> listOrderByDoB = orm.select(Student.class).orderBy("dateOfBirth").ascending().query();
       List<Student> listOrderByNameDesc = orm.select(Student.class).orderBy("name").descending().query();
       List<Student> listOrderByNameRollNumberDesc = orm.select(Student.class).orderBy("name").ascending().orderBy("rollNumber").descending().query();
       List<Student> listAgeLe21OrderByPancard =  orm.select(Student.class).where("age").le(21).orderBy("pancard").descending().query();
       List<Student> indianStudents = orm.select(Student.class).where("isIndian").eq(true).query();
       List<Student> listWhereRGt100 = orm.select(Student.class).where("rollNumber").gt(100).query();
       List<Student> listWhereRGt100AndM = orm.select(Student.class).where("rollNumber").gt(100).and("gender").eq("Male").orderBy("gender").descending().query();
       List<Student> listWhereRGt100OrM = orm.select(Student.class).where("rollNumber").gt(100).or("gender").eq("Male").orderBy("gender").descending().query();

       orm.commit();
       
     } catch(ORMException exception) {
      exception.printStackTrace();
  }
}

About Tool

This framework is provided with the GUI tool that analyses and generates the classes from the database tables. The tool must be used before the use of the framework to configure the database details.

The class for this tool is com.tm.orm.tool.ORMTool.class present in the jar file.

Author

  • Shivam Gupta, Student, Thinking Machines

Acknowledgments

About

An java based framework easing Object Relational Mapping.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors