JPA联合主键及查询

@Entity
@Table(name = "Water_hour_Year", schema = "")
public class WaterHourYear implements Serializable {

    private static final long serialVersionUID = 8650481607457710790L;
    @EmbeddedId
    private WaterHourPK id;

    @Column(name = "z")
    private BigDecimal z;  ......

 

@Embeddable
public class WaterHourPK implements Serializable {
    @Column(name = "stcd")
    private String stcd;

    @Column(name = "tm")
    private Timestamp tm;

 //根据主键查询

@Repository
public interface IVwWarningReceiversDao extends IBaseDao {
    VwWarningReceivers findByPkPointCodeAndPkHystCode(String pointCode, String hystCode);

    //@EmbeddedId
    //private VwWarningReceiversPk pk;

    //@Column(name = "POINT_CODE" )
    //private String pointCode;

    //@Column(name = "HYST_CODE" )
    //private String hystCode;
}


另一种方式: 

@Data
public class Pk implements Serializable {
    private static final long serialVersionUID = 219360372861451354L;
    private Long userId;

    private Long roleId;

}
@Data
@Entity
@Table(name = "rel")
@IdClass(Pk.class)
public class Rel implements Serializable {

    private static final long serialVersionUID = 3671504291813299190L;
    @Id
    @Column(name = "role_id")
    private Long roleId;

    @Id
    @Column(name = "user_id")
    private Long userId;

}

你可能感兴趣的:(jpa)