HQL org.hibernate.QueryException:无法解析属性

时间:2017-02-17 09:58:00

标签: hibernate hql

我想编写简单的非存在where子句HQL查询。我不熟悉Hibernate,我遇到了一些小问题。

我的查询如下:

from Day as d 
where d.IDFest = 27 
and not exists (
  from tickets  t
  where t.IDUser = 1
  and t.IDFest = 27
) 

我想选择包含IDUser当天购买的门票的所有日子。所有这些都与节日的ID相关联。

我的堆栈跟踪:

org.hibernate.QueryException: could not resolve property: IDFest of: entities.Day [from entities.Day as d  where d.IDFest = 27  and not exists (    from tickets  t     where t.IDUser = 1  and t.IDFest = 27 )  ]  
at org.hibernate.QueryException.generateQueryException(QueryException.java:137)     
at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:120)    
at org.hibernate.hql.internal.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:247)     
at org.hibernate.hql.internal.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:209)     
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:126)    
at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:88)     
at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:190)   
at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:301)     
at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:236)     
at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1796) 
Caused by: org.hibernate.QueryException: could not resolve property: IDFest of: entities.Day    
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:83)    
at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:77)   
at org.hibernate.persister.entity.AbstractEntityPersister.toType(AbstractEntityPersister.java:1978)     
at org.hibernate.hql.internal.classic.PathExpressionParser.getPropertyType(PathExpressionParser.java:291)   
at org.hibernate.hql.internal.classic.PathExpressionParser.end(PathExpressionParser.java:334)   
at org.hibernate.hql.internal.classic.WhereParser.doPathExpression(WhereParser.java:371)    
at org.hibernate.hql.internal.classic.WhereParser.doToken(WhereParser.java:404)     
at org.hibernate.hql.internal.classic.WhereParser.token(WhereParser.java:285)   
at org.hibernate.hql.internal.classic.ClauseParser.token(ClauseParser.java:107)     
at org.hibernate.hql.internal.classic.PreprocessingParser.token(PreprocessingParser.java:131)   
at org.hibernate.hql.internal.classic.ParserHelper.parse(ParserHelper.java:61)  
at org.hibernate.hql.internal.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:239)     ... 9 more

映射类:

Day.java

package entities;
// Generated Feb 17, 2017 1:18:27 AM by Hibernate Tools 4.3.1

/**
*   Day generated by hbm2java
*/
@Entity
@Table(name = "day", catalog = "pia"
)
public class Day implements java.io.Serializable {

private Integer idday;
private Festivals festivals;
private Date date;
private int totalTickets;
private int leftTickets;

public Day() {
}

@Id
@GeneratedValue(strategy = IDENTITY)

@Column(name = "IDDay", unique = true, nullable = false)
public Integer getIdday() {
    return this.idday;
}

public void setIdday(Integer idday) {
    this.idday = idday;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "IDFest", nullable = false)
public Festivals getFestivals() {
    return this.festivals;
}

public void setFestivals(Festivals festivals) {
    this.festivals = festivals;
}

@Temporal(TemporalType.DATE)
@Column(name = "date", nullable = false, length = 10)
public Date getDate() {
    return this.date;
}

public void setDate(Date date) {
    this.date = date;
}

@Column(name = "totalTickets", nullable = false)
public int getTotalTickets() {
    return this.totalTickets;
}

public void setTotalTickets(int totalTickets) {
    this.totalTickets = totalTickets;
}

@Column(name = "leftTickets", nullable = false)
public int getLeftTickets() {
    return this.leftTickets;
}

public void setLeftTickets(int leftTickets) {
    this.leftTickets = leftTickets;
}

}

Festivals.java

package entities;
// Generated Feb 17, 2017 1:18:27 AM by Hibernate Tools 4.3.1

