Giter VIP home page Giter VIP logo

jdbc-orm-framework's Introduction

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!

jdbc-orm-framework's People

Contributors

sionzee avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

cenbow yzmaodeng

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.