【问题标题】:Can't use hibernate entity in foreach无法在 foreach 中使用休眠实体
【发布时间】:2018-07-02 20:22:50
【问题描述】:

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

这是由idea创建的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;
}

    }

非常感谢您的帮助

【问题讨论】:

    标签: java hibernate foreach


    【解决方案1】:

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

    如果您有以下情况之一:

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

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

    【讨论】:

    • 或者更好的是,使用 Set 而不是 List。
    • 好贴@kaba713 +1
    猜你喜欢
    • 2023-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-29
    • 1970-01-01
    • 2017-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多