Found 9150 Articles for Object Oriented Programming

Difference between Iterator and Enumeration in Java

Teja Kolloju
Updated on 17-Apr-2025 19:00:12

7K+ Views

Iterator and Enumeration are both cursors to traverse and access elements from the collection. They both belong to the collection framework. Enumeration was added in JDK1.0 and Iterator in JDK 1.2 version in the collection framework.  Java Enumeration Enumeration: An enumeration is a special "class" that indicates a collection of constants. Enumeration can’t make structural changes in the collection because it has read-only access to its elements. It has the following methods − hasMoreElements(): The hasMoreElements() method checks to see if more elements exist in the underlying collection class nextElement(): The ... Read More

Difference between lazy and eager loading in Hibernate

Mahesh Parahar
Updated on 18-Nov-2019 06:20:10

16K+ Views

Lazy and Eager are two types of data loading strategies in ORMs such as hibernate and eclipse Link.  These data loading strategies we used when one entity class is having references to other Entities like Employee and Phone (phone in the employee). Lazy Loading − Associated data loads only when we explicitly call getter or size method.Use Lazy Loading when you are using one-to-many collections.Use Lazy Loading when you are sure that you are not using related entities. Egare Loading − Data loading happens at the time of their parent is fetched. Use Eager Loading when the relations are not too much. Thus, ... Read More

Difference between Save and SaveAndFlush in Spring Java

Teja Kolloju
Updated on 17-Apr-2025 18:59:54

5K+ Views

Save and SaveAndFlush both can be used for saving entities. They both are both belong to the Spring data library. Save may or may not write your changes to the DB straight away. When we call saveAndFlush system is enforcing the synchronization of your model state with the DB. What is the save method? The save method is used to store an entity in the database. It adds the entity to the transactional buffer, and when the transaction is committed, the data is saved. It then returns the stored entity. Example The following is an example of the save method ... Read More

Difference Between CrudRepository and JPARepository in Java

Teja Kolloju
Updated on 17-Apr-2025 18:59:05

23K+ Views

CrudRepository and JPA repository both are the interface of the spring data repository library. Spring data repository reduces the boilerplate code by providing some predefined finders to access the data layer for various persistence layers. What is JPA repository? JPA is a repository interface that extends CrudRepository and PagingAndSorting repository. It inherits some finders from crud repository such as findOne, gets and removes an entity. It also provides some extra methods related to JPA such as delete records in batch, flushing data directly to a database base and methods related to pagination and sorting. We need to extend this repository ... Read More

Importance of @JsonUnwrapped annotation using Jackson in Java?

raja
Updated on 09-Jul-2020 06:43:52

969 Views

