Object–Relational Mapping (ORM) Library

The VMJ ORM Library adopts Hibernate ORM as its foundation, offering an efficient and flexible ORM approach. This library enables the implementation of the decorator pattern at the database level, providing developers with the ability to enhance functionality without modifying the underlying structure. It includes built-in CRUD functions, simplifying database operations without the need for writing SQL queries manually. However, for more complex use cases, developers can directly execute intricate SQL queries through the library, ensuring full flexibility to meet the application's specific data handling needs.

Available Functions

This library provides a set of functions that simplify database operations. Below are the key functions available in the VMJ ORM Library:

1. saveObject(object)

The saveObject function is responsible for persisting an object to the database. It takes a single parameter, which is the object to be saved.

Example:

import id.ac.ui.cs.prices.winvmj.hibernate.RepositoryUtil;

public abstract class PaymentServiceComponent implements PaymentService {
    protected RepositoryUtil<Payment> Repository;
    // Complete the abstract component class
}

public class PaymentServiceImpl extends PaymentServiceComponent {
    public Payment savePayment(Map<String, Object> requestBody) {
        Payment payment = PaymentFactory.createPayment(
			// Complete the factory pattern
		);
        
        Repository.saveObject(payment);

        return payment;
    }

    // Complete the concrete component class
}

2. getObject(id)

The getObject function is responsible for retrieving an object from the database using its unique identifier (ID). It takes a single parameter, which is the ID of the object to be retrieved (it can be of type integer or UUID), and returns the corresponding object.

Example:

import java.util.UUID;
import id.ac.ui.cs.prices.winvmj.hibernate.RepositoryUtil;

public abstract class PaymentServiceComponent implements PaymentService {
    protected RepositoryUtil<Payment> Repository;
    // Complete the abstract component class
}

public class PaymentServiceImpl extends PaymentServiceComponent {
    public Payment getPayment(UUID paymentId) {
        return Repository.getObject(paymentId);
    }

    // Complete the concrete component class
}

3. getAllObject(tableName)

The getAllObject function is responsible for retrieving all objects from a specified table in the database. It takes a single parameter, which is the name of the table, and returns a list of all corresponding records.

Example:

import java.util.List;
import id.ac.ui.cs.prices.winvmj.hibernate.RepositoryUtil;

public abstract class PaymentServiceComponent implements PaymentService {
    protected RepositoryUtil<Payment> Repository;
    // Complete the abstract component class
}

public class PaymentServiceImpl extends PaymentServiceComponent {
    public List<Payment> getAllPayment() {
        return Repository.getAllObject("payment_comp");
    }

    // Complete the concrete component class
}

4. getAllObject(tableName, objectName)

The getAllObject function is responsible for retrieving all records from a specified table where the value of the objectname column matches the given objectName parameter. It takes two parameters: tableName, which specifies the table to query, and objectName, which specifies the value to filter the records by.

Example:

import java.util.List;
import id.ac.ui.cs.prices.winvmj.hibernate.RepositoryUtil;

public abstract class PaymentServiceComponent implements PaymentService {
    protected RepositoryUtil<Payment> Repository;
    // Complete the abstract component class
}

public class PaymentServiceImpl extends PaymentServiceComponent {
    public List<Payment> getAllPaymentByObjectName() {
        return Repository.getAllObject("payment_comp", "credit_card");
    }

    // Complete the concrete component class
}

5. getListObject(tableName, columnName, columnValue)

The getListObject function is responsible for retrieving a list of records from a specified table that match a given value in a specified column. It takes three parameters: tableName (the name of the table to query), columnName (the name of the column to filter by), and columnValue (the value to match in the specified column). It returns a list of objects that satisfy the filtering condition.

Example:

import java.util.List;
import id.ac.ui.cs.prices.winvmj.hibernate.RepositoryUtil;

public abstract class PaymentServiceComponent implements PaymentService {
    protected RepositoryUtil<Payment> Repository;
    // Complete the abstract component class
}

public class PaymentServiceImpl extends PaymentServiceComponent {
    public List<Payment> getListObject() {
        return Repository.getListObject("payment_comp", "amount", "10000");
    }

    // Complete the concrete component class
}

6. updateObject(object)

The updateObject function is responsible for updating an existing object in the database. It takes a single parameter, which is the object to be updated, and applies changes to the existing record.

Example:

import java.util.UUID;
import id.ac.ui.cs.prices.winvmj.hibernate.RepositoryUtil;

public abstract class PaymentServiceComponent implements PaymentService {
    protected RepositoryUtil<Payment> Repository;
    // Complete the abstract component class
}

public class PaymentServiceImpl extends PaymentServiceComponent {
    public Payment updatePayment(Map<String, Object> requestBody) {
        String paymentIdString = (String) requestBody.get("payment_id");
        UUID paymentId = UUID.fromString(paymentIdString);
        Payment payment = Repository.getObject(paymentId);

        // Update the payment object based on the request body
        
        Repository.updateObject(payment);

        return payment;
    }

    // Complete the concrete component class
}

7. deleteObject(id)

The deleteObject function is responsible for removing a record from the database based on its unique identifier (ID). It takes a single parameter, which is the ID of the record to be deleted (it can be of type integer or UUID).

Example:

import java.util.UUID;
import id.ac.ui.cs.prices.winvmj.hibernate.RepositoryUtil;

public abstract class PaymentServiceComponent implements PaymentService {
    protected RepositoryUtil<Payment> Repository;
    // Complete the abstract component class
}

public class PaymentServiceImpl extends PaymentServiceComponent {
    public void deletePayment(UUID paymentId) {
        Repository.deleteObject(paymentId);
    }

    // Complete the concrete component class
}

8. executeQuery(sqlQuery)

The executeQuery function is responsible for executing a raw SQL query provided as a parameter. It takes a single parameter, which is the SQL query to be executed.

Example:

import java.util.List;
import java.util.ArrayList;                            
import id.ac.ui.cs.prices.winvmj.hibernate.RepositoryUtil;

public abstract class PaymentServiceComponent implements PaymentService {
    protected RepositoryUtil<Payment> Repository;
    // Complete the abstract component class
}

public class PaymentServiceImpl extends PaymentServiceComponent {
    public List<List<Object>> runComplexPaymentQuery(String sqlQuery) {
        List<List<Object>> results = new ArrayList<>();
        Repository.executeQuery(session -> {
            results = (List<List<Object>>) session.createNativeQuery(sql).setParameter("paymentId", "d4f50a77-6c3a-4f6b-91a9-6e25b5a2d1cb");
        });

        return results;
    }

    // Complete the concrete component class
}

Changelog

v1.1.3:

  • Handled multiple concrete decorator models and abstract component models.

v1.1.2:

  • Improved implementation of the decorator pattern using the latest approach, particularly in one-to-many or many-to-many relationships and table initialization.

v1.1.1:

  • Added a function to execute custom SQL queries, enhancing flexibility in database interactions.
  • Improved code complexity in handling the decorator pattern using the latest approach.

v1.1.0:

  • Introduced a new approach for handling the decorator pattern, improving the behavior especially when multiple layers of wrapping occur.

v1.0.4:

  • Initial release with core orm functionality.