Software:Fjorm

From HandWiki
fjorm
Developer(s)Mladen Adamovic
Stable release
1.0.2 / March 16, 2015 (2015-03-16)
Written inJava
Operating systemCross-platform
PlatformJava
TypeObject-relational mapping
LicenseApache Software License
Websitecode.google.com/p/fjorm/

Fjorm is a free open source software library for fast but simple mapping of POJOs to the database. Fjorm got its name as acronym from Fast Java Object Relationship software. It targets to have less than 10% performance overhead over plain JDBC. Fjorm is designed and developed by former Google software engineer.

Paradigm

Some performance testing of most popular Java ORM, Hibernate, are showing large performance drawbacks for using Hibernate.[1]

Fjorm aims to be fast and lightweight approach. It does not support mapping of m:n relationships neither n:1 relationships due to possible performance issues. It doesn't create new query language, it uses SQL which whom most database developers are fluent and which is quite powerful and standardized. It assumes database fields will be represented as public fields in Java code and that developers shall use same name for the field as in the database. It uses Java reflection over those fields to enable to perform CRUD operations.

Example

Let us assume that we have the following MySQL database for books:

CREATE TABLE `language` (
  code            CHAR(3)       NOT NULL PRIMARY KEY,
  description     VARCHAR(50)
) DEFAULT CHARACTER SET utf8;

CREATE TABLE `author` (
  id              INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  first_name      VARCHAR(50),
  last_name       VARCHAR(50)  NOT NULL,
  date_of_birth   DATE,
  year_of_birth   INT,
  distinguished  INT DEFAULT 0
) DEFAULT CHARACTER SET utf8;

CREATE TABLE `book` (
  id              INT AUTO_INCREMENT PRIMARY KEY,
  author_id       INT,
  title           VARCHAR(400),
  published_in    int,
  language_code   CHAR(2) NOT NULL,
  
  FOREIGN KEY (author_id)   REFERENCES author(id),
  FOREIGN KEY (language_code) REFERENCES language(code)
) DEFAULT CHARACTER SET utf8;

These tables are mapped in Fjorm as follows, table 'language':

@TableName(table = "language")
public class Language {
   
  @Id
  public String code;
  
  public String description;

  public Language(String code, String description) {
    this.code = code;
    this.description = description;
  }
}

Mapping of table 'author':

@TableName(table = "author")
public class Author {
  
  @Id
  @AutoGenerated
  public int id;
  
  public String first_name;
  public String last_name;
  public java.util.*.Date date_of_birth;
  public int year_of_birth;
  public int distinguished;
}

Mapping of table 'book':

@TableName(table = "book")
public class Book {

  @Id
  @AutoGenerated
  public int id;
  
  public int author_id;
  public String title;
  public int published_in;
  public String language_code;
}

Example of creating new languages in the database:

Dao<Language> languageDao = Dao.getDao(Language.class, BookConnectionProperties.getInstance());
    
    //create languages
    Language cs_lang = new Language("cs", "Czech");
    Language en_lang = new Language("en", "English");
    Language fr_lang = new Language("fr", "french");
    Language lug_lang = new Language("lug", "luganda");
    Language swa_lang = new Language("swa", "swahili");
    languageDao.create(en_lang);
    languageDao.create(cs_lang);
    languageDao.create(fr_lang);
    languageDao.create(lug_lang);
    languageDao.create(swa_lang);

Example of updating author, setting that author as distinguished:

Author author1 = authorDao.readFirst("");
    author1.distingiuished = 1;
    authorDao.update(author1);

Get authors which have book published in 2009:

List<Author> authors2009 = authorDao.read("inner join book on author.id = book.author_id where book.published_in = ?", 2009);

Get all books from authors born after 1980:

List<Book> booksFromYoungAuthors = bookDao.read("inner join author on book.author_id = author.id where author.year_of_birth >= ?",  1980);

Example of performing delete commands from interface to our database

delete from book where author_id = null

Examaple of selecting from a database

select first_name, last_name form author where year_of_birth = 2013;

See also

References

External links