Posts

Showing posts with the label Spring Data

Spring supplier Example

Non Spring Developers Let us assume for a second that you are not a Spring developer and not familiar with Spring Integration which already provides abstractions for ROME. In that case, we can certainly use ROME directly to produce feed records. For example, this is a valid Supplier for this scenario. public Supplier<SyndEntry> feedSupplier() { return () -> { //Use the ROME framework directly to produce syndicated entries. } } The benefit here is that we can develop the supplier without any knowledge of Spring, and it can be deployed to a serverless environment directly, using the abstractions provided by that environment or by relying on a framework like Spring Cloud Function. This essentially means that if you are a Java developer without much Spring Framework skills, you can still write the functions using just the interfaces defined in the java.util.function package such as Function, Supplier and Consumer, by providing the business logic.  Spring Developers Add ...

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 @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...

Spring Data Elasticsearch MappingElasticsearchConverter

Spring Data Elasticsearch MappingElasticsearchConverter  The MappingElasticsearchConverter uses metadata to drive the mapping of objects to documents. The metadata is taken from the entity’s properties which can be annotated. The following annotations are available: @Document: Applied at the class level to indicate this class is a candidate for mapping to the database. The most important attributes are: indexName: the name of the index to store this entity in type: the mapping type. If not set, the lowercased simple name of the class is used. (deprecated since version 4.0) shards: the number of shards for the index. replicas: the number of replicas for the index. refreshIntervall: Refresh interval for the index. Used for index creation. Default value is "1s". indexStoreType: Index storage type for the index. Used for index creation. Default value is "fs". createIndex: Configuration whether to create an index on repository bootstrapping. Default...

Hibernate JPA @FilterJoinTables Example

@FilterJoinTables The @FilterJoinTables annotation is used to group multiple @FilterJoinTable annotations. FilterJoinTables- Add multiple @FilterJoinTable to a collection.

Hibernate JPA @FilterJoinTable Example

@FilterJoinTable The @FilterJoinTable annotation is used to add @Filter capabilities to a join table collection. FilterJoinTable - Add filters to a join table collection. @FilterJoinTable When using the @Filter annotation with collections, the filtering is done against the child entries (entities or embeddables). However, if you have a link table between the parent entity and the child table, then you need to use the @FilterJoinTable to filter child entries according to some column contained in the join table. The @FilterJoinTable annotation can be, therefore, applied to a unidirectional @OneToMany collection as illustrated in the following mapping: Example :  @FilterJoinTable mapping usage @Entity(name = "Client") @FilterDef(     name="firstAccounts",     parameters=@ParamDef(         name="maxOrderId",         type="int"     ) ) public static class Client {     @Id   ...