Wednesday, 25 September 2019
WebClient Spring 5 vs RestTemplate
Web-client in Spring 5 supports non-blocking asynchronous communications, however, RestTemplate is implemented in a blocking way.
spring-5-webclient
Thursday, 12 September 2019
Tuesday, 3 September 2019
Direction in Entity Relationships
The direction of a relationship can be either bidirectional or unidirectional. A bidirectional relationship has both an owning side and an inverse side. A unidirectional relationship has only an owning side. The owning side of a relationship determines how the Persistence runtime makes updates to the relationship in the database.
Bidirectional Relationships
In a bidirectional relationship, each entity has a relationship field or property that refers to the other entity. Through the relationship field or property, an entity class’s code can access its related object. If an entity has a related field, the entity is said to “know” about its related object. For example, if Order knows what LineItem instances it has and if LineItem knows what Order it belongs to, they have a bidirectional relationship.
Bidirectional relationships must follow these rules.
- The inverse side of a bidirectional relationship must refer to its owning side by using the mappedBy element of the @OneToOne, @OneToMany, or @ManyToMany annotation. The mappedBy element designates the property or field in the entity that is the owner of the relationship.
- The many side of many-to-one bidirectional relationships must not define the mappedBy element. The many side is always the owning side of the relationship.
- For one-to-one bidirectional relationships, the owning side corresponds to the side that contains the corresponding foreign key.
- For many-to-many bidirectional relationships, either side may be the owning side.
Unidirectional Relationships
In a unidirectional relationship, only one entity has a relationship field or property that refers to the other. For example, LineItem would have a relationship field that identifies Product, but Product would not have a relationship field or property for LineItem. In other words, LineItem knows about Product, but Product doesn’t know which LineItem instances refer to it.
Queries and Relationship Direction
Java Persistence query language and Criteria API queries often navigate across relationships. The direction of a relationship determines whether a query can navigate from one entity to another. For example, a query can navigate from LineItem to Product but cannot navigate in the opposite direction. For Order and LineItem, a query could navigate in both directions because these two entities have a bidirectional relationship.
Cascade Operations and Relationships
Entities that use relationships often have dependencies on the existence of the other entity in the relationship. For example, a line item is part of an order; if the order is deleted, the line item also should be deleted. This is called a cascade delete relationship.
The javax.persistence.CascadeType enumerated type defines the cascade operations that are applied in the cascade element of the relationship annotations. Table 20–1 lists the cascade operations for entities.
Table 20–1 Cascade Operations for Entities| Cascade Operation | Description |
|---|---|
| ALL | All cascade operations will be applied to the parent entity’s related entity. All is equivalent to specifying cascade={DETACH, MERGE, PERSIST, REFRESH, REMOVE} |
| DETACH | If the parent entity is detached from the persistence context, the related entity will also be detached. |
| MERGE | If the parent entity is merged into the persistence context, the related entity will also be merged. |
| PERSIST | If the parent entity is persisted into the persistence context, the related entity will also be persisted. |
| REFRESH | If the parent entity is refreshed in the current persistence context, the related entity will also be refreshed. |
| REMOVE | If the parent entity is removed from the current persistence context, the related entity will also be removed. |
Cascade delete relationships are specified using the cascade=REMOVE element specification for @OneToOne and @OneToManyrelationships. For example:
@OneToMany(cascade=REMOVE, mappedBy="customer") public SetgetOrders() { return orders; }
Orphan Removal in Relationships
When a target entity in one-to-one or one-to-many relationship is removed from the relationship, it is often desirable to cascade the remove operation to the target entity. Such target entities are considered “orphans,” and the orphanRemoval attribute can be used to specify that orphaned entities should be removed. For example, if an order has many line items and one of them is removed from the order, the removed line item is considered an orphan. If orphanRemoval is set to true, the line item entity will be deleted when the line item is removed from the order.
The orphanRemoval attribute in @OneToMany and @oneToOne takes a Boolean value and is by default false.
The following example will cascade the remove operation to the orphaned customer entity when it is removed from the relationship:
@OneToMany(mappedBy="customer", orphanRemoval="true") public ListgetOrders() { ... }
Wednesday, 21 August 2019
Is H2 Accessing filtered out???
2019-08-20 23:46:55.835 INFO 17304 --- [ restartedMain] pertySourcedRequestMappingHandlerMapping : Mapped URL path [/v2/api-docs] onto method [public org.springframework.http.ResponseEntity
2019-08-20 23:46:56.231 INFO 17304 --- [ restartedMain] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: org.springframework.security.web.util.matcher.AnyRequestMatcher@1, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@626f6b95, org.springframework.security.web.context.SecurityContextPersistenceFilter@18dc3684, org.springframework.security.web.header.HeaderWriterFilter@c731e97, org.springframework.security.web.authentication.logout.LogoutFilter@70ed9554, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@332ce77b, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@27720351, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@70321b31, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@72cf2f4, org.springframework.security.web.session.SessionManagementFilter@600eb289, org.springframework.security.web.access.ExceptionTranslationFilter@b28bf68, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@783b78d]
2019-08-20 23:46:56.234 INFO 17304 --- [ restartedMain] o.s.s.web.DefaultSecurityFilterChain : Creating filter chain: Ant [pattern='/h2/**'], [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@1bdf7eab, org.springframework.security.web.context.SecurityContextPersistenceFilter@737f3bd, org.springframework.security.web.header.HeaderWriterFilter@ecd60f, org.springframework.security.web.authentication.logout.LogoutFilter@13670ab, org.springframework.security.web.authentication.www.BasicAuthenticationFilter@39a4d12b, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@27a48a12, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@411e6c47, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@32643e76, org.springframework.security.web.session.SessionManagementFilter@bc96433, org.springframework.security.web.access.ExceptionTranslationFilter@1aa74293, org.springframework.security.web.access.intercept.FilterSecurityInterceptor@6c7ee478]
2019-08-20 23:46:56.261 INFO 17304 --- [ restartedMain] o.s.w.s.handler.SimpleUrlHandlerMapping : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
Tuesday, 20 August 2019
How to setup H2 web console in SpringBoot
Springboot may auto-config H2 database, once it sees the dependency in the classpath.
How to set up the H2 database virtual console in Spring boot, to show H2 tables in a browser.
Go to the resource folder, and add the following in the property file
thus h2 browser console will be enabled.
You may specify h2 console path
otherwise, the default will be /h2-console
spring-boot will config the database driver by default
Driver class: org.h2.Driver
You need to specify the JDBC URL for the project
Otherwise, the default value will be
JDBC URL: jdbc url: jdbc:h2:~/test
It may also specify username and password
spring.datasource.usename = xxx
spring.datasource.password = yyy
link to the URL: localhost:8080/h2
you will see the login console.
Click button 'connect', it shows the database management tool in the browser.
How to set up the H2 database virtual console in Spring boot, to show H2 tables in a browser.
Go to the resource folder, and add the following in the property file
spring.h2.console.enabled=true
thus h2 browser console will be enabled.
You may specify h2 console path
spring.h2.console.path=/h2
otherwise, the default will be /h2-console
spring-boot will config the database driver by default
Driver class: org.h2.Driver
You need to specify the JDBC URL for the project
spring.datasource.url= jdbc:h2:file:~/dev-db
Otherwise, the default value will be
JDBC URL: jdbc url: jdbc:h2:~/test
It may also specify username and password
spring.datasource.usename = xxx
spring.datasource.password = yyy
link to the URL: localhost:8080/h2
you will see the login console.
Click button 'connect', it shows the database management tool in the browser.
Sunday, 4 August 2019
Escaping refeences
Escaping references is a way to protect the data from being polluted.
fx: when returning a list of users from a class; even the list is encapsulated, but the reference to the list may be exposed to external after returning the same list to the external. On the case, the content may be modified by an external process.
There are two ways to escape the reference:
1. not returning an instance reference directly; fx. returning un-modified collections(immutable).
2. using the interface to customise the data model that is only readable.
fx: when returning a list of users from a class; even the list is encapsulated, but the reference to the list may be exposed to external after returning the same list to the external. On the case, the content may be modified by an external process.
There are two ways to escape the reference:
1. not returning an instance reference directly; fx. returning un-modified collections(immutable).
2. using the interface to customise the data model that is only readable.
How javax.inject.provider autowired in Spring
When moving a legacy code from the Guice to Spring boot, how to handle the injection of javax.inject.Provider ?
Spring implements JSR 330 staring from Spring 3.0; the javax.inject annotations are supported too in the spring.
It seems that Autowire and Inject are doing the same in the spring;
So it looks that when auto-wiring a Provider, it should return a T type instance(scope = prototype? )
needs to go further to know
Spring - Using JSR 330 Provider to Inject Narrower Scoped Bean
Spring implements JSR 330 staring from Spring 3.0; the javax.inject annotations are supported too in the spring.
It seems that Autowire and Inject are doing the same in the spring;
So it looks that when auto-wiring a Provider, it should return a T type instance(scope = prototype? )
needs to go further to know
Spring - Using JSR 330 Provider to Inject Narrower Scoped Bean
Subscribe to:
Posts (Atom)
Can Jackson Deserialize Java Time ZonedDateTime
Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...
-
The strictfp keyword is used to force the precision of floating point calculations (float or double) in Java conform to IEEE’s 754 standard...
-
In construction. Spring test annotations: @SpringBooTest @DataJpaTest @TestPropertySource @ActiveProfiles @Sql @SpringBootTest It is used f...
-
As the name implies, an anonymous inner class isn’t defined using an explicit name. An anonymous inner class is created when you combine ins...