Lightweight ORM for Java / SQL developers.
First, you should download the latest version from here.
Then, you can add the jar file to you project for example if you're using Netbeans, you should go to the Project Tree, Right click on Libraries, and choose the 'Add Jar/Folder' option, then select the path to the downloaded jar file.
Before start using the ORM you should provide credentials:
public class TestingDopsieORM {
public static void main(String[] args) {
System.setProperty("host", "localhost");
System.setProperty("port", "3306");
System.setProperty("database", "esprit");
System.setProperty("user", "root");
System.setProperty("password", "root");
// You can use Models here.
}
}
Create a Model inside Models
by extending the Model class from Models package:
package Models;
import Core.ORM.*;
/**
* User Model
*/
public class User extends Model {
}
After you will be able to retrieve data as Model from the database:
To retrieve a User by its id :
User user = Model.find(User.class, 1);
To retrieve all users:
ArrayList<User> allUsersList = Model.fetch(User.class).all().execute();
You are able to apply filters on the data using the where
method:
ArrayList<User> allUsersList = Model.fetch(User.class)
.all()
.where("last_name", "=", "john")
.execute();
You are also able to order the data using the orderBy
method:
ArrayList<User> allUsersList = Model.fetch(User.class)
.all()
.where("last_name", "=", "john")
.orderBy("last_name", "DESC")
.execute();
You can get an attribute using getAttr
method:
String lastName = user.getAttr("last_name");
You can set an attribute in an existing Model object using:
user.setAttr("last_name", "john");
After creating new model object or updating a retrieved object you can push updates to the database using save
:
user.save();
If you want to delete a Model Object you have to trigger the delete
method:
user.delete();
public class User extends Model {
public ArrayList<Post> posts() throws ModelException{
return this.hasMany(Post.class);
}
}
public class User extends Model {
public Address address() throws ModelException{
return this.hasOne(Address.class);
}
}
public class Post extends Model {
public User author() throws ModelException{
return this.hasOne(User.class);
}
}
public class User extends Model {
public ArrayList<Role> roles() throws ModelException{
return this.belongsToMany(Role.class);
}
}
public class Order extends Model {
public ArrayList<Product> products() throws ModelException{
return this.manyToMany(Product.class, ProductOrder.class);
}
}
public class User extends Model {
@Override
public String getTableName() {
return "person";
}
}
public class User extends Model {
@Override
public String getPrimaryKeyName() {
return "user_id";
}
}
You are able to execute SQL queries by calling sqlQuery
and it will return a ResultSet
:
Model.sqlQuery("SELECT * FROM users");
Or make it into a collection of Model objects by:
Model.sqlQuery("SELECT * FROM users", User.class);
For Updates you should provide the SQL statment along to arguments:
ArrayList args = new ArrayList(Arrays.asList("Mike", 1));
sqlUpdate("UPDATE users SET last_name = ? WHERE id = ?", args)