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有很大的創建一對多映射幫助 感謝你
thanx很多bt你可以告訴我,我使用多對一的y沒有顯示相同的錯誤,y它顯示我使用一對多? – 2014-09-26 13:26:38
@arjunnarahari你真的應該閱讀關於JPA的教程。如果作者有很多書,當然它需要是一個集合。 – Magnilex 2014-09-26 13:47:23