Spring Data query methods usually return one or multiple instances of the aggregate root managed by the repository. However, it might sometimes be desirable to create projections based on certain attributes of those types. Spring Data allows modeling dedicated return types, to more selectively retrieve partial views of the managed aggregates.
Projection is just a way to retrieve certain fields from the database without retrieving all fields. This improves performance as it does not retrieve all fields.
There are 3 different types of projection: Scalar, DTO, Entity projection
Scalar Projection
Scalar projection allows you to select columns you need.
@Repository
public interface BookRepository extends JpaRepository<User, Long> {
@Query("SELECT u.name, u.email FROM Book b")
List<Object[]> getNameAndEmail();
}
DTO Projection
DTO projection uses a constructor which Hibernate uses to populate data from the database.
@Repository
public interface UserRepository extends CrudRepository<User, Long> {
@Query("SELECT new com.kaveinga.user.dto.UserDetailDTO(u.firstName, u.lastName) FROM User user WHERE user.firstName = :firstName")
List<UserDetailDTO> findByFirstName(String firstName);
}
JPA DTO
As long as the DTO class has only one constructor and its parameter names match your entity class’s attribute names, Spring generates a query with the required constructor expression.
public interface UserRepository extends JpaRepository<User, Long> {
UserDTO findByEmail(String email);
}
public class UserDTO {
private Long id;
private String uid;
private String name;
private String email;
private int age;
private Address address;
public UserDTO(Long id, String uid, String name, String email, int age, Address address) {
super();
this.id = id;
this.uid = uid;
this.name = name;
this.email = email;
this.age = age;
this.address = address;
}
// getters and setters
}
JPA DTO as interface
Here is another way you can retrieve data from the database. Instead of having a DTO class you can use an interface. Your interface only defines getter methods for basic attributes.
public interface UserRepository extends JpaRepository<User, Long> {
UserView findByUid(String uuid);
}
public interface UserView {
Long getId();
String getName();
String getEmail();
int getAge();
String getUid();
// nested object
AddressView getAddress();
}
Snapshot is a backup taken from a running Elasticsearch cluster. We can take a snapshot of individual indices or of the entire cluster. Snapshots are incremental, which means each snapshot of an index only stores data that is not part of an earlier snapshot.
I have found this npm library that does what I want here.
How to update state of an object
We have a user object
setUser({
...user,
name: "Peter"
});
How to update state of list/array of objects
We have a shopping cart which contains a list of menu items
const [shopCart, setShopCart] = useState([{
name: "",
price: 0,
uuid: ""
}]);
add item to shopping cart
const addMenuItemToCart = (menuItem: any) => {
console.log("addMenuItemToCart, ", menuItem)
setShopCart(shopCart => {
shopCart.push(menuItem);
const newState = shopCart.map(obj => {
// 👇️ otherwise return object as is
return obj;
});
return newState;
});
}
A DOCTYPE is an instruction to the web browser about the version of HTML in which the web page is written. A DOCTYPE declaration appears at the top of a web page before all other elements. According to the HTML specification or standards, every HTML document requires a valid document type declaration to insure that your web pages are displayed the way they are intended to be displayed.
The DOCTYPE for HTML5
<!DOCTYPE html>
Doctypes for earlier versions of HTML were longer because the HTML language was SGML-based and therefore required a reference to a DTD, but they are obsolete now.
With HTML5 this is no longer the case and the doctype declaration is only needed to enable the standard mode for documents written using the HTML syntax.
HTML4
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
You can use the following markup as a template to create a new HTML5 document.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Hello World</title>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Create auth server and resource server
Create auth client that consumes resource from a third party like Google or Github