Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPA。如果有两条类似的记录被Java解释为相同(唯一)的,如何从DB中只选择一行_Java_Hibernate_Jpa_Unique_Embeddable - Fatal编程技术网

Java JPA。如果有两条类似的记录被Java解释为相同(唯一)的,如何从DB中只选择一行

Java JPA。如果有两条类似的记录被Java解释为相同(唯一)的,如何从DB中只选择一行,java,hibernate,jpa,unique,embeddable,Java,Hibernate,Jpa,Unique,Embeddable,我有这样的情况:我在Hibernate中使用JPA,我想从DB中获取一些数据。 不幸的是,我有两组不同的唯一约束。在我的数据库表中有一个,在我的java实体中有另一个稍有不同。我不能在DB中更改一个,因为我没有访问权限,也不能在java实体中更改一个,因为它可能会在应用程序的其他部分生成错误 在Oracle数据库中,我有表Foo。我有两个相似的记录: 我的Java实体根据参数决定记录是否唯一: 列表项 会计识别号 地点识别码 安装ID 解雇日期 箱号 FOO_ID 附件开始日期 所以,在我基于

我有这样的情况:我在Hibernate中使用JPA,我想从DB中获取一些数据。 不幸的是,我有两组不同的唯一约束。在我的数据库表中有一个,在我的java实体中有另一个稍有不同。我不能在DB中更改一个,因为我没有访问权限,也不能在java实体中更改一个,因为它可能会在应用程序的其他部分生成错误

在Oracle数据库中,我有表Foo。我有两个相似的记录:

我的Java实体根据参数决定记录是否唯一:

  • 列表项
  • 会计识别号
  • 地点识别码
  • 安装ID
  • 解雇日期
  • 箱号
  • FOO_ID
  • 附件开始日期
  • 所以,在我基于Foo实体执行hibernate选择查询之后,我将从数据库中只获得一条记录(因为java认为来自db的两个Foo是相同的)

    这没关系(我喜欢一个结果),但是我能不能指定这两条记录中的哪一条是我最后得到的?假设在本例中,我希望得到一个活动_标志为“Y”(不是表中的第一个,不是随机的,也不是最新的)。这可能吗

    现在,我使用@Embeddeble和@IdClass注释在Java代码中设置唯一的参数:

    Foo.java类:

    @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;
       }
    
    }
    
    @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;
       }
    
    }
    
    FooPK.java类:

    @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;
       }
    
    }
    
    @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;
       }
    
    }
    

    谢谢你的建议

    你真的不应该为一个实体定义一个不唯一的ID

    JPA是数据库和对象之间一个非常固执己见的接口,您要么遵循它,要么选择另一种映射/查询技术

    更新、删除、插入具有非唯一ID的记录会产生不可预知的结果

    如果
    ACTIVITY\u标志
    是唯一键的一部分,只需将其添加到
    @ID
    s即可

    如果无法将该字段添加为@ID,请将ID自动递增字段添加为表的主键

    如果这两种方法都不可行,那么在处理该表时最好使用本机SQL


    我看到您有一个
    ROW\u ID
    列作为第一列,如果这是您的主键,只需将其设置为实体的@ID。

    如果不将该信息添加到查询中,您需要更改实体。除非您愿意编写直接SQL查询并绕过JPA。您是否尝试过编写命名查询?