查询关于hibernate中的一对多关系

时间:2014-09-26 12:40:03

标签: java hibernate

我有点坚持在休眠中执行一对多的关系,所以需要一些关于我哪里出错的建议

作者类

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

}

书类

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

我的主要课程

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

}
}

我收到此错误

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)

我想创建从作者到书的一对多映射,因为一个作者可以写很多书 谁能解释我哪里出错了 它会给你很大的帮助 谢谢你

2 个答案:

答案 0 :(得分:1)

@OneToMany元素必须是Collection。所以而不是:

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

使用:

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

Set确保只存在唯一的Book(基于您的equals()实施)。如果不需要,您可以使用List<Book>

此外,当您尝试向Author添加两本书时,您首先添加一本书,然后使用下一本书覆盖它:

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

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

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

此处,请改用books.add(book)

答案 1 :(得分:1)

这是不可能的。作者中的book字段应为集合,集合:

private Set<Book> books;

您还应该相应地更改setter和getter。另请注意字段名称:books