【问题标题】:How to create HQL query using fields from extended class如何使用扩展类中的字段创建 HQL 查询
【发布时间】:2017-12-04 15:28:50
【问题描述】:

我有这两个类:Locatable 和 Device 以便 Locatable 扩展设备: 可定位:

@Entity
@Table(name = Locatable.TABLE_NAME)
@PrimaryKeyJoinColumn(name = "device_id", referencedColumnName = "id")
open class Locatable: Device() {

@Basic
@Column(name = NAME_COLUMN)
var name: String? = null
 }

设备:

@Entity
@Table(name = Device.TABLE_NAME)
@Inheritance(strategy = InheritanceType.JOINED)
abstract class Device {

 @Id
 @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGen")
 @SequenceGenerator(name = "sequenceGen", sequenceName = "devices_id_seq", allocationSize = 1)
 @Column(name = ID_COLUMN)
 open var id: Long = 0

 @Basic
 @Column(name = GROUP_COLUMN)
 open var groupId: Long = 0
 }

如您所见,groupId 字段仅存在于设备中 现在,我想做这个 repo:

  interface LocatableRepo : JpaRepository<Locatable, Long> {
   @Query("SELECT l FROM Locatable l  WHERE l.groupId IN ?1")
   fun getByGroupIdIn(ids: List<Long>): List<Locatable>

   }

我的问题是,如果我做对了,还是应该使用 left join fetch? 因为 groupId 不在 Locatable 中,这意味着它不在 l 那么我该如何使用 l.groupId?(那是因为扩展到 Device 吗?)

谢谢

【问题讨论】:

  • 有人有答案吗?

标签: java sql spring kotlin hql


【解决方案1】:

您的Locatable 类正在扩展Device,因此也扩展了超类的列。您的查询应该是正确的。

我在我的 Java 代码中经常使用这样的查询,到目前为止没有任何问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 2012-02-19
    • 2013-02-21
    • 2021-07-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-21
    相关资源
    最近更新 更多