无法在foreach中使用休眠实体

时间:2018-07-02 20:22:51

标签: java hibernate foreach

我是休眠的初学者,我的代码中有1个错误,我不知道如何解决。为什么我不能在foreach中使用我的实体。Error in foreach

这是通过构思创建的BookEntity类。

休眠版本5.0.1; Java SDK版本10.0.1

@Entity
 @Table(name = "book", schema = "library")
 public class BookEntity {

private boolean edit;
private long id;
private String name;
private long author;
private int publishYear;
private long publisherId;

public boolean isEdit(){return edit; }
public void setEdit(boolean edit){this.edit = edit;}

@Id
@Column(name = "id", nullable = false)
public long getId() {
    return id;
}
public void setId(long id) {
    this.id = id;
}

@Basic
@Column(name = "author_id", nullable = false, length = 20)
public long getAuthor() {return author; }
public void setAuthor(long author) {this.author = author;}

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

@Basic
@Column(name = "publish_year", nullable = false)
public int getPublishYear() {
    return publishYear;
}
public void setPublishYear(int publishYear) {
    this.publishYear = publishYear;
}


@Basic
@Column(name = "publisher_id", nullable = false)
public long getPublisherId() {
    return publisherId;
}
public void setPublisherId(long publisherId) {
    this.publisherId = publisherId;
}

    }

我将非常感谢您的帮助

1 个答案:

答案 0 :(得分:2)

可能是因为您没有以正确的方式定义bookList

如果您具有以下之一:

  • List bookList = new ArrayList();
  • List<Object> bookList = new ArrayList<>();

将其更改为List<BookEntity> booklist = new ArrayList<>();,它将正常工作。