[BAEL-6535] hydration of object examples (#15421)
Co-authored-by: Bhaskar <bhaskar.dastidar@freshworks.com>
This commit is contained in:
parent
72b6c78646
commit
b89c49b834
|
@ -97,6 +97,11 @@
|
|||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.3.170</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
|
@ -150,6 +155,16 @@
|
|||
<artifactId>xstream</artifactId>
|
||||
<version>1.4.16</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-jpa</artifactId>
|
||||
<version>2.1.5.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.persistence</groupId>
|
||||
<artifactId>javax.persistence-api</artifactId>
|
||||
<version>2.2</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
|
|
@ -0,0 +1,75 @@
|
|||
package com.baeldung.objecthydration;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Objects;
|
||||
|
||||
public class User implements Serializable {
|
||||
private String uId;
|
||||
private String firstName;
|
||||
private String lastName;
|
||||
private String alias;
|
||||
|
||||
public User() {
|
||||
}
|
||||
|
||||
public User(String firstName, String uId, String lastName, String alias) {
|
||||
this.firstName = firstName;
|
||||
this.uId = uId;
|
||||
this.lastName = lastName;
|
||||
this.alias = alias;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o)
|
||||
return true;
|
||||
if (o == null || getClass() != o.getClass())
|
||||
return false;
|
||||
User user = (User) o;
|
||||
return Objects.equals(firstName, user.firstName) && Objects.equals(uId, user.uId) && Objects.equals(lastName, user.lastName) && Objects.equals(alias, user.alias);
|
||||
}
|
||||
|
||||
public String getuId() {
|
||||
return uId;
|
||||
}
|
||||
|
||||
public void setuId(String uId) {
|
||||
this.uId = uId;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public void setFirstName(String firstName) {
|
||||
this.firstName = firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
|
||||
public void setLastName(String lastName) {
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public String getAlias() {
|
||||
return alias;
|
||||
}
|
||||
|
||||
public void setAlias(String alias) {
|
||||
this.alias = alias;
|
||||
}
|
||||
|
||||
public void generateMyUser() {
|
||||
this.setAlias("007");
|
||||
this.setFirstName("James");
|
||||
this.setLastName("Bond");
|
||||
this.setuId("JB");
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(firstName, uId, lastName, alias);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.objecthydration;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class UserSerialisationDeserialisation {
|
||||
public void serialisedUser(User user, String outputName) {
|
||||
try {
|
||||
FileOutputStream fileOut = new FileOutputStream(outputName);
|
||||
ObjectOutputStream out = new ObjectOutputStream(fileOut);
|
||||
out.writeObject(user);
|
||||
out.close();
|
||||
fileOut.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public User deserialiseUser(String serialisedFile) {
|
||||
User deserializedUser = null;
|
||||
try {
|
||||
FileInputStream fileIn = new FileInputStream(serialisedFile);
|
||||
ObjectInputStream in = new ObjectInputStream(fileIn);
|
||||
deserializedUser = (User) in.readObject();
|
||||
in.close();
|
||||
fileIn.close();
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
File f = new File(serialisedFile);
|
||||
f.delete();
|
||||
}
|
||||
return deserializedUser;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,71 @@
|
|||
package com.baeldung.objecthydration.entity;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
@Entity
|
||||
@Table(name = "books")
|
||||
public class Book {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private Long id;
|
||||
|
||||
@Column(name = "name")
|
||||
private String name;
|
||||
|
||||
@Column(name = "author")
|
||||
private String author;
|
||||
|
||||
@Column(name = "isbn")
|
||||
private String isbn;
|
||||
|
||||
// Constructors, getters, and setters
|
||||
|
||||
public Book() {
|
||||
// Default constructor required by Hibernate
|
||||
}
|
||||
|
||||
public Book(String name, String author, String isbn) {
|
||||
this.name = name;
|
||||
this.author = author;
|
||||
this.isbn = isbn;
|
||||
}
|
||||
|
||||
// Getters and setters for all attributes
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getAuthor() {
|
||||
return author;
|
||||
}
|
||||
|
||||
public void setAuthor(String author) {
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
public String getIsbn() {
|
||||
return isbn;
|
||||
}
|
||||
|
||||
public void setIsbn(String isbn) {
|
||||
this.isbn = isbn;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
package com.baeldung.objecthydration.repository;
|
||||
|
||||
import com.baeldung.objecthydration.entity.Book;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
public interface BookRepository extends JpaRepository<Book, Long> {
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.objecthydration;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class HydrationUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenEmptyClass_whenLazilyinitialised_thenGiveNullOnGet() {
|
||||
User iamUser = new User();
|
||||
Assert.assertNull(iamUser.getuId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInitialisedClass_withLazilyInitialised_thenDoesNotGiveNullOnGet() {
|
||||
User iamUser = new User();
|
||||
iamUser.setAlias("007");
|
||||
Assert.assertNotNull(iamUser.getAlias());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyClass_withHydration_thenShouldHaveAllAttributesSet() {
|
||||
User jamesBond = new User();
|
||||
Assert.assertNull(jamesBond.getAlias());
|
||||
|
||||
jamesBond.generateMyUser();
|
||||
Assert.assertEquals("007", jamesBond.getAlias());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUser_thenShouldPerformUserSerialisationDeserialisation() {
|
||||
User iamUser = new User();
|
||||
iamUser.setAlias("007");
|
||||
UserSerialisationDeserialisation usd = new UserSerialisationDeserialisation();
|
||||
usd.serialisedUser(iamUser, "bond.ser");
|
||||
User deserialisedUser = usd.deserialiseUser("bond.ser");
|
||||
Assert.assertEquals(iamUser.getAlias(), deserialisedUser.getAlias());
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue