我正在尝试将此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及其关系:

DebitantMod:

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

....
}


DebitMod:

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

...
}


DebitantComplementMod:

@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

10-04 12:46