[Java Spring] @Embeddable and @EmbeddedId
In software, we come across many use cases when we need to have a composite primary key to define an entry in a table. Composite primary keys are keys that use more than one column to identify a row in the table uniquely.
We represent a composite primary key in Spring Data by using the @Embeddable annotation on a class. This key is then embedded in the table's corresponding entity class as the composite primary key by using the @EmbeddedId annotation on a field of the @Embeddable type.
package com.example.ec.domain; import javax.persistence.Column; import javax.persistence.EmbeddedId; import javax.persistence.Entity; import java.util.Objects; @Entity public class TourRating { @EmbeddedId private TourRatingPk pk; @Column(nullable = false) private Integer score; @Column private String comment; public TourRating(TourRatingPk pk, Integer score, String comment) { this.pk = pk; this.score = score; this.comment = comment; } public TourRating() { } @Override public String toString() { return "TourRating{" + "pk=" + pk + ", score=" + score + ", comment='" + comment + '\'' + '}'; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; TourRating that = (TourRating) o; return Objects.equals(pk, that.pk) && Objects.equals(score, that.score) && Objects.equals(comment, that.comment); } @Override public int hashCode() { return Objects.hash(pk, score, comment); } public TourRatingPk getPk() { return pk; } public void setPk(TourRatingPk pk) { this.pk = pk; } public Integer getScore() { return score; } public void setScore(Integer score) { this.score = score; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } }
package com.example.ec.domain; import javax.persistence.Column; import javax.persistence.Embeddable; import javax.persistence.ManyToOne; import java.io.Serializable; @Embeddable public class TourRatingPk implements Serializable { @ManyToOne private Tour tour; @Column(insertable = false, updatable = false,nullable = false) private Integer customerId; public TourRatingPk(Tour tour, Integer customerId) { this.tour = tour; this.customerId = customerId; } public Tour getTour() { return tour; } public Integer getCustomerId() { return customerId; } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; TourRatingPk that = (TourRatingPk) o; if (!tour.equals(that.tour)) return false; return customerId.equals(that.customerId); } @Override public int hashCode() { int result = tour.hashCode(); result = 31 * result + customerId.hashCode(); return result; } @Override public String toString() { return "TourRatingPk{" + "tour=" + tour + ", customerId=" + customerId + '}'; } }
package com.example.ec.repo; import com.example.ec.domain.TourRating; import com.example.ec.domain.TourRatingPk; import org.springframework.data.repository.CrudRepository; import org.springframework.data.rest.core.annotation.RepositoryRestResource; import java.util.List; import java.util.Optional; @RepositoryRestResource(exported = false) public interface TourRatingRepository extends CrudRepository<TourRating, TourRatingPk> { List<TourRating> findByPkTourId(Integer tourId); Optional<TourRating> findByPkTourIdAndPkCustomerId(Integer tourId, Integer customerId); }