The @JsonUnwrapped annotation can be used to unwrap values during the serialization and deserialization process. It helps to render the values of a composed class as if it belongs to the parent class.Syntax@Target(value={ANNOTATION_TYPE, FIELD, METHOD, PARAMETER}) @Retention(value=RUNTIME) public @interface JsonUnwrappedExampleimport com.fasterxml.jackson.annotation.JsonUnwrapped; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.core.JsonProcessingException; public class JsonUnwrappedAnnotationTest {    public static void main(String args[]) throws JsonProcessingException {       ObjectMapper mapper = new ObjectMapper();       String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(new Employee());       System.out.println(jsonString);    } } class Employee {    public int empId = 110;    public String empName = "Raja Ramesh";    @JsonUnwrapped   ... Read More

When to use @JsonValue annotation using Jackson in Java?

Manisha Chand
Updated on 19-May-2025 16:23:29

5K+ Views

In Java, Jackson is a library that is used to convert JSON to Java objects and vice versa. Jackson Annotations are used during serialization and deserialization. We use these to denote or specify annotations before a particular field or method (that is declared in Java). Using an annotation before a field, we can denote whether it is a variable, is a JsonProperty, should be ignored, or what condition should be applied to it. So basically, Annotations make JSON output clearer as we required. In this Article, we will learn about ... Read More

How to control serialization through @JSON annotation using flexjson in Java?

raja
Updated on 09-Jul-2020 06:17:06

293 Views

The @JSON annotation is used by JSONSerializer class to exclude or include a field during the serialization process. We can use the serialize() method of JSONSerializer class to perform a shallow serialization of the target instance.Syntax@Retention(value=RUNTIME) @Target(value={FIELD, TYPE, METHOD}) public @interface JSONExampleimport flexjson.JSONSerializer; import flexjson.JSON; public class JSONAnnotationTest {    public static void main(String[] args) {       JSONSerializer serializer = new JSONSerializer().prettyPrint(true);       Employee emp = new Employee("Raja", "Ramesh", 30, "Hyderabad");       String jsonStr = serializer.serialize(emp);       System.out.println(jsonStr);    } } // Employee class class Employee {    private String firstName, lastName, address;    private ... Read More

How to implement custom deserializer using @JsonDeserialize annotation in Java?

raja
Updated on 09-Jul-2020 06:19:40

3K+ Views

The @JsonDeserialize annotation is used to declare custom deserializer while deserializing JSON to Java object. We can implement a  custom deserializer by extending the StdDeserializer class with a generic type Employee and need to override the deserialize() method of StdDeserializer class.Syntax@Target(value={ANNOTATION_TYPE, METHOD, FIELD, TYPE, PARAMETER}) @Retention(value=RUNTIME) public @interface JsonDeserializeIn the below program, we can implement a custom deserializer using @JsonDeserialize annotationExampleimport java.io.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.annotation.*; import com.fasterxml.jackson.databind.deser.std.*; public class JsonDeSerializeAnnotationTest {    public static void main (String[] args) throws JsonProcessingException, IOException {       Employee emp = new Employee(115, "Adithya");       ObjectMapper mapper = new ObjectMapper();     ... Read More

How to implement custom serializer using @JsonSerialize annotation in Java?

raja
Updated on 09-Jul-2020 05:49:14

2K+ Views

The @JsonSerialize annotation is used to declare custom serializer during the serialization of a field. We can implement a custom serializer by extending the StdSeralizer class. and need to override the serialize() method of StdSerializer class.Syntax@Target(value={ANNOTATION_TYPE, METHOD, FIELD, TYPE, PARAMETER}) @Retention(value=RUNTIME) public @interface JsonSerializeIn the below program, we can implement a custom serializer using @JsonSerialize annotationExampleimport java.io.*; import com.fasterxml.jackson.core.*; import com.fasterxml.jackson.databind.*; import com.fasterxml.jackson.databind.annotation.*; import com.fasterxml.jackson.databind.ser.std.*; public class JsonSerializeAnnotationTest {    public static void main (String[] args) throws JsonProcessingException, IOException {       Employee emp = new Employee(115, "Adithya", new String[] {"Java", "Python", "Scala"});       ObjectMapper mapper = new ... Read More

How to convert a JSON string to a bean using JSON-lib API in Java?

raja
Updated on 09-Jul-2020 05:18:38

1K+ Views

The JSON-lib API is a java library to serialize and de-serialize java beans, maps, arrays, and collections in the JSON format. We need to convert a JSON string to a bean by converting a string to JSON object first then convert this to a java bean.Syntaxpublic static Object toBean(JSONObject jsonObject, Class beanClass)In the below program, we can convert a JSON string to a bean.Exampleimport net.sf.json.JSONObject; import net.sf.json.JSONSerializer; public class ConvertJSONStringToBeanTest {    public static void main(String[] args) {       String jsonStr = "{\"firstName\": \"Adithya\", \"lastName\": \"Sai\", \"age\": 30, \"technology\": \"Java\"}";       JSONObject jsonObj = (JSONObject)JSONSerializer.toJSON(jsonStr); // convert String ... Read More

Advertisements