Skip to main content

Posts

Docker - Basic Commands

Greetings! Following are frequently used Docker commands. Check the docker version. docker -version Pull image from docker repository. docker pull <image_name:tag> docker pull nginx List local images. docker images Create a container from docker image. docker run docker run -it -d nginx docker run --name my-nginx -it -d nginx List running containers. docker ps docker ps -a Access the running container. docker exec docker exec -it ubuntu bash Stop running container. docker stop Kill running container. docker kill Delete a container. docker rm <container_id> Delete an image from local storage. docker rmi <image_id> Build a docker image from a given Docker file. docker build .

Java 8 - Streams - flatMap()

Greetings! With stream API it is easy to iterate over a collection and do whatever operation we like to do. Let's say we have a number list which we would like to print power of 2 of each. List<Integer> attempt1 = Arrays.asList(1, 2, 3, 4, 5); attempt1.stream().map(x -> x * x).forEach(System.out::println); // 1, 4, 9, 16, 25 What if we have a List of List? let's try that out. List<List<Integer>> attempt2 = Arrays.asList(Arrays.asList(1, 2, 3), Arrays.asList(2, 5)); Stream<List<Integer>> attempt2Stream = attempt2.stream(); attempt2Stream.map(x -> x * x).forEach(System.out::println); // compile error attempt2Stream.map(x -> x.stream().map(y -> y * y)).forEach(System.out::println); // java.util.stream.ReferencePipeline$3@4c873330, java.util.stream.ReferencePipeline$3@119d7047 As you can see Stream.map() doesn't give us the expected result for the type Stream<List<Integer>>. This is because map() operation ...

Java 8 - Streams - reduce()

Greetings! Stream API provides several terminal operations for common tasks like count, min, max, sum and average.  These operations return a single value and do specific task. Stream.collect() operation is also a terminal operations but it return a Collection. reduce() is a more-general purpose operations which can be used to combine the content of stream. These terminal operations are called reduction operations. Stream.reduce() This method mainly has two forms. With initial value. Without initial value. T reduce(T identity, BinaryOperator<T> accumulator); Optional<T> reduce(BinaryOperator<T> accumulator); reduce uses BinaryOperator (which extends BiFunction) functional interface which has the form below. R apply(T t, U u); How Does It Work Let's consider below sum operation. List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); int result = 0; for (Integer number : numbers) { result = result + number; }...

Hibernate - Basic Annotations

Greetings! @Entity - Add in class level. Declares that this class is an entity @Entity public class Employee { // ... } @Table - Add in class level. Define table, schema, catalog. Use name property to add the name of the table unless class name is used as the table name. @Entity @Table(name = "tbl_employee") public class Employee { // ... } @Column - Add column properties. @Column(name = "first_name") private String firstName; @Id - Declares property as the identifier of this class. @Id private Long id; @GeneratedValue - use along with @Id to generate primary key automatically. JPA defines 5 strategies. @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; AUTO - select depending on the underlying database. IDENTITY SEQUENCE TABLE identity copy - identity is copied form another entity. @Version - Adding this will add optimistic locking capability. For an update query Hibernate will automatically ...

Hibernate - Value Types

Greetings! When we design our domain models we can have more important classes and less important classes like Address, String, etc. In other word we have fine-grained object model which means more classes than tables. For an example User can have an Address. We can create separate class for Address fields and add it as a property in User using composition. public class User { private Long id; private String name; private Integer age; private Address address; } Here User is an entity. Name, Age, Address are value types. Entity classes need an identifier. Value type doesn't have an identifier because instances are identified through owning entity. What is an Entity? Has a database identity. Has it's own life cycle. Object reference is persisted in the database. What is a Value Type? Doesn't have own identity. Embedded into owning entity. Represent table columns. How to map? Basic types can be directly mapped using @C...