2013-04-21 87 views
1

我是新的休眠。我想在書籍和作者之間創建多對多的連接,並在休眠狀態下在netbeans中創建。我看到的所有例子都是針對Eclipse的,而且我找不到我的問題的答案。 我Book.hbm.xml是:許多到很多休眠關係的中央表

<hibernate-mapping> 
<class name="Book" table="books"> 
    <id name="bookId" type="int" column="BookID"> 
    <generator class="assigned"/> 
    </id> 
    <property name="isbn" column="Isbn" type="string"/> 
    <property name="title" column="Title" type="string"/> 
    <property name="bookPicPath" column="BookPicPath" type="string"/> 
    <property name="summary" column="Summary" type="string"/> 
    <property name="genry" column="Genry" type="string"/> 
    <property name="parentGenry" column="ParentGenry" type="string"/> 
    <set name="authors" table="Book_Author" cascade="all"> 
     <key column="BookID" /> 
     <many-to-many column="AuthorID" class="Author" /> 
    </set> 

</class> 
</hibernate-mapping> 

和Author.hbm.xml是:

<hibernate-mapping> 
<class name="Author" table="authors"> 
    <id name="authorId" type="int" column="AuthorID"> 
    <generator class="assigned"/> 
    </id> 
    <property name="fname" column="Fname" type="string"/> 
    <property name="lname" column="Lname" type="string"/> 
    <property name="biography" column="Biography" type="string"/> 
    <property name="gender" column="Gender" type="string"/> 
    <property name="website" column="Website" type="string"/> 
    <property name="authorPicPath" column="AuthorPicPath" type="string"/> 
</class> 
</hibernate-mapping> 

在主類,我創建表是這樣的:

HibernateUtil.droptable("drop table authors"); 
HibernateUtil.setup("create table authors (AuthorID int, Fname VARCHAR(20), Lname VARCHAR(20), Biography VARCHAR(255), Gender VARCHAR(20), Website VARCHAR(255), AuthorPicPath VARCHAR(255))"); 
HibernateUtil.droptable("drop table books"); 
HibernateUtil.setup("create table books (BookID int, Isbn VARCHAR(20), Title VARCHAR(255), BookPicPath VARCHAR(255), Summary VARCHAR(255), Genry VARCHAR(255), ParentGenry VARCHAR(255))"); 

我的問題:

1.in多對多關係有一個連接表(Book_Author)。現在我必須手動或h ibernate創建它自己的?
2.在主類,我有:

SessionFactory sessions = new Configuration().configure().buildSessionFactory(); 
Session session = sessions.openSession(); 

    Transaction tx = null; 
    try { 
     tx = session.beginTransaction(); 

     Set<Author> authors = new HashSet<Author>(); 
     Author a1 = new Author("1","1","1","1","1","1"); 
     Author a2 = new Author("2","2","2","2","2","2"); 
     authors.add(a1); 
     authors.add(a2); 

     Book b1= new Book("a","a","a","a","a","a",authors); 
     session.save(b1); 

     tx.commit(); 
     tx = null;   

    } catch (HibernateException e) { 
     if (tx != null) 
      tx.rollback(); 
      e.printStackTrace(); 

    } finally { 
     session.close(); 
    } 

但是當我運行程序時,我看到了以下錯誤:

a different object with the same identifier value was already associated with the session: [Author#0] 

請guide.thanks。

回答

0
  1. 如果您使用的SchemaExport(也就是hbm2ddl)Hibernate可以自動生成關係表 (Book_Author)爲您服務。你可以在你的hibernate.cfg.xml這樣配置它:<property name="hibernate.hbm2ddl.auto">create-drop</property>

  2. 我看你用你的id用<generator class="assigned"/>。在這種情況下,您必須手動分配ID;如果你不這樣做,hibernate會拋出異常。