Posts

Showing posts from April, 2023

Spring Framework and JPA

To use JPA in a Spring project, we need to set up the EntityManager. This is the main part of the configuration and we can do it via a Spring factory bean. This can be either the simpler LocalEntityManagerFactoryBean or the more flexible LocalContainerEntityManagerFactoryBean. import java.util.Properties; import javax.sql.DataSource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor; import org.springframework.jdbc.datasource.DriverManagerDataSource; import org.springframework.orm.jpa.JpaTransactionManager; import org.springframework.orm.jpa.JpaVendorAdapter; import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean; import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.annotation.EnableTrans...

Spring Boot JPA Configuration

Spring Boot configures Hibernate as the default JPA provider, so it’s no longer necessary to define the entityManagerFactory bean unless we want to customize it. Spring Boot can also auto-configure the dataSource bean, depending on the database we’re using. In the case of an in-memory database of type H2, HSQLDB and Apache Derby, Boot automatically configures the DataSource if the corresponding database dependency is present on the classpath. For example, if we want to use an in-memory H2 database in a Spring Boot JPA application, we only need to add the h2 dependency to the pom.xml file: <dependency>      <groupId>com.h2database</groupId>      <artifactId>h2</artifactId>      <version>1.4.197</version> </dependency> This way, we don’t need to define the dataSource bean, but we can do so if we want to customize it. If we want to use JPA with MySQL database, then we need the mysql-connector-java ...

Hibernate Many to Many Relationship

  @Entity @Table(name = "Employee") public class Employee { // ... @ManyToMany(cascade = { CascadeType.ALL }) @JoinTable( name = "Employee_Project", joinColumns = { @JoinColumn(name = "employee_id") }, inverseJoinColumns = { @JoinColumn(name = "project_id") } ) Set<Project> projects = new HashSet <>(); // standard constructor/getters/setters } @JoinTable specifies that an intermediate table is used to map the ManyToMany relationship it specifies the Join column as the id of the Employee class from which the Many side of the relationship is being created. The inverseJoinColumns attribute specifies the Join column of the target of the M2M relationship. @Entity @Table(name = "Project") public class Project { // ... @ManyToMany(mappedBy = "projects") private Set<Employee> employees = new HashSet <>(); // ...

Data Binding in Angular

  3. What is data binding? Which type of data binding does Angular deploy? Data binding  is a phenomenon that allows any internet user to work with dynamic data. It uses dynamic HTML and makes use of simple scripting. We use data binding in web pages that contain interactive components such as forms, calculators, tutorials, and games. When a page has lots of data, incremental display of data becomes more important from performance point of view. Angular uses the two-way binding. Any changes made to the user interface are reflected in the corresponding model state. Conversely, any changes in the model state are reflected in the UI state. This allows the framework to connect the DOM to the Model data via the controller. However, this approach affects performance since every change in the DOM has to be tracked Example <app-sizer [(size)]="fontSizePx"></app-sizer> For two-way data binding to work, the  @ Output ()  property must use the pattern, < inp...

Deprecations and Deletions in Java 11

This lists talks about all the “deprecated” features or features that are removed from the JDK. Eliminate the Java EE and CORBA Modules JDK Enhancement Proposal 320 gets rid of the below mentioned modules from the JDK: java.xml.ws (JAX-WS) java.xml.bind (JAXB) java.activation (JAF) java.xml.ws.annotation (Common Annotations) java.corba (CORBA) java.transaction (JTA) java.se.ee (aggregator module for the 6 formerly discussed modules) jdk.xml.ws (tools for JAX-WS) jdk.xml.bind (tools for JAXB) Initially, the listed technologies were designed for the Java EE platform and were incorporated into the standard edition “Java SE” when Java 6 was released. They were marked “deprecated” in Java 9 and finally got rid of with Java 11. After upgrading to Java 11, should you miss these libraries, you can bring them back to your project, example through Maven dependencies. Discontinue the Nashorn JavaScript Engine In JDK 8, the JavaScript engine, “Rhino” was introduced and with JEP 335 in Java 11, it ...

Bridge Pattern

  Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern comes under structural pattern as this pattern decouples implementation class and abstract class by providing a bridge structure between them. This pattern involves an interface which acts as a bridge which makes the functionality of concrete classes independent from interface implementer classes. Both types of classes can be altered structurally without affecting each other. public interface Vehicle API { public void createVehicle ( int length , int width , int height ); } public class R edCar implements Vehicle API { @Override public void createVehicle(int length , int width , int height ) { System . out . println ( "Drawing Car[ color: red, radius: " + radius + ", x: " + x + ", " + y + "]" ); } } public abstract class Vehicle { protecte...