带有复合主键的表在SpringBoot中的使用

在SpringBoot开发过程中,我们会遇到有些mysql表是带有复合主键的,此时我们进行开发的过程中需要注意以下方面.

1,首先在该表的实例类中的复合主键前面添加如下注解:

@EmbeddedId
比如下面的例子:我的表叫 app_network_configs 这个名字,对应的类是: AppNetwork,对应的复合主键是: AppNetworkKey(AppNetworkKey这个单独拎出来使之形成独立的一个类)

@EmbeddedId
private AppNetworkKey pk = new AppNetworkKey();
 
  
在复合主键的get方法中添加注解:@Transient ,如下的例子:
 
  
@Transient
public App getApp() {
    return getPk().getApp();
}

@Transient
public Network getNetwork() {
    return getPk().getNetwork();
}

2,在AppNetworkKey 中我们需要在类的头顶加上这样的注解:@Embeddable
然后后面的就按照正常的方式进行,特别注意,这个类中需要一个空的构造器和含有对应复合主键字段的构造器.
 
  
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "app_id")
private App app;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "network_id")
private Network network;

public AppNetworkKey() {
}

public AppNetworkKey(App app, Network network) {
    this.app = app;
    this.network = network;
}
3,使用的例子:
有了上面的表,我们可以做如下的操作:
 
  
public interface AppNetworkRepository extends JpaRepository {
    @Query("select an from AppNetwork an where an.pk.app.id=:appId")
    List findByAppId(@Param("appId")String appId);

    @Query("select an from AppNetwork an where an.pk.network.id=:networkId")
    List findByNetworkId(@Param("networkId")String networkId);
}

附上上面的表对应的实体类
 
  
@Entity
@Table(name = "app_network_configs")
public class AppNetwork implements Serializable {
    private static final long serialVersionUID = -5779694190187550788L;
    @EmbeddedId
    private AppNetworkKey pk = new AppNetworkKey();
    private String config;
    @Column(name = "config_type")
    private String configType;
    private Double weight;
    @Column(name = "min_ecpm")
    private Double minEcpm;
    private Boolean available;
    @Column(name = "created_at")
    private Date createdAt;
    @Column(name = "modified_at")
    private Date modifiedAt;
    private Boolean synced;
    @Column(name = "synced_at")
    private Date syncedAt;


    public AppNetworkKey getPk() {
        return pk;
    }


    public void setPk(AppNetworkKey pk) {
        this.pk = pk;
    }


    @Transient
    public App getApp() {
        return getPk().getApp();
    }


    @Transient
    public Network getNetwork() {
        return getPk().getNetwork();
    }


    public String getConfig() {
        return config;
    }


    public void setConfig(String config) {
        this.config = config;
    }


    public String getConfigType() {
        return configType;
    }


    public void setConfigType(String configType) {
        this.configType = configType;
    }


    public Double getWeight() {
        return weight;
    }


    public void setWeight(Double weight) {
        this.weight = weight;
    }


    public Double getMinEcpm() {
        return minEcpm;
    }


    public void setMinEcpm(Double minEcpm) {
        this.minEcpm = minEcpm;
    }


    public Boolean getAvailable() {
        return available;
    }


    public void setAvailable(Boolean available) {
        this.available = available;
    }


    public Date getCreatedAt() {
        return createdAt;
    }


    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }


    public Date getModifiedAt() {
        return modifiedAt;
    }


    public void setModifiedAt(Date modifiedAt) {
        this.modifiedAt = modifiedAt;
    }


    public Boolean getSynced() {
        return synced;
    }


    public void setSynced(Boolean synced) {
        this.synced = synced;
    }


    public Date getSyncedAt() {
        return syncedAt;
    }


    public void setSyncedAt(Date syncedAt) {
        this.syncedAt = syncedAt;
    }


}


你可能感兴趣的:(work,summary,springboot,java)