Bibernate is a custom simplified Object-Relational Mapping framework. It helps to work with database in object-oriented manner.
- Configuration
- Read data source configuration from yaml file
- Multiple persistence units are supported
- Builds Hikari DataSource pool
- Mapping to provide metadata for ORM
- First level cache for session
- Dirty checking mechanism
- Transaction management
- CRUD operations supported by Entity repository
| Annotation | Target | Description |
|---|---|---|
@Entity |
TYPE |
The annotation used to specify entity |
@Id |
TYPE |
The annotation used to mark primary key field of an entity. Each entity should contain exactly one field, annotated with such annotation |
@Table |
TYPE |
The annotation used to specify a custom table name for an entity |
@Column |
FIELD |
The annotation used to specify a custom column name for a field of an entity |
@ManyToOne |
FIELD |
The annotation is used to map many-to-one relationships with using reference types |
@OneToMany |
FIELD |
The annotation is used to map bidirectional one-to-many relationships with using collection types |
@OneToOne |
FIELD |
The annotation is used to map one-to-one relationships with using reference types |
You should setup such tools:
To create application with usage of the Bibernate, you have to clone Bibernate source code repository Bibernate.
git clone git@github.com:bobocode-blyznytsia/bibernate.git
cd bibernatemvn clean install<dependencies>
...
<dependency>
<groupId>com.bobocode.blyznytsia</groupId>
<artifactId>bibernate</artifactId>
<version>${bibernate.version}</version>
</dependency>
...
</dependencies>The variable bibernate.version is the current version of Bibernate.
mvn clean installTo start the project with Bibernate you need to create configuration for your database first.
- Create
persistence.ymlfile with such structure:
persistenceUnits:
- name: h2
dataSource:
jdbcUrl: jdbc:h2:mem:testdb
userName: sa
password: password
driverClassName: org.h2.Driver- Use the name of persistence unit in
DefaultSessionFactoryBuilder.
SessionFactoryBuilder sessionFactoryBuilder = new DefaultSessionFactoryBuilder("h2");
SessionFactory sessionFactory = sessionFactoryBuilder.createSessionFactory();- You can create the
SessionfromSessionFactoryinstance:
Session session = sessionFactory.openSession();- To use transaction you can do:
Transaction transaction = session.getTransaction();
transaction.begin();
// do some action
transaction.commit();
// for rollback you can use
transaction.rollback();- Session has number of method to work with Entity
-
void persist(Object entity):This method is used to persist an entity object to the database. The entity object passed as a parameter will be inserted into the database as a new record.
-
void remove(Object entity):This method is used to remove an entity object from the database. The entity object passed as a parameter will be deleted from the database.
-
<T> T find(Class<T> entityClass, Object primaryKey):This method is used to find an entity in the database by its primary key. The entity class and primary key value are passed as parameters, and the corresponding entity object is returned from the database.
-
void flush():This method is used to synchronize any changes made to the entities in the session with the database. Any pending changes in the session will be written to the database.
-
void close():This method is used to close the session and release any resources associated with it, such as database connections. After closing the session, no further operations can be performed on it.
-
boolean isOpen():This method is used to check if the session is currently open, i.e., if it has not been closed using the close() method.
-
Transaction getTransaction():This method is used to obtain the current transaction associated with the session.
Currently, mapping is supported only for read operations.
Default fetch type is EAGER.
@Entity
@Table(name = "persons")
public class Person {
@Id
private Long id;
private String firstName;
private String lastName;
private LocalDate birthday;
private LocalDateTime createdAt = LocalDateTime.now();
@OneToOne(mappedBy = "person")
private Address address;
@OneToMany(mappedBy = "person")
private List<Note> notes = new ArrayList<>();
}
@Entity
@Table(name = "notes")
public class Note {
@Id
private Long id;
private String body;
@ManyToOne(joinColumnName = "person_id")
private Person person;
}
@Entity
@Table(name = "addresses")
public class Address {
@Id
private Long id;
private String city;
private String street;
@OneToOne(joinColumnName = "person_id")
private Person person;
}You can check the bibernate-demo project, which shows the features of Bibernate
