Cast default hibernate createSQLQuery to list of self-made class

Viewed 928

I have a problem - I'm new to hibernate and need to cast a table, returned by executing session.createSQLQuery(query).list() command. I have tried to add addEntity(Result.class) command, but program throws exception:

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.MappingException: Unknown entity: com.hib.entities.Result

My result table in mysql looks like this:


| bookName | authorSurname | authorFirstName | authorPatronymic | releaseDate | amountBooks|

I have tried to create Entity class Result and annotate it, but it seems like I've failed. My entity class looks like this:

package com.hib.entities;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table
public class Result {

    String bookName;
    String authorSurname;
    String authorFirstName;
    String authorPatronymic;
    Integer releaseDate;
    Integer amountBooks;

    public Result(String bookName, String authorSurname, String authorFirstName, String authorPatronymic, Integer releaseDate, Integer
            amountBooks) {
        this.bookName = bookName;
        this.authorSurname = authorSurname;
        this.authorFirstName = authorFirstName;
        this.authorPatronymic = authorPatronymic;
        this.releaseDate = releaseDate;
        this.amountBooks = amountBooks;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public void setAuthorSurname(String authorSurname) {
        this.authorSurname = authorSurname;
    }

    public void setAuthorFirstName(String authorFirstName) {
        this.authorFirstName = authorFirstName;
    }

    public void setAuthorPatronymic(String authorPatronymic) {
        this.authorPatronymic = authorPatronymic;
    }

    public void setReleaseDate(Integer releaseDate) {
        this.releaseDate = releaseDate;
    }

    public void setAmountBooks(Integer amountBooks) {
        this.amountBooks = amountBooks;
    }

    public String getBookName() {
        return bookName;
    }

    public String getAuthorSurname() {
        return authorSurname;
    }

    public String getAuthorFirstName() {
        return authorFirstName;
    }

    public String getAuthorPatronymic() {
        return authorPatronymic;
    }

    public Integer getReleaseDate() {
        return releaseDate;
    }

    public Integer getAmountBooks() {
        return amountBooks;
    }

    @Override
    public String toString() {
        return "Result{" +
                "bookName='" + bookName + '\'' +
                ", authorSurname='" + authorSurname + '\'' +
                ", authorFirstName='" + authorFirstName + '\'' +
                ", authorPatronymic='" + authorPatronymic + '\'' +
                ", releaseDate=" + releaseDate +
                ", amountBooks=" + amountBooks +
                '}';
    }
}

Can you please help me to solve this problem?

1 Answers
Related