Skip to content
The power of DbUtils. The simplicity of JPA.
Java
Find file
Latest commit bc168f9 Dec 27, 2014 @mwanji Updated dependencies
Failed to load latest commit information.
src
.gitignore
README.textile
pom.xml

README.textile

DbUtils-JPA brings JPA’s easy-to-use mapping annotations to DbUtils. It eliminates basic, repetitive SQL, while leaving all the power of DbUtils within easy reach.

Warning

DbUtils-JPA currently relies on my fork of DbUtils to insert records. This will change when this feature is integrated into the released version of DbUtils. Please clone and mvn install that library.

Main classes

  • JpaBeanProcessor uses JPA annotations to convert a java.sql.ResultSet to an @Entity.
  • SqlWriter uses sql-writer to generate SQL.
  • JpaQueryRunner provides a friendly, but limited, interface for common operations: get by id, update, insert and delete.

Example


  JpaQueryRunner queryRunner = new JpaQueryRunner(underlyingQueryRunner); // this instance can be cached and re-used

  MyEntity newEntity = new MyEntity();
  newEntity.setName("Bob");
  newEntity.age = 42;
  queryRunner.save(newEntity); // executes an INSERT
  assert newEntity.getId() == 2L; // the generated primary key has been set
    
  MyEntity savedEntity = queryRunner.query(MyEntity.class, 1L); // executes a SELECT
  savedEntity.setName("Alice");
  savedEntity.age = 39;
  queryRunner.save(savedEntity) // executes an UPDATE
    
  queryRunner.delete(MyEntity.class, savedEntity.getId()); // executes a DELETE

The JpaQueryRunner always requires that you give it a QueryRunner. It can use a default SqlWriter and RowProcessor, but these can be customised by using the appropriate constructor.

Using elements independently

JpaBeanProcessor can be used apart from JpaQueryRunner, in conjunction with a RowProcessor and a ResultSetHandler, just like any other BeanProcessor, to use JPA mappings in cases not handled by JpaQueryRunner.

ResultSetHandler<List<MyEntity>> handler = new BeanListHandler<MyEntity>(MyEntity.class, new BasicRowProcessor(new JpaBeanProcessor()));
List<MyEntity> myEntities = queryRunner.query("SELECT * FROM MyEntity", handler);

Supported annotations and attributes

  • Entity
  • Table(name)
  • Id
  • Column(name, updatable, insertable)
  • Transient (and the transient keyword)

Limitations

The semantics of JPA’s annotations are followed as much as possible. However, as DbUtils-JPA does not enforce constraints or DDL instructions, certain annotations are ignored, such as @Column#nullable.

Like DbUtils itself, DbUtils-JPA does not map joins. OneToMany, ManyToOne and OneToOne are ignored. This may be added in the future.

To validate your entities, use a Bean Validation API implementation such as Hibernate Validator or Apache Bean Validator.

Todo

  • OrderBy
  • Use OneToMany, ManyToOne and OneToOne for joins.
  • Basic(fetchType)
  • Embeddable, Embedded
  • IdClass, EmbeddedId

License

DbUtils-JPA is licensed under the Apache License, Version 2.0.

Something went wrong with that request. Please try again.