2014-09-26 55 views
0

IM還挺停留在執行一個在休眠一對多關係多對多關係查詢,所以需要一些建議,以在那裏我去錯了對於一個在休眠

Author類

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類

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) 

我想從筆者本書作爲一個作者可以寫很多書 有人可以解釋我在哪裏,我去錯了 它WLD有很大的創建一對多映射幫助 感謝你

回答

1

@OneToMany元素必須是Collection。因此,而不是:

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

用途:

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

Set確保只有唯一Book S能夠存在(根據您的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); 

在這裏,使用​​代替。

+0

thanx很多bt你可以告訴我,我使用多對一的y沒有顯示相同的錯誤,y它顯示我使用一對多? – 2014-09-26 13:26:38

+2

@arjunnarahari你真的應該閱讀關於JPA的教程。如果作者有很多書,當然它需要是一個集合。 – Magnilex 2014-09-26 13:47:23

1

這是直截了當的。在筆者book場應該是一個集合,一組:

private Set<Book> books;

你也應該改變setter和getter相應爲好。還請注意字段名稱:books