/JDBC-ORM-Framework

This is MySQL framework for Java which add entity way to manage your project.

Primary LanguageJava

JDBC ORM Framework 2.2

Framework for manage entities. This framework is created to be user-friendly and easy to use.

Unit tests are ready! (test package)


Requirements

  • Java 8
  • Brain and time
  • A little SQL knowledge

Usage

Copy content of package 'main/java' into your project.

 Init a database

boolean enableEvents = true;
MySQL databaseInstance = new MySQL(host, database, user, password, port);
EntityManager entityManager = new EntityManager(databaseInstance, enableEvents);

Note:

  • For a custom connection use custom class which is implementing IConnection
  • EntityManager is needed everywhere!

 Create an entity

@Table
public class User {
	@Column private int id;
	@Column private Rank rank;
	@Column private String name;
	public int getId() {return this.id;}
	public void setName(String name) {this.name = name;}
	public void setRank(Rank rank){this.rank = rank;}
	public String getName() {return this.name;}
	public Rank getRank() {return this.rank;}
}
@Table
public class Rank {
	@Column private int id;
	@Column private String name;
	public int getId() {return this.id;}
	public String getName() {return this.name;}
	public void setName(String name) {this.name = name;}
}

Note:

  • Entities have to own @Table annotation
  • Every field which have to be in database must have a @Column annotation. (including id)
  • Declare field 'entityManager' of type 'EntityManager' to get instance of EntityManager in your Entity class. (Not recommended)
  • Declare method 'getTable()' of type 'String' to set custom table name in runtime environment.

 Create tables

TableCreator tableCreator = entityManager.getTableCreator();
boolean recreateTables = false;

//Automatically
tableCreator.createAllTablesInJar(new File("PathToThisJar"), recreateTables);

//Manually
tableCreator.createTable(User.class, recreateTables);
tableCreator.createTable(Rank.class, recreateTables);

Note:

  • Path to this Jar you can receive through: JarUtils.getJarFile(EntityManager.class);

 Insert into a table

Rank rank = new Rank();
rank.setName("Administrator");

User user = new User();
user.setName("George");
user.setRank(rank);

//First persist rank and then user. Why? Firstly needs to be created all inner entities. Then theirs parents.
entityManager.persist(rank).persist(user).flush();

Note:

  • On persist is automatically assigned ID to entity.

 Select from a database

User user = entityManager.getRepository(User.class).find().where("id = {0}", 1).one();
if(user != null) {
    print("User " + user.getName() + " have a rank " + user.getRank().getName());
    //User George have a rank Administrator
} else {
    print("User ID: " + 1 + " not found!"); 
    //User ID: 1 not found!
}

 Modify value

User user = entityManager.getRepository(User.class).find().where("id = {0}", 1).one();
user.setName("NewName");
entityManager.persist(user).flush();

 Remove an entity

User user = entityManager.getRepository(User.class).find().where("id = {0}", 1).one();
if(user != null) // Exists
	entityManager.delete(user);
	
//second way
entityManager.getRepository(User.class).delete().where("id = {0}", 1).one();

entityManager.flush();

Note:

  • When calling a delete, don't forget for a flush!

 Custom repositories

class UserRepository extends Repository<User> {
    public UserRepository(Class<User> clazz, EntityManager entityManager) {
        super(clazz, entityManager);
    }
    
    public User getUserByName(String username) {
        return this.find().where("username = {0}", username).one();
    }
}

//Register repository in EntityManager
UserRepository userRepository = em.registerRepository(UserRepository.class, User.class);

//And now usage :-)
User user = userRepository.getUserByName("George");
if(user == null)
    //CODE  

 Register a listener

//<AnyEvent>.getHandlerList().addListener(event -> {});
EntityUpdateEvent.getHandlerList().addListener(event -> {
    if(event.getUpdateTo() instanceof User && ((User) event.getUpdateTo()).getAge() == 22)
        event.setCancelled(true);
    //Update will be cancelled! :)
});

Note:

  • Events are only called when they are enabled in constructor

Supported Types

  • enum
  • boolean
  • String
  • int
  • long
  • byte
  • short
  • float
  • double
  • Date (sql package)
  • Timestamp (sql package)
  • Time (sql package)
  • char
  • entity

And arrays of all supported types!