在一对一映射列中获取除null之外的其他内容?

时间:2014-03-11 13:21:02

标签: hibernate scala jpa

我在表中有一些历史数据,这些数据链接到另外两个具有一对一关系的表。除此之外,由于它的历史,其他两个表中的条目并不总是存在。使用NotFoundAction.IGNORE意味着如果引用的表中没有适当的记录,我会为mediaInfo和/或fileInfo获取NULL。

所以现在我需要能够执行order by fileInfo.fileName,结果集只包含引用fileInfo不为空的那些行。是否有一种方法可以使结果集包含MigrationHistory的所有行,即使fileInfo通常为null(通过让它返回默认值而不是null)?或者有必要做两个选择,一个获取fileInfo不为null的所有行,然后是第二个获取那些为空的行?

在这种情况下,查询是一个简单的Hibernate from MigrationHistory order by fileInfo.fileName asc

@Entity
@Table(name = "MigrationHistory")
class MigrationHistory extends Serializable with TableObject with MigrationKeys {
  @Id
  @Column(name = "FileLocationID", nullable = false, insertable = true, updatable = true, length = 19, precision = 0)
  @BeanProperty var fileLocationID: Long = _

  @Id
  @Column(name = "MediaInfoID", nullable = false, insertable = true, updatable = true, length = 19, precision = 0)
  @BeanProperty var mediaInfoID: Long = _

  @Column(name = "priority", nullable = false, insertable = true, updatable = true, length = 5, precision = 0)
  @BeanProperty var priority: Short = _

  @Column(name = "isNewPrimary", nullable = false, insertable = true, updatable = true, length = 1, precision = 0)
  @BeanProperty var isNewPrimary: Boolean = _

  @Column(name = "copyOrMove", nullable = false, insertable = true, updatable = true, length = 1, precision = 0)
  @BeanProperty var copyOrMove: String = _

  @Column(name = "migrationStatus", nullable = false, insertable = true, updatable = true, length = 15, precision = 0)
  @BeanProperty var migrationStatus: String = _

  @Column(name = "insertDate", nullable = false, insertable = true, updatable = true, length = 23, precision = 3)
  @BeanProperty var insertDate: Date = _

  @Column(name = "comment", nullable = true, insertable = true, updatable = true, length = 1000, precision = 0)
  @BeanProperty var comment: String = _

  @OneToOne
  @JoinColumn(name = "MediaInfoID", referencedColumnName = "MediaInfoID", insertable = false, updatable = false, nullable = false)
  var mediaInfo: MediaInfo = _

  @OneToOne(optional = true)
  @JoinColumn(name = "FileLocationID", referencedColumnName = "FileInfoID", insertable = false, updatable = false, nullable = true)
  @NotFound(action = NotFoundAction.IGNORE)
  var fileInfo: FileInfo = _
}

@Entity
@Table(name="FileInfo")
class FileInfo extends Serializable {
  @GeneratedValue
  @Column(name = "FileInfoID", nullable = false, insertable = false, updatable = false, length = 19, precision = 0)
  @Id
  @BeanProperty var id: Long = _

  @Column(name = "fileName", nullable = false, insertable = true, updatable = true, length = 30, precision = 0)
  @BeanProperty var fileName: String = _

  @Column(name = "fileSize", nullable = false, insertable = true, updatable = true, length = 7, precision = 0)
  @BeanProperty var fileSize: Float = _
}

1 个答案:

答案 0 :(得分:1)

克里斯是对的。我需要的是左外连接。以下是有效的查询:

from edu.stsci.aofl.dads.MigrationHistory as mh left outer join mh.fileInfo fi left outer join mh.mediaInfo mi order by fi.fileName asc

关于这个问题的一个有趣的事情,我猜测它是由三个" as"引起的。子句,它不是生成由一堆MigrationHistory对象组成的结果集,而是生成一个由三个元素数组组成的结果集。第一个元素是MigrationHistory对象,其余元素为null。

这个查询的另一个有趣的事情是大多数DBMS将null视为可能的最低值。某些DBMS(例如Derby)提供了使它们具有最高可能值的选项(NULLS FIRST与NULLS LAST相比)。 MS SQL Server没有。