0

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

user17341714
  • 49
  • 1
  • 1
  • 6
  • Here is a similar question I answered, the links to tutorials might be helpful https://stackoverflow.com/a/76110117/217324. I'm not a fan of the code posted here, using ID for equals is not the best idea and the ID generation is normally done with annotations on the entities, you don't use a repository for it. My preference would be not to use the abstract classes. – Nathan Hughes May 22 '23 at 05:15
  • Hey ty for your response. But im still not understanding it :( – user17341714 May 22 '23 at 05:18
  • 1
    If this code is a representative sample of what the book is like, you need to find a better book. – Nathan Hughes May 22 '23 at 05:20
  • can you care to explain? – user17341714 May 22 '23 at 05:25
  • Are you looking for `@jakarta.persistence.Id` and `@jakarta.persistence.GeneratedValue(strategy=jakarta.persistence.GenerationType.AUTO)` annotations? The column in the DB will also need to be configured to auto generate like so `id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY`. You can find more [here](https://www.baeldung.com/jpa-get-auto-generated-id). – Mirza Prangon May 22 '23 at 05:27
  • @MirzaPrangon the book seemed to miss out on the implementation of generator.getNextUniqueId(). Idts its mentioned anywhere to annotate the id column. Im asking help for the implementation of the getNextUniqueId() method – user17341714 May 22 '23 at 05:33

0 Answers0