Hibernate映射异常:无法确定列的类型:[org.hibernate.mapping.Column]

tin*_*tin 12 hibernate jpa

我想在两个表BookStock和Book之间进行一对多的映射,但是我得到以下异常

Exception in thread "main" org.hibernate.MappingException: Could not determine type for: java.util.List, at table: book_stock, for columns: [org.hibernate.mapping.Column(books)]
Run Code Online (Sandbox Code Playgroud)

bookStock实体是

@Entity
@Table(name = "book_stock")
public class BookStock {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private Long stock;
    private List<Book> books;

    @Column(name = "id")
    public Long getId() {
        return id;
    }

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

    @Column(name = "stock")
    public Long getStock() {
        return stock;
    }

    public void setStock(Long stock) {
        this.stock = stock;
    }

    @OneToMany
    @PrimaryKeyJoinColumn
    public List<Book> getBooks() {
        return books;
    }

    public void setBooks(List<Book> books) {
        this.books = books;
    }
}
Run Code Online (Sandbox Code Playgroud)

图书实体是

@Entity
@Table(name = "book")
public class Book {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String bookName;
    private BigDecimal price;
    private BookStock bookStock;

    @Column(name = "id")
    public Long getId() {
        return id;
    }

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

    @Column(name = "book_name")
    public String getBookName() {
        return bookName;
    }

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

    @Column(name = "price")
    public BigDecimal getPrice() {
        return price;
    }

    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    @ManyToOne
    public BookStock getBookStock() {
        return bookStock;
    }

    public void setBookStock(BookStock bookStock) {
        this.bookStock = bookStock;
    }
}
Run Code Online (Sandbox Code Playgroud)

Example类是

public class transactionsExample {

    public static void main(String[] args) {
        AnnotationConfiguration configuration = new AnnotationConfiguration();
        configuration.configure();
        configuration.addAnnotatedClass(Book.class);
        configuration.addAnnotatedClass(BookStock.class);
        configuration.setNamingStrategy(ImprovedNamingStrategy.INSTANCE);
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        Session session = sessionFactory.openSession();

        session.beginTransaction();
        Book book1 = EntityBuilder.book().withISBN("21234").withName("Anario").withPrice(25.90).purchase();
       session.save(book1);
        session.getTransaction().commit();
    }
}
Run Code Online (Sandbox Code Playgroud)

这种关系出了什么问题?

Mik*_*unu 42

它没有找到所有注释,因为您正在注释两个字段和方法.您只需使用一种策略.在你的情况下,首先看到id 字段中的 @Id注释,这就是为什么get-methods中的注释没有效果.因此,hibernate正确地假定了这一点

List<Book> books;
Run Code Online (Sandbox Code Playgroud)

未映射.如果还将注释从id-field移动到getId-method(对于两个实体),问题就解决了.

关于使用映射修复其他问题,请参阅下面的注释.

  • 接得好.而且,映射完全错误.PrimaryKeyJoinColumn没有意义,并且关联是双向的,但是单侧没有mappedBy属性.见http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html_single/#collections-bidirectional (3认同)