How to make an immutable class in hibernate?
In Hibernate, creating an immutable class involves ensuring that the class cannot be modified after it has been created. This is typically done by making the class final, providing no setters, and ensuring that all fields are final and initialized through the constructor. Additionally, you should ensure that any mutable objects are not exposed directly.
Here’s a step-by-step guide to creating an immutable class in Hibernate:
Here’s an example of an immutable class in Hibernate:
import javax.persistence.Column;
import javax.persistence.Embeddable;
@Embeddable
public final class Address {
@Column(name = "street")
private final String street;
@Column(name = "city")
private final String city;
@Column(name = "zip_code")
private final String zipCode;
// Constructor
public Address(String street, String city, String zipCode) {
this.street = street;
this.city = city;
this.zipCode = zipCode;
}
// Getters
public String getStreet() {
return street;
}
public String getCity() {
return city;
}
public String getZipCode() {
return zipCode;
}
// Override equals and hashCode for proper comparison
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Address)) return false;
Address address = (Address) o;
return street.equals(address.street) &&
city.equals(address.city) &&
zipCode.equals(address.zipCode);
}
@Override
public int hashCode() {
return Objects.hash(street, city, zipCode);
}
}
You can use the immutable class as a field in an entity class. Here’s an example:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Embedded;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Embedded
private final Address address;
// Constructor
public User(Address address) {
this.address = address;
}
// Getters
public Long getId() {
return id;
}
public Address getAddress() {
return address;
}
}
When you create an instance of the User
class, you will need to provide an Address
instance:
Address address = new Address("123 Main St", "Springfield", "12345");
User user = new User(address);
// Save the user using Hibernate
session.save(user);
By following these guidelines, you can create immutable classes in Hibernate that are safe and efficient.