JDBC
JPA
Spring Data
The JDBC API makes 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)
Connection conn = null;
Statement stmt = null;
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/EMP",
"username", "password");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id, first FROM Employees");
while (rs.next()) {
System.out.println("ID: " + rs.getInt("id")
+ ", First: " + rs.getString("first"));
}
rs.close();
stmt.close();
conn.close();
JDBC (2)
JPA(1)
Includes:
• the API itself, defined in the
javax.persistence package
• the Java Persistence Query
Language (JPQL)
• object/relational metadata
DAO -> Persistance
JPA(2)
@Entity
@Table(name = "orders")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private long id;
@Column(name = "date")
private Date date;
@ManyToOne
@JoinColumn(name = "product_id")
private Product product;
}
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
CrudRepository PagingAndSortingRepository
JPARepository
Spring Data
JPA
CassandraRepository
Spring Data
Cassandra
MongoRepository
Spring Data
MongoDB
Spring Data
...
JPA
JDBC
DataStax Java Driver Mongo Java Driver
RDBMS Cassandra MongoDB
Spring Data(1)
Spring Data(2)
public interface CrudRepository<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.
}
Spring Data(3)
public interface PersonRepository 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");
}
}
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
}
Spring Data(5)
Spring Data JPA
@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)

JDBC - JPA - Spring Data

  • 1.
  • 2.
    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)
  • 3.
    Connection conn =null; Statement stmt = null; Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost/EMP", "username", "password"); stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT id, first FROM Employees"); while (rs.next()) { System.out.println("ID: " + rs.getInt("id") + ", First: " + rs.getString("first")); } rs.close(); stmt.close(); conn.close(); JDBC (2)
  • 4.
    JPA(1) Includes: • the APIitself, defined in the javax.persistence package • the Java Persistence Query Language (JPQL) • object/relational metadata DAO -> Persistance
  • 5.
    JPA(2) @Entity @Table(name = "orders") publicclass Order { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id") private long id; @Column(name = "date") private Date date; @ManyToOne @JoinColumn(name = "product_id") private Product product; }
  • 6.
    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();
  • 7.
    Spring Data CrudRepository PagingAndSortingRepository JPARepository SpringData JPA CassandraRepository Spring Data Cassandra MongoRepository Spring Data MongoDB Spring Data ... JPA JDBC DataStax Java Driver Mongo Java Driver RDBMS Cassandra MongoDB Spring Data(1)
  • 8.
    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)