簡體   English   中英

JPQL在openjpa中獲取多對多映射的聯接

[英]Fetch join by JPQL in openjpa for many to many mapping

我有一個device和device_group表,通過device_group_mapping表進行映射,如下所示

CREATE TABLE device_group_mapping
(
  device_id character varying(64) NOT NULL,
  device_group_id bigint NOT NULL,
  CONSTRAINT "FK_device_group_mapping_device" FOREIGN KEY (device_id)
      REFERENCES device (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT "FK_device_group_mapping_device_group" FOREIGN KEY (device_group_id)
      REFERENCES device_group (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);

openjpa的device和deviceGroup實體如下

@Entity
@Table(name = "device")
public class Device implements Serializable
{
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "device_group_mapping", joinColumns =
    {@JoinColumn(name = "device_id", referencedColumnName = "id", nullable = false)}, inverseJoinColumns =
    {@JoinColumn(name = "device_group_id", referencedColumnName = "id", nullable = false)})
    private List<DeviceGroup>   deviceGroupCollection;  
}

@Entity
@Table(name = "device_group")
public class DeviceGroup implements Serializable
{
    @ManyToMany(mappedBy = "deviceGroupCollection", fetch = FetchType.EAGER)
    @OrderBy()
    private List<Device>    deviceCollection;
}

由於獲取類型是惰性的,因此我必須獲取deviceGroupCollection如下代碼

@Override
@Transactional
public List<Device> findAllDevicesWithGroupMapping() throws Exception
{
    List<Device> list = new ArrayList<Device>();

    list = this.deviceDao.findAll();

    for (Device device : list)
    {
        device.setDeviceGroupCollection(device.getDeviceGroupCollection());
    }
    return list;
}

但是,當設備列表包含設備數量時,這將非常慢。

我想也許我可以通過JPQL來找到設備實體,並通過提取加入device_group,但不知道該怎么做。 根據openjpa規范,它不支持on子句,也不支持嵌套的訪存聯接。

我目前使用的openjpa如下

    <dependency>
        <groupId>org.apache.openjpa</groupId>
        <artifactId>openjpa-all</artifactId>
        <version>2.2.2</version>
    </dependency>

任何幫助表示贊賞。

您可以像在其他任何關聯上一樣使用訪存聯接ManyToMany。 你不需要任何on化酶,因為關聯映射已經定義了兩個實體如何相互鏈接:

select d from Device d
left join fetch d.deviceGroupCollection
where ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM