Java org.hibernate.QueryException:无法解析属性:MetadataForHibernate of:bookshare.entity.hasbooks.hasbooks

Java org.hibernate.QueryException:无法解析属性:MetadataForHibernate of:bookshare.entity.hasbooks.hasbooks,java,hibernate,Java,Hibernate,这是我第一次使用Hibernate,我想在Hibernate中进行以下简单查询: 我尝试了所有方法,但每次都得到相同的错误输出: org.hibernate.QueryException:无法解析属性:MetadataForHibernate of:bookshare.entity.hasbooks.hasbooks[从 我制作的功能: @SuppressWarnings("unchecked") public List<MetadataForHibernate> getBooksB

这是我第一次使用Hibernate,我想在Hibernate中进行以下简单查询:

我尝试了所有方法,但每次都得到相同的错误输出:
org.hibernate.QueryException:无法解析属性:MetadataForHibernate of:bookshare.entity.hasbooks.hasbooks[从

我制作的功能:

@SuppressWarnings("unchecked")
public List<MetadataForHibernate> getBooksByTitle(int userID, String Title) {
    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();
    Session session = factory.openSession();
    Transaction tx = session.beginTransaction();


    Query query = session.createQuery(
            "SELECT H.MetadataForHibernate FROM HasBooks as H WHERE H.users.id = :userid AND LOWER(H.MetadataForHibernate.title) LIKE LOWER(:title) ORDER BY B.title ASC ");
    query.setParameter("userid", userID);
    query.setParameter("title", "%" + Title + "%");

    List<MetadataForHibernate> books = (List<MetadataForHibernate>) query.list();
    tx.rollback();
    session.close();
    factory.close();
    return books;
}
@Entity
@Table(name="tblbooks")
public class MetadataForHibernate {


@Id
@Column(name = "bookshareId")
private int bookshareId;

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

@Column(name = "availableToDownload")
private int availableToDownload;

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

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

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

@Column(name = "contentId")
private int contentId;

@Column(name = "copyright")
private Date copyright;

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

@Column(name="dtbookSize")
private int dtbookSize;

@Column(name = "freelyAvailable")
private int freelyAvailable;

@Column(name = "brf")
private int brf;

@Column(name = "daisy")
private int daisy;

@Column(name = "images")
private int images;

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

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

@Column(name = "publishDate")
private Date publishDate;

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

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

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

@OneToMany(mappedBy="book")
private List<HasBooks> hasBooks;

public MetadataForHibernate(){
    hasBooks = new ArrayList<HasBooks>();
}


//Getters & Setters

public List<HasBooks> getHasBooks() {
    return hasBooks;
}

public void setHasBooks(List<HasBooks> hasBooks) {
    this.hasBooks = hasBooks;
}

public int getFreelyAvailable ()
{
    return freelyAvailable;
}

public void setFreelyAvailable (String freelyAvailable)
{
    this.freelyAvailable = Integer.parseInt(freelyAvailable);
}

public String getCompleteSynopsis ()
{
    return completeSynopsis;
}

public void setCompleteSynopsis (String completeSynopsis)
{
    this.completeSynopsis = completeSynopsis;
}

public int getDaisy ()
{
    return daisy;
}

public void setDaisy (String daisy)
{
    this.daisy = Integer.parseInt(daisy);
}

public Date getCopyright ()
{
    return copyright;
}

public void setCopyright (Date copyright)
{
    this.copyright = copyright;
}

public int getAvailableToDownload ()
{
    return availableToDownload;
}

public void setAvailableToDownload (String availableToDownload)
{
    this.availableToDownload = Integer.parseInt(availableToDownload);
}

public int getContentId ()
{
    return contentId;
}

public void setContentId (String contentId)
{
    this.contentId = Integer.parseInt(contentId);
}

public String getPublisher ()
{
    return publisher;
}

public void setPublisher (String publisher)
{
    this.publisher = publisher;
}

public int getBookshareId ()
{
    return bookshareId;
}

public void setBookshareId (String bookshareId)
{
    this.bookshareId = Integer.parseInt(bookshareId);
}

public String getAuthor ()
{
    return author;
}

public void setAuthor (String author)
{
    this.author = author;
}

public String getTitle ()
{
    return title;
}

public void setTitle (String title)
{
    this.title = title;
}

public String getCategory ()
{
    return category;
}

public void setCategory (String category)
{
    this.category = category;
}

public String getQuality ()
{
    return quality;
}

public void setQuality (String quality)
{
    this.quality = quality;
}

public String getIsbn13 ()
{
    return isbn13;
}

public void setIsbn13 (String isbn13)
{
    this.isbn13 = isbn13;
}

public int getImages ()
{
    return images;
}

public void setImages (String images)
{
    this.images = Integer.parseInt(images);
}

public String getLanguage ()
{
    return language;
}

public void setLanguage (String language)
{
    this.language = language;
}

public String getBriefSynopsis ()
{
    return briefSynopsis;
}

public void setBriefSynopsis (String briefSynopsis)
{
    this.briefSynopsis = briefSynopsis;
}

public int getDtbookSize ()
{
    return dtbookSize;
}

public void setDtbookSize (int dtbookSize)
{
    this.dtbookSize = dtbookSize;
}

public int getBrf ()
{
    return brf;
}

public void setBrf (String brf)
{
    this.brf = Integer.parseInt(brf);
}

public Date getPublishDate ()
{
    return publishDate;
}

public void setPublishDate (Date publishDate)
{
    this.publishDate = publishDate;
}

public String getDownloadFormat ()
{
    return downloadFormat;
}

public void setDownloadFormat (String downloadFormat)
{
    this.downloadFormat = downloadFormat;
}

@Override
public String toString()
{
    return "ClassPojo [freelyAvailable = "+freelyAvailable+", completeSynopsis = "+completeSynopsis+", daisy = "+daisy+", copyright = "+copyright+", availableToDownload = "+availableToDownload+", contentId = "+contentId+", publisher = "+publisher+", bookshareId = "+bookshareId+", author = "+author+", title = "+title+", category = "+category+", quality = "+quality+", isbn13 = "+isbn13+", images = "+images+", language = "+language+", briefSynopsis = "+briefSynopsis+", dtbookSize = "+dtbookSize+", brf = "+brf+", publishDate = "+publishDate+", downloadFormat = "+downloadFormat+"]";
}



public void convertDataOf(BookDetail book) throws ParseException{
    DateFormat format;
    Date date;


    this.bookshareId=book.getBookshare().getBook().getMetadata().getBookshareId();
    this.author=String.join(",", book.getBookshare().getBook().getMetadata().getAuthor());
    this.availableToDownload=book.getBookshare().getBook().getMetadata().getAvailableToDownload();
    this.briefSynopsis=book.getBookshare().getBook().getMetadata().getBriefSynopsis();
    this.category=String.join(",", book.getBookshare().getBook().getMetadata().getCategory());
    this.completeSynopsis=book.getBookshare().getBook().getMetadata().getCompleteSynopsis();
    this.contentId=book.getBookshare().getBook().getMetadata().getContentId();


    //convert String to date
    format = new SimpleDateFormat("yyyy");
    date = format.parse(book.getBookshare().getBook().getMetadata().getCopyright());
    this.copyright=date;


    this.downloadFormat=String.join(",", book.getBookshare().getBook().getMetadata().getDownloadFormat());
    this.dtbookSize=book.getBookshare().getBook().getMetadata().getDtbookSize();
    this.freelyAvailable=book.getBookshare().getBook().getMetadata().getFreelyAvailable();
    this.brf=book.getBookshare().getBook().getMetadata().getBrf();
    this.daisy=book.getBookshare().getBook().getMetadata().getDaisy();
    this.images=book.getBookshare().getBook().getMetadata().getImages();
    this.isbn13=book.getBookshare().getBook().getMetadata().getIsbn13();
    this.language=String.join(",", book.getBookshare().getBook().getMetadata().getLanguage());

    //convert String to date
    format = new SimpleDateFormat("MMddyyyy");
    date = format.parse(book.getBookshare().getBook().getMetadata().getPublishDate());
    this.publishDate=date;


    this.publisher=book.getBookshare().getBook().getMetadata().getPublisher();
    this.quality=book.getBookshare().getBook().getMetadata().getQuality();
    this.title=book.getBookshare().getBook().getMetadata().getTitle();

  }   
}
@Entity
@Table(name = "tblhasbooks")
public class HasBooks implements Serializable {

    //@Column(name = "Id",unique = true,nullable = false)

    @Id
    @GeneratedValue()
    private int hasBooksId;

    @ManyToOne(cascade = CascadeType.ALL)
    private Users user;

    @ManyToOne(cascade = CascadeType.ALL)
    private MetadataForHibernate book;




    public MetadataForHibernate getBook() {
        return book;
    }

    public Users getUser() {
        return user;
    }

    public int getHasBooksId() {
        return hasBooksId;
    }

    public void setHasBooksId(int hasBooksId) {
        this.hasBooksId = hasBooksId;
    }

    public void setUser(Users user) {
        this.user = user;
    }

    public void setBook(MetadataForHibernate book) {
        this.book = book;
    }

}
@Entity
@Table(name="tblusers")
public class Users implements Serializable{



public Users(){hasBooks = new ArrayList<HasBooks>();
}


@Id
@Column(name = "Id",unique = true,nullable = false)
@GeneratedValue(strategy=GenerationType.AUTO)
private int Id;

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

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

@OneToMany(mappedBy="user")
private List<HasBooks> hasBooks;


//Getters & Setters

public List<HasBooks> getHasBooks() {
    return hasBooks;
}

public void setHasBooks(List<HasBooks> hasBooks) {
    this.hasBooks = hasBooks;
}

public int getId() {
    return Id;
}

public void setUser_id(int Id) {
    this.Id = Id;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}


}
用户:

@SuppressWarnings("unchecked")
public List<MetadataForHibernate> getBooksByTitle(int userID, String Title) {
    Configuration cfg = new Configuration();
    cfg.configure("hibernate.cfg.xml");
    SessionFactory factory = cfg.buildSessionFactory();
    Session session = factory.openSession();
    Transaction tx = session.beginTransaction();


    Query query = session.createQuery(
            "SELECT H.MetadataForHibernate FROM HasBooks as H WHERE H.users.id = :userid AND LOWER(H.MetadataForHibernate.title) LIKE LOWER(:title) ORDER BY B.title ASC ");
    query.setParameter("userid", userID);
    query.setParameter("title", "%" + Title + "%");

    List<MetadataForHibernate> books = (List<MetadataForHibernate>) query.list();
    tx.rollback();
    session.close();
    factory.close();
    return books;
}
@Entity
@Table(name="tblbooks")
public class MetadataForHibernate {


@Id
@Column(name = "bookshareId")
private int bookshareId;

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

@Column(name = "availableToDownload")
private int availableToDownload;

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

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

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

@Column(name = "contentId")
private int contentId;

@Column(name = "copyright")
private Date copyright;

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

@Column(name="dtbookSize")
private int dtbookSize;

@Column(name = "freelyAvailable")
private int freelyAvailable;

@Column(name = "brf")
private int brf;

@Column(name = "daisy")
private int daisy;

@Column(name = "images")
private int images;

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

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

@Column(name = "publishDate")
private Date publishDate;

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

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

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

@OneToMany(mappedBy="book")
private List<HasBooks> hasBooks;

public MetadataForHibernate(){
    hasBooks = new ArrayList<HasBooks>();
}


//Getters & Setters

public List<HasBooks> getHasBooks() {
    return hasBooks;
}

public void setHasBooks(List<HasBooks> hasBooks) {
    this.hasBooks = hasBooks;
}

public int getFreelyAvailable ()
{
    return freelyAvailable;
}

public void setFreelyAvailable (String freelyAvailable)
{
    this.freelyAvailable = Integer.parseInt(freelyAvailable);
}

public String getCompleteSynopsis ()
{
    return completeSynopsis;
}

public void setCompleteSynopsis (String completeSynopsis)
{
    this.completeSynopsis = completeSynopsis;
}

public int getDaisy ()
{
    return daisy;
}

public void setDaisy (String daisy)
{
    this.daisy = Integer.parseInt(daisy);
}

public Date getCopyright ()
{
    return copyright;
}

public void setCopyright (Date copyright)
{
    this.copyright = copyright;
}

public int getAvailableToDownload ()
{
    return availableToDownload;
}

public void setAvailableToDownload (String availableToDownload)
{
    this.availableToDownload = Integer.parseInt(availableToDownload);
}

public int getContentId ()
{
    return contentId;
}

public void setContentId (String contentId)
{
    this.contentId = Integer.parseInt(contentId);
}

public String getPublisher ()
{
    return publisher;
}

public void setPublisher (String publisher)
{
    this.publisher = publisher;
}

public int getBookshareId ()
{
    return bookshareId;
}

public void setBookshareId (String bookshareId)
{
    this.bookshareId = Integer.parseInt(bookshareId);
}

public String getAuthor ()
{
    return author;
}

public void setAuthor (String author)
{
    this.author = author;
}

public String getTitle ()
{
    return title;
}

public void setTitle (String title)
{
    this.title = title;
}

public String getCategory ()
{
    return category;
}

public void setCategory (String category)
{
    this.category = category;
}

public String getQuality ()
{
    return quality;
}

public void setQuality (String quality)
{
    this.quality = quality;
}

public String getIsbn13 ()
{
    return isbn13;
}

public void setIsbn13 (String isbn13)
{
    this.isbn13 = isbn13;
}

public int getImages ()
{
    return images;
}

public void setImages (String images)
{
    this.images = Integer.parseInt(images);
}

public String getLanguage ()
{
    return language;
}

public void setLanguage (String language)
{
    this.language = language;
}

public String getBriefSynopsis ()
{
    return briefSynopsis;
}

public void setBriefSynopsis (String briefSynopsis)
{
    this.briefSynopsis = briefSynopsis;
}

public int getDtbookSize ()
{
    return dtbookSize;
}

public void setDtbookSize (int dtbookSize)
{
    this.dtbookSize = dtbookSize;
}

public int getBrf ()
{
    return brf;
}

public void setBrf (String brf)
{
    this.brf = Integer.parseInt(brf);
}

public Date getPublishDate ()
{
    return publishDate;
}

public void setPublishDate (Date publishDate)
{
    this.publishDate = publishDate;
}

public String getDownloadFormat ()
{
    return downloadFormat;
}

public void setDownloadFormat (String downloadFormat)
{
    this.downloadFormat = downloadFormat;
}

@Override
public String toString()
{
    return "ClassPojo [freelyAvailable = "+freelyAvailable+", completeSynopsis = "+completeSynopsis+", daisy = "+daisy+", copyright = "+copyright+", availableToDownload = "+availableToDownload+", contentId = "+contentId+", publisher = "+publisher+", bookshareId = "+bookshareId+", author = "+author+", title = "+title+", category = "+category+", quality = "+quality+", isbn13 = "+isbn13+", images = "+images+", language = "+language+", briefSynopsis = "+briefSynopsis+", dtbookSize = "+dtbookSize+", brf = "+brf+", publishDate = "+publishDate+", downloadFormat = "+downloadFormat+"]";
}



public void convertDataOf(BookDetail book) throws ParseException{
    DateFormat format;
    Date date;


    this.bookshareId=book.getBookshare().getBook().getMetadata().getBookshareId();
    this.author=String.join(",", book.getBookshare().getBook().getMetadata().getAuthor());
    this.availableToDownload=book.getBookshare().getBook().getMetadata().getAvailableToDownload();
    this.briefSynopsis=book.getBookshare().getBook().getMetadata().getBriefSynopsis();
    this.category=String.join(",", book.getBookshare().getBook().getMetadata().getCategory());
    this.completeSynopsis=book.getBookshare().getBook().getMetadata().getCompleteSynopsis();
    this.contentId=book.getBookshare().getBook().getMetadata().getContentId();


    //convert String to date
    format = new SimpleDateFormat("yyyy");
    date = format.parse(book.getBookshare().getBook().getMetadata().getCopyright());
    this.copyright=date;


    this.downloadFormat=String.join(",", book.getBookshare().getBook().getMetadata().getDownloadFormat());
    this.dtbookSize=book.getBookshare().getBook().getMetadata().getDtbookSize();
    this.freelyAvailable=book.getBookshare().getBook().getMetadata().getFreelyAvailable();
    this.brf=book.getBookshare().getBook().getMetadata().getBrf();
    this.daisy=book.getBookshare().getBook().getMetadata().getDaisy();
    this.images=book.getBookshare().getBook().getMetadata().getImages();
    this.isbn13=book.getBookshare().getBook().getMetadata().getIsbn13();
    this.language=String.join(",", book.getBookshare().getBook().getMetadata().getLanguage());

    //convert String to date
    format = new SimpleDateFormat("MMddyyyy");
    date = format.parse(book.getBookshare().getBook().getMetadata().getPublishDate());
    this.publishDate=date;


    this.publisher=book.getBookshare().getBook().getMetadata().getPublisher();
    this.quality=book.getBookshare().getBook().getMetadata().getQuality();
    this.title=book.getBookshare().getBook().getMetadata().getTitle();

  }   
}
@Entity
@Table(name = "tblhasbooks")
public class HasBooks implements Serializable {

    //@Column(name = "Id",unique = true,nullable = false)

    @Id
    @GeneratedValue()
    private int hasBooksId;

    @ManyToOne(cascade = CascadeType.ALL)
    private Users user;

    @ManyToOne(cascade = CascadeType.ALL)
    private MetadataForHibernate book;




    public MetadataForHibernate getBook() {
        return book;
    }

    public Users getUser() {
        return user;
    }

    public int getHasBooksId() {
        return hasBooksId;
    }

    public void setHasBooksId(int hasBooksId) {
        this.hasBooksId = hasBooksId;
    }

    public void setUser(Users user) {
        this.user = user;
    }

    public void setBook(MetadataForHibernate book) {
        this.book = book;
    }

}
@Entity
@Table(name="tblusers")
public class Users implements Serializable{



public Users(){hasBooks = new ArrayList<HasBooks>();
}


@Id
@Column(name = "Id",unique = true,nullable = false)
@GeneratedValue(strategy=GenerationType.AUTO)
private int Id;

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

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

@OneToMany(mappedBy="user")
private List<HasBooks> hasBooks;


//Getters & Setters

public List<HasBooks> getHasBooks() {
    return hasBooks;
}

public void setHasBooks(List<HasBooks> hasBooks) {
    this.hasBooks = hasBooks;
}

public int getId() {
    return Id;
}

public void setUser_id(int Id) {
    this.Id = Id;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}


}
@实体
@表(name=“tblusers”)
公共类用户实现可序列化{
公共用户(){hasBooks=newarraylist();
}
@身份证
@列(name=“Id”,unique=true,nullable=false)
@GeneratedValue(策略=GenerationType.AUTO)
私有int-Id;
@列(name=“email”)
私人字符串电子邮件;
@列(name=“password”)
私有字符串密码;
@OneToMany(mappedBy=“用户”)
私人书籍;
//接球手和接球手
公共列表getHasBooks(){
还书;
}
public void setHasBooks(列出hasBooks){
this.hasBooks=hasBooks;
}
公共int getId(){
返回Id;
}
public void setUser_id(int id){
这个.Id=Id;
}
公共字符串getEmail(){
回复邮件;
}
公用电子邮件(字符串电子邮件){
this.email=电子邮件;
}
公共字符串getPassword(){
返回密码;
}
public void setPassword(字符串密码){
this.password=密码;
}
}

您需要指定属性名称,而不是属性类型

从HasBooks中选择H.MetadataForHibernate作为H

需要更正为

从HasBooks H中选择H.book

您需要一个联接来检查书籍属性

SELECT book 
  FROM HasBooks H inner join H.book book 
  where book.title :=title

您编写的查询不是有效的Hibernate查询语言(HQL),或者您可以始终使用从结果集中获取对象[]列表,保留已有的查询