[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); }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
2019-12-15 [Algorithm] 53. Maximum Subarray
2018-12-15 [Algorithm] Breadth First JavaScript Search Algorithm for Graphs
2018-12-15 [Algorithm] JavaScript Graph Data Structure
2017-12-15 [Python] Read and plot data from csv file
2017-12-15 [React] Make Controlled React Components with Control Props
2016-12-15 [JS Compose] 6. Semigroup examples
2016-12-15 [JS Compose] 5. Create types with Semigroups