HQL SELECT多个表直接进入类

时间:2018-04-20 07:26:31

标签: hibernate hql jpql hibernate3

所以我试图将2个表的所有列放入我的自定义创建的类中而不循环到数组中。但我不能让它发挥作用。

这是我的课程:

SampleForm:

public class SampleForm {
    List<Tbaudittrail> audittrail;
    List<Tbauditevents> auditevents;

    public SampleForm(List<Tbaudittrail> audittrail, List<Tbauditevents> auditevents) {
        super();
        this.audittrail = audittrail;
        this.auditevents = auditevents;
    }
    public List<Tbaudittrail> getAudittrail() {
        return audittrail;
    }
    public void setAudittrail(List<Tbaudittrail> audittrail) {
        this.audittrail = audittrail;
    }
    public List<Tbauditevents> getAuditevents() {
        return auditevents;
    }
    public void setAuditevents(List<Tbauditevents> auditevents) {
        this.auditevents = auditevents;
    }

Tbaudittrail:

public class Tbaudittrail {

    private Integer audittrailid;
    private String eventname;
    private String eventdescription;
    private String username;
    private Date eventdatetime;

    public Tbaudittrail(Integer audittrailid, String eventname, String eventdescription, String username,
            Date eventdatetime) {
        super();
        this.audittrailid = audittrailid;
        this.eventname = eventname;
        this.eventdescription = eventdescription;
        this.username = username;
        this.eventdatetime = eventdatetime;
    }
    public Tbaudittrail() {
    }
    public Integer getAudittrailid() {
        return audittrailid;
    }

    public void setAudittrailid(Integer audittrailid) {
        this.audittrailid = audittrailid;
    }

    public String getEventname() {
        return eventname;
    }

    public void setEventname(String eventname) {
        this.eventname = eventname;
    }

    public String getEventdescription() {
        return eventdescription;
    }

    public void setEventdescription(String eventdescription) {
        this.eventdescription = eventdescription;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public Date getEventdatetime() {
        return eventdatetime;
    }

    public void setEventdatetime(Date eventdatetime) {
        this.eventdatetime = eventdatetime;
    }

Tbauditevents:

public class Tbauditevents {

    private Integer eventid;
    private String eventname;

    public Tbauditevents(Integer eventid, String eventname) {
        super();
        this.eventid = eventid;
        this.eventname = eventname;
    }

    public Tbauditevents() {
    }

    public Integer getEventid() {
        return eventid;
    }

    public void setEventid(Integer eventid) {
        this.eventid = eventid;
    }

    public String getEventname() {
        return eventname;
    }

    public void setEventname(String eventname) {
        this.eventname = eventname;
    }

使用SELECT new

Query query = session.createQuery("SELECT new com.project.audit.forms.SampleForm(audittrail,auditevents) FROM Tbaudittrail audittrail,Tbauditevents auditevents WHERE auditevents.eventid=audittrail.audittrailid");
List<SampleForm> customList = query.list();

我收到了这个错误:

org.hibernate.PropertyNotFoundException: no appropriate constructor in class: com.project.audit.forms.SampleForm]

我也尝试过使用ResultTransformer,但无法使用。

映射Tbaudittrail和Tbauditevents,而SampleForm则不映射。我正在使用MS SQL SERVER。

1 个答案:

答案 0 :(得分:0)

您的问题是SampleForm构造函数。您不会在每个参数中收到List,而只会收到一个实体。

所以,正确的构造函数是:

public SampleForm (Tbaudittrail audittrail, Tbauditevents auditevents) {
    super();
    this.audittrail = audittrail;
    this.auditevents = auditevents;
}