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.
In Oracle Database I have table Foo. Inside I have two similar records:
My Java Entity decides if record is unique base on parameters:
- List item
- ACC_ID
- PLACE_ID
- INSTALLATION_ID
- DISM_DATE
- BOX_ID
- FOO_ID
- ACC_START_DATE
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)
It's OK (I prefer one result), but can I somehow specify which one of these two records 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?
Right now I use @Embeddable and @IdClass annotations to set unique parameters in Java code:
Foo.java class:
@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;
}
}
FooPK.java class:
@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;
}
}
Thanks for any ideas!