arjun narahari
arjun narahari

Reputation: 176

query regarding one to many relationship in hibernate

i m kinda stuck in performing one to many relationship in hibernate , so needed some suggestions as to where i m going wrong

Author Class

package com.hibernate.arjun3;

import javax.persistence.*;

@Entity
public class Author {

private String name;
@Id
@GeneratedValue
private int author_id;

@OneToMany(cascade=CascadeType.ALL)
private Book book;

public Book getBook() {
    return book;
}

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

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAuthor_id() {
    return author_id;
}

public void setAuthor_id(int author_id) {
    this.author_id = author_id;
}

}

Book Class

package com.hibernate.arjun3;

import javax.persistence.*;


@Entity
public class Book {

private String title;

@Id
@GeneratedValue
private int author_id;

public String getTitle() {
    return title;
}

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

public int getAuthor_id() {
    return author_id;
}

public void setAuthor_id(int author_id) {
    this.author_id = author_id;
}
}

My Main Class

package com.hibernate.arjun3;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class Main {

public static void main(String args[]) {

    Author author = new Author();
    author.setName("Brian");

    Book book = new Book();
    book.setTitle("The Incredible Brian");

    Book book2 = new Book();
    book2.setTitle("Superman");

    author.setBook(book);
    author.setBook(book2);


    SessionFactory factory = new AnnotationConfiguration().configure().buildSessionFactory();
    Session session = factory.openSession();
    session.beginTransaction();

    session.save(author);

    session.getTransaction().commit();
    session.close();
    factory.close();

}
}

and i m getting this error

Exception in thread "main" org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: com.hibernate.arjun3.Author.book
    at org.hibernate.cfg.annotations.CollectionBinder.getCollectionBinder(CollectionBinder.java:330)
    at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1919)
    at org.hibernate.cfg.AnnotationBinder.processIdPropertiesIfNotAlready(AnnotationBinder.java:963)
    at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:796)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processAnnotatedClassesQueue(Configuration.java:3788)
    at org.hibernate.cfg.Configuration$MetadataSourceQueue.processMetadata(Configuration.java:3742)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1410)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
    at com.hibernate.arjun3.MainAB.main(MainAB.java:24)

i want to create one to many mapping from author to book as to one author can write many books can somebody explain me where i m going wrong it wld be of great help Thanking you

Upvotes: 0

Views: 934

Answers (2)

Ori Dar
Ori Dar

Reputation: 19020

This is straitforward. The book field in Author should be a Collection, a Set:

private Set<Book> books;

You should also change setter and getter accordingly as well. Also note the field name: books.

Upvotes: 1

Magnilex
Magnilex

Reputation: 11988

The @OneToMany element must be a Collection. So instead of:

@OneToMany(cascade=CascadeType.ALL)
private Book book;

Use:

@OneToMany(cascade=CascadeType.ALL)
private Set<Book> books;

The Set makes sure that only unique Books can exist (based on your equals() implementation). You can use List<Book> if that is not needed.

Also, when you are trying to add two books to the Author, you are first adding one, then overwriting it with the next:

Book book = new Book();
book.setTitle("The Incredible Brian");

Book book2 = new Book();
book2.setTitle("Superman");

author.setBook(book);
author.setBook(book2);

Here, use books.add(book) instead.

Upvotes: 1

Related Questions