当前位置: 代码迷 >> 综合 >> SpringData JPA复合主键
  详细解决方案

SpringData JPA复合主键

热度:11   发布时间:2023-10-16 10:50:31.0

前言

这次有个需求,给项目角色创建一个模板,在数据库建表时发现,模板表template_project_role的主键需要为复合主键:

SpringData JPA复合主键

 

正文

这里采用@IdClass注解的方式来实现复合主键;

思路

  1. 编写一个复合主键类TemplateProjectRoleKey;
  2. 通过@IdClass注释在实体中标注复合主键;
  3. Repository 里标明主键为TemplateProjectRoleKey类;

实现

复合主键类

package com.tlgc.system.mvc.model.projectmanager;import javax.persistence.Column;
import javax.persistence.Id;public class TemplateProjectRoleKey implements java.io.Serializable {/**构造函数,根据自己的主键修改**/public TemplateProjectRoleKey(){}public TemplateProjectRoleKey(String roleId,String templateId){this.roleId = roleId;this.templateId = templateId;}/**构造函数,根据自己的主键修改**//**主键列,根据自己的主键修改**/private String roleId;private String templateId;@Column(name = "template_id", length = 50)public String getTemplateId() {return templateId;}public void setTemplateId(String templateId) {this.templateId = templateId;}@Column(name = "role_id", length = 50)public String getRoleId() {return roleId;}public void setRoleId(String roleId) {this.roleId = roleId;}/**主键列,根据自己的主键修改**//**重点,重写hashCode 和equals方法**/@Overridepublic int hashCode() {final int PRIME = 31;int result = 1;/**重点,根据自己的主键修改**/result = PRIME * result + ((roleId == null) ? 0 : roleId.hashCode());result = PRIME * result + ((templateId == null) ? 0 : templateId.hashCode());/**重点,根据自己的主键修改**/return result;}@Overridepublic boolean equals(Object obj){if(this == obj){return true;}if(obj == null){return false;}if(getClass() != obj.getClass()){return false;}final TemplateProjectRoleKey other = (TemplateProjectRoleKey)obj;/**重点,根据自己的主键修改**/if(roleId == null){if(other.roleId != null){return false;}}else if(!roleId.equals(other.roleId)){return false;}if(templateId == null){if(other.templateId != null){return false;}}else if(!templateId.equals(other.templateId)){return false;}/**重点,根据自己的主键修改**/return true;}
}

实体类

package com.tlgc.system.mvc.model.projectmanager;import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenericGenerator;import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;@Entity
@Table(name = "template_project_role")
@DynamicInsert(true)
@DynamicUpdate(true)
@IdClass(TemplateProjectRoleKey.class)/**重点,注解@IdClass标注用于标注实体所使用主键规则的类**/
public class TemplateProjectRole  implements Serializable {private static final long serialVersionUID = -7509069780835660418L;private String roleName;/**重点,主键列**/private String roleId;private String templateId;@Id@Column(name = "template_id", length = 50)public String getTemplateId() {return templateId;}public void setTemplateId(String templateId) {this.templateId = templateId;}@Id@Column(name = "role_id", length = 50)public String getRoleId() {return roleId;}public void setRoleId(String roleId) {this.roleId = roleId;}/**重点,主键列**/@Column(name = "role_name")public String getRoleName() {return roleName;}public void setRoleName(String roleName) {this.roleName = roleName;}}

注意: 
1. @IdClass标注用于标注实体所使用主键规则的类; 

Repository类

标明主键为TemplateProjectRoleKey类,JpaRepository<TemplateProjectRole, TemplateProjectRoleKey>

package com.tlgc.system.mvc.repository.projectmanager;import com.tlgc.system.mvc.model.projectmanager.TemplateProjectRole;
import com.tlgc.system.mvc.model.projectmanager.TemplateProjectRoleKey;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;public interface TemplateProjectRoleRepository extends JpaRepository<TemplateProjectRole, TemplateProjectRoleKey>, JpaSpecificationExecutor<TemplateProjectRole> {}