Welcome to the RCRUD pattern page

(Reflective CRUD. Also known as Yet)  

A Software pattern by Macario Polo, Mario Piattini and Francisco Ruiz, from the University of Castilla-La Mancha, in Spain.

RCRUD is a pattern to provide persistence responsibilities to Java classes. At this moment, RCRUD provides the CRUD methods in a reflective way:

It is easy to implement these methods in any class: if we have a correspondence between the class and a table (for example: class Person and table Person) and correspondence among class fields and table rows, we can implement an empty constructor, a constructor to materialize instances (corresponding to the Read method) and the insert method in this way:

public class Person {
    String mPKName;   // fields with PK correspond to the primary key
    int mAge;
    float mSalary;

    public Person() throws Exception {
        mPKNanme=new String();
        mAge=0;
        mSalary=0.0;
    }

    public Person(String name) throws Exception {
        String SQL="Select * from Person where Name='" + name "'";
  
         /* We use the Database Broker pattern to access the DB, but
            we could use any other manner */

        ResultSet r=Broker.select(SQL); 
        if (r.next()) {
            this.mPKName = r.getString(1);
            this.mAge    = r.getInt(2);
            this.mSalary = r.getFloat(3);
        } else
            throw new Exception(name + " does not exist in the database");
    }

    public void insert() throws Exception {
        String SQL="Insert into Person (Name, Age, Salary) values ('" +
            mPKName + "', " + mAge + ", " + mSalary + ")";
        Broker.insert(SQL);
    }
    ....   // Other persistent and non-persistent methods
}

However, when you have a big number of persistent classes, it is hard to write these methods in all classes, it is easy to have mistakes and systems are hard to maintain. Through the RCRUD pattern, we explode the characteristics of the Reflection API to generate, at runtime, all persistent methods of a class.

In Java, all classes are specializations of the Object class, which has the getClass() method to know the runtime class of the instance, which returns a result of the Class type:

With these data, the reader can imagine that it is easy to generate, for example, the insert() method of any object using Reflection. To do this, we only need to make the class a child of the RCRUD class, that generates all persistence methods in runtime.

Using RCRUD, the full implementation of a persistent class (with no other methods) is as follows:

class Person extends RCRUD {
    String mPKName;  
    int mAge;
    float mSalary;

    Person() {
       super();  // RCRUD has a "superconstructor" to build empty instances of any class.
    }

    Person(String name[]) {
        super(name);  // Other "superconstructor" can materialize instances of a any class.
    }
    // Rest of methods
}

We have succesfully used this pattern in a number of projects, some of them with near 100 persistent classes. One of them (with no so many classes) is "easyTest", a program (in Spanish) to generate automatically exams and to help in its correction, that you can find here .

We have this pattern accepted in the EuroPlop 2001 conference. You can download the latest version of the paper in this link. Also you can find three full examples clicking here, here and here.