java - hibernate多对一注释-映射异常

标签 java hibernate mapping

我必须表bookborrow,并且我想在我的Borrow类方法中使用来获取Book。我有这样的东西:

    @Entity
@Table(name="borrow")
public class Borrow {
    @Id
    @Column(name="ID")
    @GeneratedValue
    private Long id;

    @Column(name="book_id")
    private long bookId;

    @Column(name="user_id")
    private long userId;

    @Column(name="borrow_date")
    private Date borrowDate;

    @Column(name="return_date")
    private Date returnDate;

    private Book book;

    public long getBookId() {
        return bookId;
    }

    public void setBookId(long bookId) {
        this.bookId = bookId;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id", nullable = false)
    @ForeignKey(name = "id")
    public Book getBook() {
        return this.book;
    }

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

    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    public Date getBorrowDate() {
        return borrowDate;
    }

    public void setBorrowDate(Date borrowDate) {
        this.borrowDate = borrowDate;
    }

    public Date getReturnDate() {
        return returnDate;
    }

    public void setReturnDate(Date returnDate) {
        this.returnDate = returnDate;
    }

}

在表书中我有 id 字段,我想通过它加入。就像 book.id = 借用.book_id 一样。我得到执行:

Could not determine type for: Book, at table: borrow, for columns: [org.hibernate.mapping.Column(book)]

图书类别:

    @Entity
@Table(name="book")
public class Book {
    @Id
    @Column(name="ID")
    @GeneratedValue
    private Long id;

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

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

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

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

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

    @Column(name="book_url")
    private String bookUrl;

    @Column(name="review_url")
    private String reviewUrl;

    @Column(name="status_id")
    private int status;

    @Column(name="accepted")
    private boolean accepted;

    @Column(name="in_library")
    private boolean inLibrary;

    @Column(name="user_id")
    private long userId;

    public long getUserId() {
        return userId;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    public boolean isAccepted() {
        return accepted;
    }

    public void setAccepted(boolean accepted) {
        this.accepted = accepted;
    }

    public boolean isInLibrary() {
        return inLibrary;
    }

    public void setInLibrary(boolean inLibrary) {
        this.inLibrary = inLibrary;
    }

    public Book(long l, String string) {
        this.title = string;
        this.id = l;
    }


    public Book() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

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

    public String getAuthor() {
        return author;
    }

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

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public String getPublisher() {
        return publisher;
    }

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

    public String getBookUrl() {
        return bookUrl;
    }

    public void setBookUrl(String bookUrl) {
        this.bookUrl = bookUrl;
    }

    public String getReviewUrl() {
        return reviewUrl;
    }

    public void setReviewUrl(String reviewUrl) {
        this.reviewUrl = reviewUrl;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }




}

最佳答案

问题在于您混合了字段和属性访问。您的 @Id 注释位于一个字段上,因此 hibernate 就是在其中查找 book 的注释。来自 2.2.2.2. Access type :

The placement of annotations within a class hierarchy has to be consistent (either field or on property) to be able to determine the default access type. It is recommended to stick to one single annotation placement strategy throughout your whole application.

因此,要么保持注释一致(推荐),要么使用该部分其余部分中描述的方法来覆盖 book 的访问类型(例如,通过使用 @Access注释)

关于java - hibernate多对一注释-映射异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17097209/

相关文章:

java:检查数组1中的任何元素是否存在于数组2中

java - 使用 Tomcat 9 时出现 Jasper 异常

java - 可以使用 Hibernate 来存储没有类来表示其结构的数据的 HashMap 吗?

结果均匀分布的 MySQL Lat/Lng Boundary

inheritance - Entity Framework 4.1 代码优先 : Get all Entities with a specific base class

java - 使用 Java 对链表进行递归合并排序

java - 使所有对 mysite.com/user/specified/path 的请求运行相同的 JSP

java - 在 Hibernate 集合和我自己的集合之间转换

java - 如何从循环外部获取每个循环的长计数值

Grails:映射字段的列名和相同类型的belongsTo