I am working on a CRUD application by referring to Practical Guide to Building API backend with Spring Boot by Wim Deblauwe. I'm stuck in the part where they are telling to implement an unique id generator whenever we create an instance of the entity class to save to the database. Could someone please help with the implementation part of the id generator as I tried but failed miserably.
AbstractEntityId.java
package com.example.orm.jpa;
import com.example.util.ArtifactForFramework;
import javax.persistence.MappedSuperclass;
import java.io.Serializable;
import java.util.Objects;
import static com.google.common.base.MoreObjects.toStringHelper;
@MappedSuperclass
public abstract class AbstractEntityId<T extends Serializable> implements Serializable,
EntityId<T> {
private T id;
@ArtifactForFramework
protected AbstractEntityId() {
}
protected AbstractEntityId(T id) {
this.id = Objects.requireNonNull(id);
}
@Override
public T getId() {
return id;
}
@Override
public String asString() {
return id.toString();
}
@Override
public boolean equals(Object o) {
boolean result = false;
if (this == o) {
result = true;
} else if (o instanceof AbstractEntityId) {
AbstractEntityId other = (AbstractEntityId) o;
result = Objects.equals(id, other.id);
}
return result;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return toStringHelper(this)
.add("id", id)
.toString();
}
}
AbstractEntity.java
package com.example.orm.jpa;
import com.example.util.ArtifactForFramework;
import javax.persistence.EmbeddedId;
import javax.persistence.MappedSuperclass;
import java.util.Objects;
import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Abstract super class for entities. We are assuming that early primary key
* generation will be used.
*
* @param <T> the type of {@link EntityId} that will be used for this entity
*/
@MappedSuperclass
public abstract class AbstractEntity<T extends EntityId> implements Entity<T> {
@EmbeddedId
private T id;
@ArtifactForFramework
protected AbstractEntity() {
}
public AbstractEntity(T id) {
this.id = checkNotNull(id);
}
@Override
public T getId() {
return id;
}
@Override
public boolean equals(Object obj) {
boolean result = false;
if (this == obj) {
result = true;
} else if (obj instanceof AbstractEntity) {
AbstractEntity other = (AbstractEntity) obj;
result = Objects.equals(id, other.id);
}
return result;
}
@Override
public int hashCode() {
return Objects.hash(id);
}
@Override
public String toString() {
return toStringHelper(this)
.add("id", id)
.toString();
}
}
EntityId.java
package com.example.orm.jpa;
import java.io.Serializable;
/**
* Interface for primary keys of entities.
*
* @param <T> the underlying type of the entity id
*/
public interface EntityId<T> extends Serializable {
T getId();
String asString(); ①
}
package com.example.copsboot.user;
public interface UserRepositoryCustom {
UserId nextId();
}
public interface UserRepository extends CrudRepository<User, UUID>, UserRepositoryCustom
{
}
package com.example.copsboot.user;
import com.example.orm.jpa.UniqueIdGenerator;
import java.util.UUID;
public class UserRepositoryImpl implements UserRepositoryCustom {
private final UniqueIdGenerator<UUID> generator;
public UserRepositoryImpl(UniqueIdGenerator<UUID> generator) {
this.generator = generator;
}
@Override
public UserId nextId() {
return new UserId(generator.getNextUniqueId());
}
}
The last section of code is where they are asking for the generation(generator.getNextUniqueId()). All the above codes are for context. It could be really helpful if someone also explained why they are implementing these abstract entity classes for id generation.
Thanks in advance