多对多hibernate关系中的中心表

多对多hibernate关系中的中心表,hibernate,netbeans,many-to-many,Hibernate,Netbeans,Many To Many,我是冬眠新手。我想在netbeans中使用hibernate在book和author之间创建多对多连接。我看到的所有例子都是针对Eclipse的,我找不到问题的答案。 我的Book.hbm.xml是: <hibernate-mapping> <class name="Book" table="books"> <id name="bookId" type="int" column="BookID"> <generator class="ass

我是冬眠新手。我想在netbeans中使用hibernate在book和author之间创建多对多连接。我看到的所有例子都是针对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>
<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>
我的问题是:

1.在多对多关系中有一个连接表(Book_Author)。现在我必须手动执行,否则hibernate将创建自己的连接表?
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();
    }
请导游,谢谢

  • 如果使用SchemaExport(又名hbm2ddl),hibernate可以自动生成关系表 (
    Book\u Author
    )给你。您可以在
    hibernate.cfg.xml中对其进行如下配置:
    createdrop

  • 我看到你用
    作为身份证。在这种情况下,您必须手动分配id;如果不这样做,hibernate将抛出该异常

  • 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]