求助JBOSS下CMP中CMR错误

07-09-12 cswxzx

Book与Author一对一在修改Book时出现错误。

javax.ejb.EJBException: Data contains multiple values, but this cmr field is single valued: [45, 47, 48]
<p>

BookBean的代码:

/**
	 * @ejb.interface-method
	 * @ejb.relation name="BookAndAuthorRelation" role-name="BookInAuthor"
	 *               target-role-name="AuthorHasBooks" target-ejb="Author"
	 * @jboss.relation fk-column = "AUTHORID_FK" related-pk-field = "authorId"
	 * @return
	 */
	public abstract AuthorLocal getAuthor();

	/**
	 * @ejb.interface-method
	 * @param author
	 */
	public abstract void setAuthor(AuthorLocal author);
<p>

ejb-jar.xml

<ejb-relation >
         <ejb-relation-name>BookAndAuthorRelation</ejb-relation-name>

         <ejb-relationship-role >
            <ejb-relationship-role-name>BookInAuthor</ejb-relationship-role-name>
            <multiplicity>One</multiplicity>
            <relationship-role-source >
               <ejb-name>Book</ejb-name>
            </relationship-role-source>
            <cmr-field >
               <cmr-field-name>author</cmr-field-name>
            </cmr-field>
         </ejb-relationship-role>

         <ejb-relationship-role >
            <ejb-relationship-role-name>AuthorHasBooks</ejb-relationship-role-name>
            <multiplicity>One</multiplicity>
            <relationship-role-source >
               <ejb-name>Author</ejb-name>
            </relationship-role-source>
         </ejb-relationship-role>

      </ejb-relation>
<p>

jbosscmp-jdbc.xml

<ejb-relation>
      <ejb-relation-name>BookAndAuthorRelation</ejb-relation-name>

      <ejb-relationship-role>
          <ejb-relationship-role-name>BookInAuthor</ejb-relationship-role-name>
		  <key-fields/>

      </ejb-relationship-role>
      <ejb-relationship-role>
          <ejb-relationship-role-name>AuthorHasBooks</ejb-relationship-role-name>
          <key-fields>
             <key-field>
               <field-name>authorId</field-name>
               <column-name>AUTHORID_FK</column-name>
             </key-field>
          </key-fields>

      </ejb-relationship-role>
    </ejb-relation>
<p>

修改Book的代码:

public boolean editBook(BookData book) {
		try {
			BookLocalHome home = BookUtil.getLocalHome();
			if (home != null) {
				BookLocal bookLocal = home.findByPrimaryKey(book.getBookId());
				if (bookLocal != null) {
					bookLocal.setBookName(book.getBookName());
					bookLocal.setPrice(book.getPrice());
					bookLocal.setQuantity(book.getQuantity());
					AuthorLocalHome authorHome = AuthorUtil.getLocalHome();
					AuthorLocal authorLocal = authorHome.findByPrimaryKey(book.getAuthor().getAuthorId());
					bookLocal.setAuthor(authorLocal);
					BookTypeLocalHome bookTypeHome = BookTypeUtil.getLocalHome();
					BookTypeLocal bookTypeLocal = bookTypeHome.findByPrimaryKey(book.getType().getTypeId());
					bookLocal.setType(bookTypeLocal);
					return true;
				}
			}
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (FinderException e) {
			e.printStackTrace();
		}
		return false;
	}
<p>

banq
2007-09-15 18:04

大概意思是你设定是一对一关系,但是好像实际不是这样,可能一本书有多个作者,不是一对一关系。