HQL:多个不同联接中的无效路径

时间:2017-02-06 09:25:31

标签: java hibernate hql

我试图将此SQL查询转换为HQL:

guiConfig

我尝试的HQL代码是:

JSON.parse(JSON.stringify(guiConfig[buzzword]))

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

在这里,所有DAO与他们的关系:

DebitantMod:

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

DebitMod:

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

DebitSaciMod:

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

....
}

RushServiceMod:

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

...
}

BulletinCorrectionMod:

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

...
}

DebitantComplementMod:

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

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

...
}

CommuneMod:

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

...
}

1 个答案:

答案 0 :(得分:0)

问题是语法无效。在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