gpt4 book ai didi

java - HQL :Invalid Path in Multiple different Joins

转载 作者:太空宇宙 更新时间:2023-11-04 11:48:36 24 4
gpt4 key购买 nike

我正在尝试将此 SQL 查询转换为 HQL:

SELECT
elements of t2
FROM
debitant t2
INNER JOIN debit t3 ON t2.id_debit = t3.id
INNER JOIN l_debit_saci t7 ON t3.id = t7.id_debit AND t7.principal = 1
INNER JOIN rushServices t8 ON t7.code_acs = t8.acs
INNER JOIN bulletin_correction t10 ON t2.id = t10.id_debitant
LEFT OUTER JOIN l_debitant_complement t9 ON t2.adr_info = t9.id
INNER JOIN commune t6 ON t3.adr_cc = t6.id
WHERE
t2.id = @debitant_id AND t10.annee = @PECAnnee
ORDER BY
t3.adr_cp , t2.id

我尝试过的 HQL 代码是:

buffer = new StringBuilder();
buffer.append("SELECT D ");
buffer.append("FROM " + getEntityClassName() + " D ");
buffer.append("INNER JOIN FETCH D.debit DE ");
buffer.append("INNER JOIN FETCH DE.sacis DS ");
buffer.append("INNER JOIN FETCH DS.service RS ");
buffer.append("INNER JOIN BulletinCorrectionMod.debitant BC ");
buffer.append("LEFT OUTER JOIN D.adresseInfo DEC ");
buffer.append("INNER JOIN FETCH DE.adresseCommune CO ");
buffer.append("WHERE DS.principal = 1 ");
buffer.append("AND D.id = :debitantId ");
buffer.append("AND DC.annee = :annee ");
buffer.append("ORDER BY DE.adresseCodePostal, D.id");

hqlQuery = session.createQuery(buffer.toString());
hqlQuery.setInteger("debitantId", debitantId);
hqlQuery.setInteger("annee", year);

此代码返回此错误:无效路径:'BulletinCorrectionMod.debitant'

这里,所有 DAO 及其关系:

借方模式:

@Entity
@Table(name = "debitant")
public class DebitantMod implements Serializable
{
@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO, generator = "sequence_name")
@SequenceGenerator(name = "sequence_name", sequenceName = "debitant_id_seq")
private Integer id = null;

@Column(name = "adr_info", nullable = true)
private Integer adresseInfo = null;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_debit", nullable = false)
private DebitMod debit = null;

....
}

借记模式:

@Entity
@Table(name = "debit")
public class DebitMod implements Serializable
{
@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO, generator = "sequence_name")
@SequenceGenerator(name = "sequence_name", sequenceName = "debit_id_seq")
private Integer id = null;

@OneToMany(mappedBy = "debit", fetch = FetchType.LAZY)
private Set<DebitantMod> debitants = null;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "adr_cc", nullable = true, columnDefinition = "bpchar")
private CommuneMod adresseCommune = null;

...
}

DebitSaciMod:

@Entity
@Table(name = "l_debit_saci")
public class DebitSaciMod implements Serializable
{

@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO, generator = "sequence_name")
@SequenceGenerator(name = "sequence_name", sequenceName = "l_debit_saci_id_seq")
private Integer id = null;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_debit", nullable = false)
private DebitMod debit = null;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "code_acs", nullable = false)
private RushServiceMod service = null;

...
}

RushServiceMod:

@Entity
@Table(name = "rushservices")
public class RushServiceMod implements Serializable
{

@Id
@Column(name = "acs", nullable = false)
private String acs = null;

...
}

BulletinCorrectionMod:

@Entity
@Table(name = "bulletin_correction")
public class BulletinCorrectionMod implements Serializable{

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "id_debitant", nullable = false)
private DebitantMod debitant = null;

@Column(name = "annee", nullable = false)
private Integer annee = null;

...
}

借记补充模式:

@Entity
@Table(name = "l_debitant_complement")
public class DebitantComplementMod implements Serializable{

@Id
@Column(name = "id", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO, generator = "sequence_name")
@SequenceGenerator(name = "sequence_name", sequenceName = "l_debitant_complement_id_seq")
private Integer id = null;

...
}

CommuneMod:

@Entity
@Table(name = "commune")
public class CommuneMod implements Serializable
{

@Id
@Column(name = "id", nullable = false, columnDefinition = "bpchar")
private String id = null;

...
}

最佳答案

问题是语法无效。在 Hibernate 5.1 之前,您只能加入相关实体,并且在您的情况下,DebitantModBulletinCorrectionMod 没有关系。

您必须将两个实体放入 FROM 子句中,并在 WHERE 中添加条件:

`buffer = new StringBuilder();
buffer.append("SELECT D ");
buffer.append("FROM " + getEntityClassName() + " D ");
buffer.append("INNER JOIN FETCH D.debit DE ");
buffer.append("INNER JOIN FETCH DE.sacis DS ");
buffer.append("INNER JOIN FETCH DS.service RS ");
// buffer.append("INNER JOIN BulletinCorrectionMod.debitant BC "); // invalid - remove this line
buffer.append("LEFT OUTER JOIN D.adresseInfo DEC ");
buffer.append("INNER JOIN FETCH DE.adresseCommune CO ");
buffer.append(", BulletinCorrectionMod BC "); // add this line
buffer.append("WHERE DS.principal = 1 ");
buffer.append("AND D.id = :debitantId ");
buffer.append("AND DC.annee = :annee ");
buffer.append("AND BC.debitant = D "); // and this one
buffer.append("ORDER BY DE.adresseCodePostal, D.id");

由于BulletinCorrectionModDebitantMod有关系,编写查询的另一种方法是:

buffer = new StringBuilder();
buffer.append("SELECT D ");
buffer.append("FROM BulletinCorrectionMod BC ");
buffer.append("INNER JOIN BC.debitant D ");
buffer.append("INNER JOIN FETCH D.debit DE ");
buffer.append("INNER JOIN FETCH DE.sacis DS ");
buffer.append("INNER JOIN FETCH DS.service RS ");
buffer.append("LEFT OUTER JOIN D.adresseInfo DEC ");
buffer.append("INNER JOIN FETCH DE.adresseCommune CO ");
buffer.append("WHERE DS.principal = 1 ");
buffer.append("AND D.id = :debitantId ");
buffer.append("AND DC.annee = :annee ");
buffer.append("ORDER BY DE.adresseCodePostal, D.id");

如果您使用的是 Hibernate 5.1 或更高版本,您可以加入不相关的实体,请查看 this

关于java - HQL :Invalid Path in Multiple different Joins,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42064263/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com