Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hibernate mapping exception: Could not determine type for columns: [org.hibernate.mapping.Column]

Tags:

hibernate

jpa

I'm want to do a one-to-many mapping between two tables BookStock and Book, but I'm getting the following exception

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)]

The bookStock entity is

@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;
    }
}

The Book Entity is

@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;
    }
}

The Example class is

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();
    }
}

What's going wrong in this relation?

like image 633
tintin Avatar asked Jan 15 '12 13:01

tintin


1 Answers

It does not find all of your annotations, because you are annotating both fields and methods. You have to use only one strategy. In your case @Id annotation in id field is seen first and thats why annotations in get-methods does not have effect. As result hibernate assumes correctly that

List<Book> books;

is not mapped. If you move also annotations from id-field to the getId-method (for both entities), problem is solved.

About fixing other issues with mappings, please see comment below.

like image 154
Mikko Maunu Avatar answered Nov 05 '22 05:11

Mikko Maunu