title: Java JPA. How select only one specific row from DB if there is couple similar records interpreted like the same (unique) by Java
body: <p>I have this situation: I use JPA with Hibernate and I want to get some data from DB. 
  Unfortunately I have two different sets of unique constraints. There's one on my Database Table and another one slightly different in my java Entity. I can't change one in DB because I don't have access there and I can't change one in java Entity either because it will probably generate errors in other parts of my application.</p>
  
  <p>In Oracle Database I have table <strong>Foo</strong>. Inside I have two similar records:</p>
  
  <p><a href="https://i.stack.imgur.com/BjNZs.png" rel="nofollow noreferrer">table</a></p>
  
  <p>My Java Entity decides if record is unique base on parameters:</p>
  
  <ol>
  <li>List item</li>
  <li>ACC_ID</li>
  <li>PLACE_ID</li>
  <li>INSTALLATION_ID</li>
  <li>DISM_DATE</li>
  <li>BOX_ID</li>
  <li>FOO_ID</li>
  <li>ACC_START_DATE</li>
  </ol>
  
  <p>So after I execute hibernate select query based on Foo Entity I will get only one record from database (because java thinks that both foos from db are the same)</p>
  
  <p>It's OK (I prefer one result), but <strong>can I somehow specify which one of these two records</strong> I will get at the end? Assume that in this case I want to get one with ACTIVITY_FLAG = 'Y' (not first from table, not random and not newest). Is this possible?</p>
  
  <p>Right now I use @Embeddable and @IdClass annotations to set unique parameters in Java code:</p>
  
  <p><strong>Foo.java class:</strong></p>
  
  <pre><code>@Entity
  @Table(name = "Foo")
  @IdClass(FooPK.class)
  
  public final class Foo {
  
     private Long       rowId;
     private Long       boxId;
     private Integer    fooNumber;
     private String     description;
     private BigDecimal upperReadoutDate;
     private BigDecimal upperReadoutDate;
     private String     activityFlag;
     private Long       accId;
     private Long       placeId;
     private Long       installationId;
     private Date       dismountingDate;
     private Date       accStartDate;
  
     @Id
     @Column(name = "FOO_ID")
     public Integer getFooNumber() {
        return fooNumber;
     }
  
     public void setFooNumber(Integer mFooNumber) {
        fooNumber = mFooNumber;
     }
  
     @Column(name = "DESCRIPTION")
     public String getDescription() {
        return description;
     }
  
     public void setDescription(String mDescription) {
        description = mDescription;
     }
  
     @Column(name = "D_READ_DATE")
        public BigDecimal getLowerReadoutDate() {
        return lowerReadoutDate;
     }
  
     public void setLowerReadoutDate(BigDecimal mLowerReadoutDate) {
        lowerReadoutDate = mLowerReadoutDate;
     }
  
     @Column(name = "U_READ_DATE")
     public BigDecimal getUpperReadoutDate() {
        return upperReadoutDate;
     }
  
     public void setUpperReadoutDate(BigDecimal mUpperReadoutDate) {
        upperReadoutDate = mUpperReadoutDate;
     }
  
     @Column(name = "ACTIVITY_FLAG")
     public String getActivityFlag() {
        return activityFlag;
     }
  
     public void setActivityFlag(String mActivityFlag) {
        activityFlag = mActivityFlag;
     }
  
     @Id
     @Column(name = "BOX_ID")
     public Long getBoxId() {
        return boxId;
     }
  
     public void setBoxId(Long mBoxId) {
        boxId = mBoxId;
     }
  
     @Column(name = "ROW_ID")
     public Long getRowId() {
        return rowId;
     }
  
     public void setRowId(Long mRowId) {
        rowId = mRowId;
     }
  
     @Id
     @Column(name = "ACC_ID")
     public Long getAccId() {
        return accId;
     }
  
     public void setAccId(Long mAccId) {
        accId = mAccId;
     }
  
     @Id
     @Column(name = "PLACE_ID")
     public Long getPlaceId() {
        return placeId;
     }
  
     public void setPlaceId(Long mPlaceId) {
        placeId = mPlaceId;
     }
  
     @Id
     @Column(name = "INSTALLATION_ID")
     public Long getInstallationId() {
        return installationId;
     }
  
     public void setInstallationId(Long mInstallationId) {
        installationId = mInstallationId;
     }
  
     @Id
     @Column(name = "DISM_DATE")
     public Date getDismountingDate() {
        return dismountingDate;
     }
  
     public void setDismountingDate(Date mDismountingDate) {
        dismountingDate = mDismountingDate;
     }
  
     @Column(name = "ACC_START_DATE")
     public Date getAccStartDate() {
        return accStartDate;
     }
  
     public void setKuStartDate(Date mKuStartDate) {
        kuStartDate = mKuStartDate;
     }
  
  }
  </code></pre>
  
  <p><strong>FooPK.java class:</strong></p>
  
  <pre><code>@Embeddable
  public class FooPK implements Serializable {
  
     private static final long serialVersionUID = xxxxxxxxxxxxxxxxxxxL;
     private Long              accId;
     private Long              placeId;
     private Long              installationId;
     private Long              boxId;
     private Integer           fooNumber;
     private Date              dismountingDate;
     private Date              accStartDate;
  
     @Column(name = "ACC_ID")
     public Long getAccId() {
         return accId;
     }
  
     public void setAccId(Long mAccId) {
         accId = mAccId;
     }
  
     @Column(name = "PLACE_ID")
     public Long getPlaceId() {
         return placeId;
     }
  
     public void setPlaceId(Long mPlaceId) {
         placeId = mPlaceId;
     }
  
     @Column(name = "INSTALLATION_ID")
     public Long getInstallationId() {
         return installationId;
     }
  
     public void setInstallationId(Long mInstallationId) {
         installationId = mInstallationId;
     }
  
     @Column(name = "DISM_DATE")
     public Date getDismountingDate() {
         return dismountingDate;
     }
  
     public void setDismountingDate(Date mDismountingDate) {
         dismountingDate = mDismountingDate;
     }
  
     @Column(name = "BOX_ID")
     public Long getBoxId() {
         return boxId;
     }
  
     public void setBoxId(Long mBoxId) {
         boxId = mBoxId;
     }
  
     @Column(name = "FOO_ID")
     public Integer getFooNumber() {
         return fooNumber;
     }
  
     public void setFooNumber(Integer mFooNumber) {
         fooNumber = mFooNumber;
     }
  
     @Column(name = "ACC_START_DATE")
     public Date getAccStartDate() {
         return accStartDate;
     }
  
     public void setAccStartDate(Date mAccStartDate) {
         accStartDate = mAccStartDate;
     }
  
  }
  </code></pre>
  
  <p>Thanks for any ideas!</p>