Hibernate生成语法错误的SQL

时间:2016-04-04 07:44:05

标签: sql hibernate syntax-error criteria

我有以下实体:

@Entity
@Table(name = "follow_ups")
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
@Access(AccessType.FIELD)
public class FollowUp extends EntityObject {

    private static final long serialVersionUID = -1343114665843716990L;

    @ManyToOne(optional = false)
    @JoinColumn(name = "contact_id")
    private Contact contact;

    @ManyToOne(optional = true)
    @JoinColumn(name = "dossier_id")
    private Dossier dossier;

    @ManyToOne(optional = true)
    @JoinColumn(name = "website_item_id")
    private WebsiteItem websiteItem;

    @ManyToOne(optional = true, fetch = FetchType.LAZY)
    @JoinColumn(name = "invoice_id")
    private Invoice invoice;

    @Column(name = "message_date")
    @OrderBy("message_date DESC")
    private Date messageDate;

    @Column(name = "message")
    private String message;

    @ManyToOne
    @JoinColumn(name = "creator")
    private BackendUser creator;

    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }

    public Dossier getDossier() {
        return dossier;
    }

    public void setDossier(Dossier dossier) {
        this.dossier = dossier;
    }

    public WebsiteItem getWebsiteItem() {
        return websiteItem;
    }

    public void setWebsiteItem(WebsiteItem websiteItem) {
        this.websiteItem = websiteItem;
    }

    public Invoice getInvoice() {
        return invoice;
    }

    public void setInvoice(Invoice invoice) {
        this.invoice = invoice;
    }

    public BackendUser getCreator() {
        return creator;
    }

    public void setCreator(BackendUser creator) {
        this.creator = creator;
    }

    public Date getMessageDate() {
        return messageDate;
    }

    public void setMessageDate(Date messageDate) {
        this.messageDate = messageDate;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

使用检查发票上的任何内容的条件,例如invoice.id,或者急切地提取发票时,会创建以下sql(这只是它执行的许多查询之一):

SELECT vatdetails0_.invoice_id  AS invoice_5_40_1_,
  vatdetails0_.id               AS id1_39_1_,
  vatdetails0_.id               AS id1_39_0_,
  vatdetails0_.invoice_id       AS invoice_5_39_0_,
  vatdetails0_.invoice_vat_type AS invoice_2_39_0_,
  vatdetails0_.vat              AS vat3_39_0_,
  vatdetails0_.vat_amount       AS vat_amou4_39_0_
FROM invoice_vat_details vatdetails0_
WHERE vatdetails0_.invoice_id IN
  (SELECT invoice11_.id
  FROM website_lots l
  WHERE l.website_item_id = websiteite14_.id
  )                       = 0 THEN websiteite14_.end_date ELSE
  (SELECT MAX(l.enddate)
  FROM website_lots l
  WHERE l.website_item_id = websiteite14_.id
  )
END THEN
CASE
  WHEN (SELECT COUNT(*)
    FROM website_lots wl
    INNER JOIN website_lots_auction wla
    ON wl.id               = wla.id
    WHERE wla.status       = 'AVAILABLE'
    AND wl.website_item_id = websiteite14_.id) =
    (SELECT COUNT(*)
    FROM website_lots wl
    INNER JOIN website_lots_auction wla
    ON wl.id                 = wla.id
    WHERE wl.website_item_id = websiteite14_.id
    )
  THEN 5
  WHEN (SELECT COUNT(*)
    FROM website_lots wl
    INNER JOIN website_lots_auction wla
    ON wl.id               = wla.id
    WHERE wla.status       = 'AVAILABLE'
    AND wl.website_item_id = websiteite14_.id) = 0
  THEN 7
  ELSE 6
END WHEN now()              < websiteite14_.start_date THEN 2 WHEN now() >= websiteite14_.start_date THEN 1 ELSE -1
END WHEN websiteite14_.type = 'RENT' THEN 1 ELSE
CASE
  WHEN (SELECT COUNT(*)
    FROM website_lots
    INNER JOIN lots
    ON lots.id                         = website_lots.lot_id
    WHERE website_lots.website_item_id = websiteite14_.id) =
    (SELECT COUNT(*)
    FROM website_lots
    INNER JOIN lots
    ON lots.id                       = website_lots.lot_id
    WHERE lots.status                = 'SOLD'
    AND website_lots.website_item_id = websiteite14_.id
    )
  THEN 3
  ELSE 1
END
END AS formula10_12_,
CASE
  WHEN (SELECT COUNT(*)
    FROM website_lots wl
    LEFT JOIN website_lots_sale wls
    ON wl.id=wls.id
    LEFT JOIN website_lots_auction wla
    ON wl.id                 =wla.id
    WHERE wl.website_item_id = websiteite14_.id
    AND (wls.status         <> 'AVAILABLE'
    OR wla.status           <> 'AVAILABLE')) = 0
  THEN 'ALL'
  WHEN (SELECT COUNT(*)
    FROM website_lots wl
    LEFT JOIN website_lots_sale wls
    ON wl.id=wls.id
    LEFT JOIN website_lots_auction wla
    ON wl.id                 =wla.id
    WHERE wl.website_item_id = websiteite14_.id
    AND (wls.status          = 'AVAILABLE'
    OR wla.status            = 'AVAILABLE')) = 0
  THEN 'NONE'
  ELSE 'PART'
END AS formula11_12_,
CASE
  WHEN (SELECT COUNT(*)
    FROM website_lots l
    WHERE l.website_item_id = websiteite14_.id) = 0
  THEN websiteite14_.end_date
  ELSE
    (SELECT MAX(l.enddate)
    FROM website_lots l
    WHERE l.website_item_id = websiteite14_.id
    )
END AS formula12_12_,
CASE
  WHEN (SELECT COUNT(*)
    FROM website_lots l
    WHERE l.website_item_id = websiteite14_.id) = 0
  THEN NULL
  ELSE
    (SELECT MAX(l.soldDate)
    FROM website_lots l
    WHERE l.website_item_id = websiteite14_.id
    )
END                AS formula18_12_,
websiteite14_.type AS type1_80_12_
FROM follow_ups this_
INNER JOIN contacts contact2_
ON this_.contact_id=contact2_.id
LEFT OUTER JOIN frontend_users frontendus3_
ON contact2_.id=frontendus3_.contact_id
LEFT OUTER JOIN backend_users backenduse4_
ON this_.creator=backenduse4_.id
LEFT OUTER JOIN dossiers dossier5_
ON this_.dossier_id=dossier5_.id
LEFT OUTER JOIN backend_users backenduse6_
ON dossier5_.applier_backend_user_id=backenduse6_.id
LEFT OUTER JOIN backend_users backenduse7_
ON dossier5_.owner_backend_user_id=backenduse7_.id
LEFT OUTER JOIN commissions commission8_
ON dossier5_.id=commission8_.dossier_id
LEFT OUTER JOIN commission_brackets commission9_
ON commission8_.id=commission9_.commission_id
LEFT OUTER JOIN contacts contact10_
ON commission8_.contact_id=contact10_.id
LEFT OUTER JOIN invoices invoice11_
ON this_.invoice_id=invoice11_.id
LEFT OUTER JOIN invoiceContacts invoicecon12_
ON invoice11_.id=invoicecon12_.invoice_id
LEFT OUTER JOIN contacts contact13_
ON invoicecon12_.contact_id=contact13_.id
LEFT OUTER JOIN website_items websiteite14_
ON this_.website_item_id=websiteite14_.id )

此查询不正确,生成的错误是关于第一个THEN关键字,该关键字无法在该位置使用,而且我以前从未使用过hibernate。在互联网上找不到有关hibernate生成错误查询的信息。

使用的标准是最简单的情况:

session.createCriteria(getEntityClass())

什么可能是错的,我应该看看吗? 如何深入了解休眠以查看问题所在? 也许应该将其记录为hibernate错误?

使用的hibernate版本是:4.3.10.Final 使用的方言:POSTGRESQL。

0 个答案:

没有答案