Posts

Showing posts from June, 2020

Spring boot : Application Events and Listeners - ApplicationContextInitializedEvent

Spring boot : Application Events and Listeners - ApplicationContextInitializedEvent ApplicationContextInitializedEvent An ApplicationContextInitializedEvent is sent when the ApplicationContext is prepared and ApplicationContextInitializers have been called but before any bean definitions are loaded.

Spring boot : Application Events and Listeners - ApplicationEnvironmentPreparedEvent

Spring boot : Application Events and Listeners - ApplicationEnvironmentPreparedEvent ApplicationEnvironmentPreparedEvent  An ApplicationEnvironmentPreparedEvent is sent when the Environment to be used in the context is known but before the context is created.

Spring boot : Application Events and Listeners - ApplicationStartingEvent

Spring boot : Application Events and Listeners - ApplicationStartingEvent ApplicationStartingEvent  An ApplicationStartingEvent is sent at the start of a run but before any processing, except for the registration of listeners and initializers.

Spring boot Liveness State vs Readiness State

Application Availability When deployed on platforms, applications can provide information about their availability to the platform using infrastructure such as Kubernetes Probes. Spring Boot includes out-of-the box support for the commonly used “liveness” and “readiness” availability states. If you are using Spring Boot’s “actuator” support then these states are exposed as health endpoint groups. In addition, you can also obtain availability states by injecting the ApplicationAvailability interface into your own beans. Liveness State The “Liveness” state of an application tells whether its internal state allows it to work correctly, or recover by itself if it’s currently failing. A broken “Liveness” state means that the application is in a state that it cannot recover from, and the infrastructure should restart the application. In general, the "Liveness" state should not be based on external checks, such as Health checks. If it did, a failing external system (a dat...

What is Springboot Lazy Initialization?

SpringApplication allows an application to be initialized lazily. When lazy initialization is enabled, beans are created as they are needed rather than during application startup. As a result, enabling lazy initialization can reduce the time that it takes your application to start. In a web application, enabling lazy initialization will result in many web-related beans not being initialized until an HTTP request is received. A downside of lazy initialization is that it can delay the discovery of a problem with the application. If a misconfigured bean is initialized lazily, a failure will no longer occur during startup and the problem will only become apparent when the bean is initialized. Care must also be taken to ensure that the JVM has sufficient memory to accommodate all of the application’s beans and not just those that are initialized during startup. For these reasons, lazy initialization is not enabled by default and it is recommended that fine-tuning of the JVM’s heap size i...

Angular 10 : tabs/navigation Example

Image
Angular 10 : tabs/navigation Angular Material tabs organize content into separate views where only one view can be visible at a time. Each tab's label is shown in the tab header and the active tab's label is designated with the animated ink bar. When the list of tab labels exceeds the width of the header, pagination controls appear to let the user scroll left and right across the labels. The active tab may be set using the selectedIndex input or when the user selects one of the tab labels in the header. Events The selectedTabChange output event is emitted when the active tab changes. The focusChange output event is emitted when the user puts focus on any of the tab labels in the header, usually through keyboard navigation. HTML: <mat-tab-group mat-align-tabs="start">   <mat-tab label="First">Content 1</mat-tab>   <mat-tab label="Second">Content 2</mat-tab>   <mat-tab label="Third"...

Angular 10 : autocomplete Example

Angular 10 : autocomplete The autocomplete is a normal text input enhanced by a panel of suggested options. Simple autocomplete Start by creating the autocomplete panel and the options displayed inside it. Each option should be defined by a mat-option tag. Set each option's value property to whatever you'd like the value of the text input to be when that option is selected. <mat-autocomplete #auto="matAutocomplete">   <mat-option *ngFor="let option of options" [value]="option">     {{option}}   </mat-option> </mat-autocomplete> HTML: <form class="example-form">   <mat-form-field class="example-full-width">     <input type="text"            placeholder="Pick one"            aria-label="Number"            matInput            [formControl]="myControl"       ...

Angular 10 : checkbox example

Image
Angular 10 : checkbox example <mat-checkbox> provides the same functionality as a native <input type="checkbox"> enhanced with Material Design styling and animations. HTML: <mat-card>   <mat-card-content>     <h2 class="example-h2">Checkbox configuration</h2>     <section class="example-section">       <mat-checkbox class="example-margin" [(ngModel)]="checked">Checked</mat-checkbox>       <mat-checkbox class="example-margin" [(ngModel)]="indeterminate">Indeterminate</mat-checkbox>     </section>     <section class="example-section">       <label class="example-margin">Align:</label>       <mat-radio-group [(ngModel)]="labelPosition">         <mat-radio-button class="example-margin" value="after">After</mat-radio-button>    ...

Angular 10 : input example

Angular 10 : input example matInput is a directive that allows native <input> and <textarea> elements to work with <mat-form-field>. <input> and <textarea> attributes All of the attributes that can be used with normal <input> and <textarea> elements can be used on elements inside <mat-form-field> as well. This includes Angular directives such as ngModel and formControl. The only limitation is that the type attribute can only be one of the values supported by matInput. Supported <input> types The following input types can be used with matInput: color date datetime-local email month number password search tel text time url week HTML: <form class="example-form">   <mat-form-field class="example-full-width">     <mat-label>Email</mat-label>     <input matInput [formControl]="emailFormControl" [errorStateMatcher]="matcher"       ...

Angular 10 : menu example

Angular 10 : menu example <mat-menu> is a floating panel containing list of options. By itself, the <mat-menu> element does not render anything. The menu is attached to and opened via application of the matMenuTriggerFor directive: <button mat-button [matMenuTriggerFor]="menu">Menu</button> HTML: <button mat-icon-button [matMenuTriggerFor]="menu" aria-label="Example icon-button with a menu">   <mat-icon>more_vert</mat-icon> </button> <mat-menu #menu="matMenu">   <button mat-menu-item>     <mat-icon>dialpad</mat-icon>     <span>Redial</span>   </button>   <button mat-menu-item disabled>     <mat-icon>voicemail</mat-icon>     <span>Check voice mail</span>   </button>   <button mat-menu-item>     <mat-icon>notifications_off</mat-icon>   ...

Angular 10 : radio-button Example

Angular 10 : radio-button Example <mat-radio-button> provides the same functionality as a native <input type="radio"> enhanced with Material Design styling and animations. All radio-buttons with the same name comprise a set from which only one may be selected at a time. HTML: <label id="example-radio-group-label">Pick your favorite season</label> <mat-radio-group   aria-labelledby="example-radio-group-label"   class="example-radio-group"   [(ngModel)]="favoriteSeason">   <mat-radio-button class="example-radio-button" *ngFor="let season of seasons" [value]="season">     {{season}}   </mat-radio-button> </mat-radio-group> <div>Your favorite season is: {{favoriteSeason}}</div> JavaScript/TS: <label id="example-radio-group-label">Pick your favorite season</label> <mat-radio-group   aria-labelledby=...

Angular 10 : datepicker Example

Angular 10 : datepicker The datepicker allows users to enter a date either through text input, or by choosing a date from the calendar. It is made up of several components and directives that work together. Example: <mat-form-field appearance="fill">   <mat-label>Choose a date</mat-label>   <input matInput [matDatepicker]="picker">   <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>   <mat-datepicker #picker></mat-datepicker> </mat-form-field>

Angular 10 : Toggle Button Example

Angular 10 : toggle button <mat-button-toggle> are on/off toggles with the appearance of a button. These toggles can be configured to behave as either radio-buttons or checkboxes. While they can be standalone, they are typically part of a mat-button-toggle-group. Example: <mat-button-toggle-group name="fontStyle" aria-label="Font Style">   <mat-button-toggle value="bold">Bold</mat-button-toggle>   <mat-button-toggle value="italic">Italic</mat-button-toggle>   <mat-button-toggle value="underline">Underline</mat-button-toggle> </mat-button-toggle-group>

Angular 10 : Button example

Image
Angular 10 Button Angular Material buttons are native <button> or <a> elements enhanced with Material Design styling and ink ripples. Native <button> and <a> elements are always used in order to provide the most straightforward and accessible experience for users. A <button> element should be used whenever some action is performed. An <a> element should be used whenever the user will navigate to another view. There are several button variants, each applied as an attribute: Attribute   mat-button   Rectangular text button w/ no elevation mat-raised-button Rectangular contained button w/ elevation mat-flat-button Rectangular contained button w/ no elevation mat-stroked-button Rectangular outlined button w/ no elevation mat-icon-button Circular button with a transparent background, meant to contain an icon mat-fab Circular button w/ elevation, defaults to theme's accent color mat-mini-fab Same as mat-fab b...

Spring Batch - Use bulk writes in MongoItemWriter

Use bulk writes in MongoItemWriter Up until now, the MongoItemWriter used MongoOperations.save() in a for loop to save items to the database. In this release, we replaced this mechanism with a single call to BulkOperations. With this change, the MongotItemWriter is 25x faster than the previous version, according to benchmark mongo-item-writer-benchmark.

Use of bulk writes in RepositoryItemWriter

Up to spring-batch version 4.2, it was required to specify the method name to use to save an item to the database. This method was then called in a for loop to save all items. In order to use CrudRepository.saveAll, it was required to extend RepositoryItemWriter and override write(List), which is not convenient. In spring-batch v4.3.0 release, RepositoryItemWriter use CrudRepository.saveAll by default. This changes improves the performance of the writer by a factor of 2, according to our benchmark repository-item-writer-benchmark.

Configuration of Spring Batch tests with JUnit 5

Configuration of Spring Batch tests with JUnit 5 Similar to how many Spring Boot test annotations are meta-annotated with @ExtendWith(SpringExtension.class) (like @SpringBootTest, @WebMvcTest, and others), we updated @SpringBatchTest to be meta-annotated with @ExtendWith(SpringExtension.class). This simplifies the configuration when writing tests with JUnit Jupiter. Please note that this feature does not affect JUnit 4 users, it only concerns JUnit 5 based tests.

Spring Batch JpaPagingItemReader Example

JpaPagingItemReader Spring Batch 4.3.0 Add support for named queries in JpaPagingItemReader. Up until now, it was possible to use named queries with the JpaPagingItemReader. However, this required the creation of a custom query provider, as follows: JpaPagingItemReader<Foo> reader = new JpaPagingItemReaderBuilder<Foo>()     .name("fooReader")     .queryProvider(new AbstractJpaQueryProvider() {        @Override        public Query createQuery() {           return getEntityManager().createNamedQuery("allFoos", Foo.class);        }        @Override        public void afterPropertiesSet() throws Exception {        }     })     // set other properties on the reader     .build(); In this release, we introduced a JpaNamedQueryProvider next to the JpaNativeQueryProvider...

Hibernate JPA @OrderBy Example

@OrderBy The @OrderBy annotation is used to specify a SQL ordering directive for sorting the currently annotated collection. It differs from the JPA @OrderBy annotation because the JPA annotation expects a JPQL order-by fragment, not an SQL directive. OrderBy Order a collection using SQL ordering (not HQL ordering). Different from OrderBy in that this expects SQL fragment, JPA OrderBy expects a valid JPQL order-by fragment. Customizing ORDER BY SQL clause While the JPA @OrderBy annotation allows you to specify the entity attributes used for sorting when fetching the current annotated collection, the Hibernate specific @OrderBy annotation is used to specify a SQL clause instead. In the following example, the @OrderBy annotation uses the CHAR_LENGTH SQL function to order the Article entities by the number of characters of the name attribute. Example : @OrderBy mapping example @Entity(name = "Person") public static class Person { @Id private Long id; pri...

Hibernate JPA @OptimisticLocking Example

@OptimisticLocking The @OptimisticLocking annotation is used to specify the currently annotated entity’s optimistic locking strategy. The four possible strategies are defined by the OptimisticLockType enumeration: NONE The implicit optimistic locking mechanism is disabled. VERSION The implicit optimistic locking mechanism is using a dedicated version column. ALL The implicit optimistic locking mechanism is using all attributes as part of an expanded WHERE clause restriction for the UPDATE and DELETE SQL statements. DIRTY The implicit optimistic locking mechanism is using the dirty attributes (the attributes that were modified) as part of an expanded WHERE clause restriction for the UPDATE and DELETE SQL statements. OptimisticLocking Used to define the style of optimistic locking to be applied to an entity. In a hierarchy, only valid on the root entity. Versionless optimistic locking Although the default @Version property optimistic locking mechanism is sufficie...

Hibernate JPA @OptimisticLock Example

@OptimisticLock The @OptimisticLock annotation is used to specify if the currently annotated attribute will trigger an entity version increment upon being modified. OptimisticLock Whether or not a change of the annotated property will trigger an entity version increment. If the annotation is not present, the property is involved in the optimistic lock strategy (default). Excluding attributes By default, every entity attribute modification is going to trigger a version incrementation. If there is an entity property which should not bump up the entity version, then you need to annotate it with the Hibernate @OptimisticLock annotation, as illustrated in the following example. Example : @OptimisticLock mapping example @Entity(name = "Phone") public static class Phone { @Id private Long id; @Column(name = "`number`") private String number; @OptimisticLock( excluded = true ) private long callCount; @Version private Long version; //Gett...

Hibernate JPA @OnDelete Example

@OnDelete The @OnDelete annotation is used to specify the delete strategy employed by the currently annotated collection, array or joined subclasses. This annotation is used by the automated schema generation tool to generated the appropriate FOREIGN KEY DDL cascade directive. The two possible strategies are defined by the OnDeleteAction enumeration: CASCADE Use the database FOREIGN KEY cascade capabilities. NO_ACTION Take no action. OnDelete Strategy to use on collections, arrays and on joined subclasses delete. OnDelete of secondary tables currently not supported. @OnDelete cascade While the previous cascade types propagate entity state transitions, the @OnDelete cascade is a DDL-level FK feature which allows you to remove a child record whenever the parent row is deleted. So, when annotating the @ManyToOne association with @OnDelete( action = OnDeleteAction.CASCADE ), the automatic schema generator will apply the ON DELETE CASCADE SQL directive to the Foreign Key...

Hibernate JPA @NotFound Example

@NotFound The @NotFound annotation is used to specify the NotFoundAction strategy for when an element is not found in a given association. NotFound Action to do when an element is not found on a association. The NotFoundAction defines two possibilities: EXCEPTION An exception is thrown when an element is not found (default and recommended). IGNORE Ignore the element when not found in the database. @NotFound association mapping When dealing with associations which are not enforced by a Foreign Key, it’s possible to bump into inconsistencies if the child record cannot reference a parent entity. By default, Hibernate will complain whenever a child association references a non-existing parent record. However, you can configure this behavior so that Hibernate can ignore such an Exception and simply assign null as a parent object referenced. To ignore non-existing parent entity references, even though not really recommended, it’s possible to use the annotation org.hiber...

Hibernate JPA @NaturalIdCache Example

@NaturalIdCache The @NaturalIdCache annotation is used to specify that the natural id values associated with the annotated entity should be stored in the second-level cache. NaturalIdCache Used to specify that the natural id values associated with the annotated entity should be cached in Hibernate's shared (L2) cache. Can optionally name a custom cache region. Natural id caching @Entity(name = "Book") @NaturalIdCache public static class Book { @Id private Long id; private String title; private String author; @NaturalId private String isbn; //Getters and setters are omitted for brevity }

Hibernate JPA @NaturalId Example

@NaturalId The @NaturalId annotation is used to specify that the currently annotated attribute is part of the natural id of the entity. NaturalId This specifies that a property is part of the natural id of the entity. Natural Ids Natural ids represent domain model unique identifiers that have a meaning in the real world too. Even if a natural id does not make a good primary key (surrogate keys being usually preferred), it’s still useful to tell Hibernate about it. As we will see later, Hibernate provides a dedicated, efficient API for loading an entity by its natural id much like it offers for loading by its identifier (PK). Natural Id Mapping Natural ids are defined in terms of one or more persistent attributes. Example : Natural id using single basic attribute @Entity(name = "Book") public static class Book { @Id private Long id; private String title; private String author; @NaturalId private String isbn; //Getters and setters are omitte...

Hibernate JPA @Nationalized Example

@Nationalized The @Nationalized annotation is used to specify that the currently annotated attribute is a character type (e.g. String, Character, Clob) that is stored in a nationalized column type (NVARCHAR, NCHAR, NCLOB). Nationalized Marks a character data type (String, Character, character, Clob) as being a nationalized variant (NVARCHAR, NCHAR, NCLOB, etc). Mapping Nationalized Character Data JDBC 4 added the ability to explicitly handle nationalized character data. To this end, it added specific nationalized character data types: NCHAR NVARCHAR LONGNVARCHAR NCLOB Considering we have the following database table: Example 46. NVARCHAR - SQL CREATE TABLE Product (     id INTEGER NOT NULL ,     name VARCHAR(255) ,     warranty NVARCHAR(255) ,     PRIMARY KEY ( id ) ) To map a specific attribute to a nationalized variant data type, Hibernate defines the @Nationalized annotation. Example : NVARCHAR mapping @Entity(...

Hibernate JPA @NamedQuery Example

@NamedQuery The @NamedQuery annotation extends the JPA @NamedQuery with Hibernate specific features, like: flush mode for this particular query if the query should be cached, and which cache region should be used the selected entity CacheModeType strategy the JDBC Statement fetch size the JDBC Statement execution timeout if the query is a CallableStatement, targeting a stored procedure or a database function what SQL-level comment should be sent to the database if the query is read-only, hence it does not store the resulted entities into the currently running Persistence Context NamedQuery Specifies a static, named query in the Java Persistence query language. Query names are scoped to the persistence unit. The NamedQuery annotation can be applied to an entity or mapped superclass. The following is an example of the definition of a named query in the Java Persistence query language:     @NamedQuery(             name="findAllC...

Hibernate JPA @NamedNativeQuery Example

@NamedNativeQuery The @NamedNativeQuery annotation extends the JPA @NamedNativeQuery with Hibernate specific features, like: flush mode for this particular query if the query should be cached, and which cache region should be used the selected entity CacheModeType strategy the JDBC Statement fetch size the JDBC Statement execution timeout if the query is a CallableStatement, targeting a stored procedure or a database function what SQL-level comment should be sent to the database if the query is read-only, hence it does not store the resulted entities into the currently running Persistence Context  Hibernate NamedNativeQuery @NamedNativeQueries({     @NamedNativeQuery(         name = "get_person_phone_count",         query = "SELECT pr.name AS name, count(*) AS phoneCount " +                 "FROM Phone p " +                 "JOI...

Hibernate JPA @MetaValue Eample

@MetaValue The @MetaValue annotation is used by the @AnyMetaDef annotation to specify the association between a given discriminator value and an entity type. MetaValue Maps a given discriminator value to the corresponding entity type. See Any for more information. Any Defines a ToOne-style association pointing to one of several entity types depending on a local discriminator, as opposed to discriminated inheritance where the discriminator is kept as part of the entity hierarchy. For example, if you consider an Order entity containing Payment information where Payment might be of type CashPayment or CreditCardPayment the @Any approach would be to keep that discriminator and matching value on the Order itself. Thought of another way, the "foreign-key" really is made up of the value and discriminator (there is no physical foreign key here as databases do not support this):     @Entity     class Order {         ...       ...

Hibernate JPA @MapKeyType Example

@MapKeyType The @MapKeyType annotation is used to specify the map key type. MapKeyType Allows defining the type of the key of a persistent map. @MapKeyType mapping example @Entity @Table(name = "person") public static class Person { @Id private Long id; @ElementCollection @CollectionTable( name = "call_register", joinColumns = @JoinColumn(name = "person_id") ) @MapKeyType( @Type( type = "org.hibernate.userguide.collections.type.TimestampEpochType" ) ) @MapKeyColumn( name = "call_timestamp_epoch" ) @Column(name = "phone_number") private Map<Date, Integer> callRegister = new HashMap<>(); //Getters and setters are omitted for brevity }

Version 10 of Angular Now Available | What's new in Angular 10?

Angular Version 10 Released! This is a major release that spans the entire platform, including the framework, Angular Material, and the CLI. What’s in Angular 10 release? New Date Range Picker Angular Material now includes a new date range picker. <mat-form-field>   <mat-label>Enter a date range</mat-label>   <mat-date-range-input [rangePicker]="picker">     <input matStartDate matInput placeholder="Start date">     <input matEndDate matInput placeholder="End date">   </mat-date-range-input>   <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>   <mat-date-range-picker #picker></mat-date-range-picker> </mat-form-field> To use the new date range picker, you can use the mat-date-range-input and mat-date-range-picker components. Warnings about CommonJS imports When you use a dependency that is packaged with CommonJS, it can result i...

Hibernate JPA @Loader Example

@Loader The @Loader annotation is used to override the default SELECT query used for loading an entity. Loader Used to override how Hibernate performs load operations. naming a named query to use instead of its generated SELECT SQL. Custom CRUD @Entity(name = "Person") @SQLInsert( sql = "INSERT INTO person (name, id, valid) VALUES (?, ?, true) ", check = ResultCheckStyle.COUNT ) @SQLUpdate( sql = "UPDATE person SET name = ? where id = ? " ) @SQLDelete( sql = "UPDATE person SET valid = false WHERE id = ? " ) @Loader(namedQuery = "find_valid_person") @NamedNativeQueries({ @NamedNativeQuery( name = "find_valid_person", query = "SELECT id, name " + "FROM person " + "WHERE id = ? and valid = true", resultClass = Person.class ) }) public static class Person { @Id @GeneratedValue private Long id; private String name; @ElementCollection @S...

Hibernate JPA @ListIndexBase Example

@ListIndexBase The @ListIndexBase annotation is used to specify the start value for a list index, as stored in the database. By default, List indexes are stored starting at zero. Generally used in conjunction with @OrderColumn. ListIndexBase Defines the start index value for a list index as stored on the database. This base is subtracted from the incoming database value on reads to determine the List position; it is added to the List position index when writing to the database. By default list indexes are stored starting at zero. Generally used in conjunction with OrderColumn. Customizing ordered list ordinal You can customize the ordinal of the underlying ordered list by using the @ListIndexBase annotation. Example : @ListIndexBase mapping example @OneToMany(mappedBy = "person", cascade = CascadeType.ALL) @OrderColumn(name = "order_id") @ListIndexBase(100) private List<Phone> phones = new ArrayList<>(); When inserting two Phone rec...

Hibernate JPA @LazyToOne Example

@LazyToOne The @LazyToOne annotation is used to specify the laziness options, represented by LazyToOneOption, available for a @OneToOne or @ManyToOne association. LazyToOne Define the laziness options available for a ToOne (ie OneToOne or ManyToOne) association. LazyToOneOption defines the following alternatives: FALSE Eagerly load the association. This one is not needed since the JPA FetchType.EAGER offers the same behavior. NO_PROXY This option will fetch the association lazily while returning real entity object. PROXY This option will fetch the association lazily while returning a proxy instead. Bidirectional @OneToOne lazy association Although you might annotate the parent-side association to be fetched lazily, Hibernate cannot honor this request since it cannot know whether the association is null or not. The only way to figure out whether there is an associated record on the child side is to fetch the child association using a secondary query. Because this ...

Hibernate JPA @LazyGroup Example

@LazyGroup The @LazyGroup annotation is used to specify that an entity attribute should be fetched along with all the other attributes belonging to the same group. To load entity attributes lazily, bytecode enhancement is needed. By default, all non-collection attributes are loaded in one group named "DEFAULT". This annotation allows defining different groups of attributes to be initialized together when access one attribute in the group. LazyGroup For use with bytecode-enhanced lazy-loading support. Identifies grouping for performing lazy attribute loading. By default all non-collection attributes are loaded in one group named "DEFAULT". This annotation allows defining different groups of attributes to be initialized together when access one attribute in the group. @LazyGroup example @Entity public class Customer { @Id private Integer id; private String name; @Basic( fetch = FetchType.LAZY ) private UUID accountsPayableXrefId; @Lob ...

Convert JSON To Class Object In Java

Convert JSON To Class Object In Java In this example we will see how to convert json string to java object with spring framework and jackson apis. Example: Json: String name= "{ "name" : "Text1" }"; Create a class for json attribute: class User{ String name; } import com.fasterxml.jackson.anotation.JsonInclude; import com.fasterxml.jackson.databind.ObjectMapper; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; public class ConvertJsonToJavaClassObject{ public static void main(String args[]){ String userString = //json string ObjectMapper objectMapper = new Jackson2ObjectMapperBuilder().build(); User user = objectMapper.readValue(userString, User.class); } } JSON to Collection object: json to list: Jsong string: String name = { users:[{ "name" :"name1" }, { "name" :"name2" } ] } Java Example: class Users { List<User> users; } ...