Quick and easy use of JPA with a DAO so instant it will shock you.
A DAO (Data Access Object) is almost always needed in a java project. Extend the eisiges.sudden_dao.GenericPersistenceManager
class and that's it.
- Query a database for a list of all objects whose values match an object's non-null object member variables
Rock keyRock = new Rock();
keyRock.setColor(Color.BLUE);
List<Rock> blueRocks = rockManager.getMatching(keyRock);
- Retrieve an object's primary key
rockManager.getId(myObject);
- Add sudden-dao to your project
Maven:
<dependency>
<groupId>es.eisig</groupId>
<artifactId>sudden-dao</artifactId>
<version>1.0.0</version>
</dependency>
Gradle:
repositories {
mavenCentral()
}
dependencies {
implementation 'es.eisig:sudden-dao:1.0.0'
}
-
Create a class extending
eisiges.sudden_dao.GenericPersistenceManager
-
Create an empty constructor which calls
super(Class<T> entityClass)
-
(optional) add additional methods
@Local // javax.ejb.Local
public class UserBean extends GenericPersistenceManager<MyUser, Long> { // MyUser: entity being managed, Long: type of primary key
public UserBean(){
super(MyUser.class);
}
public TypedQuery<MyUser> getByBirthdateAsc() {
return this.find().sortBy(MyUser_.birthdate).ascending().build();
}
}
The following functions are exposed by GenericPersistenceManager:
boolean containsKey(K)
T persist(T)
K getId(T)
T save(T)
T get(K)
void remove(T)
void saveAll(Collection<T>)
void removeAll(Collection<T>)
List<T> getAll()
boolean isTableEmpty()
List<T> getMatching(T)