The document discusses JDBC, JPA, and Spring Data frameworks. JDBC allows establishing database connections and sending SQL statements. JPA provides object-relational mapping and includes annotations to map entities to tables. Spring Data provides repositories to access data based on the underlying data access technology like JPA or MongoDB. It supports common CRUD operations and custom queries.
The JDBC APImakes it possible to
do three things:
• Establish a connection with a
database or access any tabular
data source
• Send SQL statements
• Process the results
JDBC (1)
JPA(1)
Includes:
• the APIitself, defined in the
javax.persistence package
• the Java Persistence Query
Language (JPQL)
• object/relational metadata
DAO -> Persistance
JPA(3)
Product product =new Product();
Order order = new Order();
order.setProduct(product);
entityManager.persist(product);
entityManager.persist(order);
Product dbProduct = entityManager.find(Product.class, product.getId());
String name = "InitialName";
String query = "SELECT p FROM Product p WHERE p.name LIKE
:productName";
Product product = entityManager.createQuery(query, Product.class)
.setParameter("productName", name)
.getSingleResult();
Spring Data(2)
public interfaceCrudRepository<T, ID extends Serializable>
extends Repository<T, ID> {
<S extends T> S save(S entity);
T findOne(ID primaryKey);
Iterable<T> findAll();
Long count();
void delete(T entity);
boolean exists(ID primaryKey);
// … more functionality omitted.
}
9.
Spring Data(3)
public interfacePersonRepository extends Repository<User, Long> { … }
List<Person> findByLastname(String lastname);
public class SomeClient {
@Autowired
private PersonRepository repository;
public void doSomething() {
List<Person> persons = repository.findByLastname("Matthews");
}
}
10.
Spring Data(4)
interface UserRepositoryCustom{
public void someCustomMethod(User user);
}
class UserRepositoryImpl implements UserRepositoryCustom {
public void someCustomMethod(User user) {
// Your custom implementation
}
}
public interface UserRepository extends CrudRepository<User, Long>,
UserRepositoryCustom {
// Declare query methods here
}
11.
Spring Data(5)
Spring DataJPA
@Query("select u from User u where u.emailAddress = ?1")
User findByEmailAddress(String emailAddress);
@Query("select u from User u")
Stream<User> findAllByCustomQueryAndStream();
Spring Data REST
Expose DB through REST (JSON in HAL notation)