/**
 * Festivals generated by hbm2java
*/
@Entity
@Table(name = "festivals", catalog = "pia"
)
public class Festivals implements java.io.Serializable {

private Integer idfest;
private String name;
private String place;
private Date begin;
private Date end;
private int priceDay;
private int priceFestival;
private String facebook;
private String twitter;
private String instagram;
private String youtube;
private long visited;
private int ticketsPerUser;
private int ticketsPerDay;
private int totalRating;
private int numberRatings;
private Set<Users> userses = new HashSet<Users>(0);
private Set<Messages> messageses = new HashSet<Messages>(0);
private Set<Files> fileses = new HashSet<Files>(0);
private Set<Day> days = new HashSet<Day>(0);
private Set<Performers> performerses = new HashSet<Performers>(0);
private Set<Tickets> ticketses = new HashSet<Tickets>(0);
private Set<Comments> commentses = new HashSet<Comments>(0);

public Festivals() {
}

@Id
@GeneratedValue(strategy = IDENTITY)

@Column(name = "IDFest", unique = true, nullable = false)
public Integer getIdfest() {
    return this.idfest;
}

public void setIdfest(Integer idfest) {
    this.idfest = idfest;
}

@Column(name = "name", nullable = false, length = 45)
public String getName() {
    return this.name;
}

public void setName(String name) {
    this.name = name;
}

@Column(name = "place", nullable = false, length = 45)
public String getPlace() {
    return this.place;
}

public void setPlace(String place) {
    this.place = place;
}

@Temporal(TemporalType.DATE)
@Column(name = "begin", nullable = false, length = 10)
public Date getBegin() {
    return this.begin;
}

public void setBegin(Date begin) {
    this.begin = begin;
}

@Temporal(TemporalType.DATE)
@Column(name = "end", nullable = false, length = 10)
public Date getEnd() {
    return this.end;
}

public void setEnd(Date end) {
    this.end = end;
}

@Column(name = "priceDay", nullable = false)
public int getPriceDay() {
    return this.priceDay;
}

public void setPriceDay(int priceDay) {
    this.priceDay = priceDay;
}

@Column(name = "priceFestival", nullable = false)
public int getPriceFestival() {
    return this.priceFestival;
}

public void setPriceFestival(int priceFestival) {
    this.priceFestival = priceFestival;
}

@Column(name = "facebook", length = 100)
public String getFacebook() {
    return this.facebook;
}

public void setFacebook(String facebook) {
    this.facebook = facebook;
}

@Column(name = "twitter", length = 100)
public String getTwitter() {
    return this.twitter;
}

public void setTwitter(String twitter) {
    this.twitter = twitter;
}

@Column(name = "instagram", length = 100)
public String getInstagram() {
    return this.instagram;
}

public void setInstagram(String instagram) {
    this.instagram = instagram;
}

@Column(name = "youtube", length = 100)
public String getYoutube() {
    return this.youtube;
}

public void setYoutube(String youtube) {
    this.youtube = youtube;
}

@Column(name = "visited", nullable = false)
public long getVisited() {
    return this.visited;
}

public void setVisited(long visited) {
    this.visited = visited;
}

@Column(name = "ticketsPerUser", nullable = false)
public int getTicketsPerUser() {
    return this.ticketsPerUser;
}

public void setTicketsPerUser(int ticketsPerUser) {
    this.ticketsPerUser = ticketsPerUser;
}

@Column(name = "ticketsPerDay", nullable = false)
public int getTicketsPerDay() {
    return this.ticketsPerDay;
}

public void setTicketsPerDay(int ticketsPerDay) {
    this.ticketsPerDay = ticketsPerDay;
}

@Column(name = "totalRating", nullable = false)
public int getTotalRating() {
    return this.totalRating;
}

public void setTotalRating(int totalRating) {
    this.totalRating = totalRating;
}

@Column(name = "numberRatings", nullable = false)
public int getNumberRatings() {
    return this.numberRatings;
}

public void setNumberRatings(int numberRatings) {
    this.numberRatings = numberRatings;
}

@ManyToMany(fetch = FetchType.EAGER, mappedBy = "festivalses")
public Set<Users> getUserses() {
    return this.userses;
}

public void setUserses(Set<Users> userses) {
    this.userses = userses;
}

@OneToMany(fetch = FetchType.EAGER, mappedBy = "festivals")
public Set<Messages> getMessageses() {
    return this.messageses;
}

public void setMessageses(Set<Messages> messageses) {
    this.messageses = messageses;
}

@OneToMany(fetch = FetchType.EAGER, mappedBy = "festivals")
public Set<Files> getFileses() {
    return this.fileses;
}

public void setFileses(Set<Files> fileses) {
    this.fileses = fileses;
}

@OneToMany(fetch = FetchType.EAGER, mappedBy = "festivals")
public Set<Day> getDays() {
    return this.days;
}

public void setDays(Set<Day> days) {
    this.days = days;
}

@OneToMany(fetch = FetchType.EAGER, mappedBy = "festivals")
public Set<Performers> getPerformerses() {
    return this.performerses;
}

public void setPerformerses(Set<Performers> performerses) {
    this.performerses = performerses;
}

@OneToMany(fetch = FetchType.EAGER, mappedBy = "festivals")
public Set<Tickets> getTicketses() {
    return this.ticketses;
}

public void setTicketses(Set<Tickets> ticketses) {
    this.ticketses = ticketses;
}

@OneToMany(fetch = FetchType.EAGER, mappedBy = "festivals")
public Set<Comments> getCommentses() {
    return this.commentses;
}

public void setCommentses(Set<Comments> commentses) {
    this.commentses = commentses;
}

}

Tickets.java

package entities;
// Generated Feb 17, 2017 1:18:27 AM by Hibernate Tools 4.3.1

/**
* Tickets generated by hbm2java
*/
@Entity
@Table(name = "tickets", catalog = "pia"
)
public class Tickets implements java.io.Serializable {

private Integer idtic;
private Festivals festivals;
private Users users;
private int isBought;
private int price;
private Date date;

public Tickets() {
}

@Id
@GeneratedValue(strategy = IDENTITY)

@Column(name = "IDTic", unique = true, nullable = false)
public Integer getIdtic() {
    return this.idtic;
}

public void setIdtic(Integer idtic) {
    this.idtic = idtic;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "IDFest", nullable = false)
public Festivals getFestivals() {
    return this.festivals;
}

public void setFestivals(Festivals festivals) {
    this.festivals = festivals;
}

@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "IDUser", nullable = false)
public Users getUsers() {
    return this.users;
}

public void setUsers(Users users) {
    this.users = users;
}

@Column(name = "isBought", nullable = false)
public int getIsBought() {
    return this.isBought;
}

public void setIsBought(int isBought) {
    this.isBought = isBought;
}

@Column(name = "price", nullable = false)
public int getPrice() {
    return this.price;
}

public void setPrice(int price) {
    this.price = price;
}

@Temporal(TemporalType.DATE)
@Column(name = "date", nullable = false, length = 10)
public Date getDate() {
    return this.date;
}

public void setDate(Date date) {
    this.date = date;
}

}

1 个答案:

答案 0 :(得分:0)

你应该在你的查询(而不是sql)中使用hql,比如

where d.festivals.id = 27