Nie mogę sobie poradzić z odwzorowaniem relacji wiele do wielu. Mam takie tabele:
Book:
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| titlePl | varchar(100) | NO | | NULL | |
| titleOrg | varchar(100) | YES | | NULL | |
| publisher | varchar(50) | NO | | NULL | |
| pages | int(11) | NO | | NULL | |
| ISBN | varchar(17) | YES | UNI | NULL | |
| price | float(5,2) | NO | | NULL | |
| mainCat | varchar(20) | NO | | NULL | |
| cat | varchar(50) | NO | | NULL | |
| year | int(11) | YES | | NULL | |
| ratingCounter | int(11) | YES | | NULL | |
| ratingSum | float(5,1) | YES | | NULL | |
| info | text | YES | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
<hibernate-mapping>
<class catalog="IWM" name="pl.iwm.Book" table="book">
<id name="id" type="java.lang.Integer">
<column name="id"/>
<generator class="identity"/>
</id>
<property name="titlePl" type="string">
<column length="100" name="titlePl" not-null="true"/>
</property>
<property name="titleOrg" type="string">
<column length="100" name="titleOrg"/>
</property>
<property name="publisher" type="string">
<column length="50" name="publisher" not-null="true"/>
</property>
<property name="pages" type="int">
<column name="pages" not-null="true"/>
</property>
<property name="isbn" type="string">
<column length="17" name="ISBN" unique="true"/>
</property>
<property name="price" type="float">
<column name="price" not-null="true" precision="4"/>
</property>
<property name="mainCat" type="string">
<column length="20" name="mainCat" not-null="true"/>
</property>
<property name="cat" type="string">
<column length="20" name="cat" not-null="true"/>
</property>
<property name="year" type="java.lang.Integer">
<column name="year"/>
</property>
<property name="ratingCounter" type="java.lang.Integer">
<column name="ratingCounter"/>
</property>
<property name="ratingSum" type="float">
<column name="ratingSum"/>
</property>
<property lazy="true" name="info" type="string">
<column length="65535" name="info"/>
</property>
<list name="authors" table="books_authors">
<key column="bookId"/>
<list-index column="id"/>
<many-to-many class="pl.iwm.Author" column="authorId"/>
</list>
</class>
</hibernate-mapping>
Author:
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| fName | varchar(20) | NO | | NULL | |
| mName | varchar(20) | YES | | NULL | |
| lName | varchar(20) | NO | | NULL | |
| info | text | YES | | NULL | |
+-------+-------------+------+-----+---------+----------------+
<hibernate-mapping>
<class catalog="IWM" name="pl.iwm.Author" table="author">
<id name="id" type="java.lang.Integer">
<column name="id"/>
<generator class="identity"/>
</id>
<property name="fname" type="string">
<column length="20" name="fName" not-null="true"/>
</property>
<property name="mname" type="string">
<column length="20" name="mName"/>
</property>
<property name="lname" type="string">
<column length="20" name="lName" not-null="true"/>
</property>
<property name="info" type="string">
<column length="65535" name="info"/>
</property>
<list name="books" table="books_authors">
<key column="authorId"/>
<list-index column="id"/>
<many-to-many class="pl.iwm.Book" column="bookId"/>
</list>
</class>
</hibernate-mapping>
Tabela łącząca Books_Authors:
+----------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| bookId | int(11) | NO | MUL | NULL | |
| authorId | int(11) | NO | MUL | NULL | |
+----------+---------+------+-----+---------+----------------+
Gdy próbuję wykonać taki kod:
BookDAO bookDAO = new BookDAO();
Book book = bookDAO.get(1);
System.out.println(book.getTitlePl());
List authors = new ArrayList();
authors = book.getAuthors();
Iterator iter = authors.iterator();
while (iter.hasNext()) {
Author author = (Author) iter.next();
System.out.println(author.getFname() + author.getLname());
}
Dostaję java.lang.NullPointerException przy pierwszym odwołaniu w pętli do autora.
Gdy wyświetlam kolekcję na stronie za pomocą <t:dataTable> dostaję najpierw puste wiersze a na końcu dopiero te właściwe. Przy pierwszej książce z tabeli jest to 5 pustych i dopiero 2 prawidłowe wierszy a przy ostatniej wyświetlają się wszystkie i tylko ostatni jest właściwy.
Tak samo się dzieje gdy próbuję w drugą stronę. Wyświetlając książki napisane przez danego autora dostaję kilka wierszy pustych i dopiero na końcu te właściwe.
A może to nie błąd z mapowaniem tylko coś zupełnie innego? Gdy kolekcją był set a nie lista wszystko było prawidłowo. Tylko jak wyświetlić set za pomocą <h:dataTable>?