[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 423
  • Last Modified:

CMR problem

Hi,

I'm creating an enterprise application using EJB 2.0, CMP 2.0, MySql and WebLogic 8.11. I'm using JBuilder 2005 as an IDE.

I've created a cmr relationship between a local entity bean "Category" and "Product". It is a one-to-one relationship. The field for connecting the two beans is categoryId, which is the primary key in Category and just a normal field in Product (but is going to be used as a foreign key).

They way I created the beans, was to import the database schema into JBuilder, marked the following tables I wanted to use, and clicked “create cmp 2.0 entity bean”. It then creates all the code including interfaces and xml.

When setting up this in JBuilder, everything seems to be ok. But when I try to compile the project, I get this message:

"WebShopEar.jar": Role 'CategoryRelationshipRole' of relationship 'product-category' contains an invalid key-column value of 'category_id'. The key-column must specify a primary key column in the related bean. However, there is no primary key column named 'category_id' defined in the related bean 'Product' or else 'category_id' is not being recognized due to case mismatch.

From weblogic-cmp-rdbms-jar:

<weblogic-rdbms-relation>
    <relation-name>product-category</relation-name>
    <weblogic-relationship-role>
      <relationship-role-name>CategoryRelationshipRole</relationship-role-name>
      <relationship-role-map>
        <foreign-key-table>category_tbl</foreign-key-table>
        <primary-key-table>product_tbl</primary-key-table>
        <column-map>
          <foreign-key-column>category_id</foreign-key-column>
          <key-column>category_id</key-column>
        </column-map>
      </relationship-role-map>
    </weblogic-relationship-role>
  </weblogic-rdbms-relation>


From ejb-jar:

<ejb-relationship-role>
        <description>category</description>
        <ejb-relationship-role-name>CategoryRelationshipRole</ejb-relationship-role-name>
        <multiplicity>One</multiplicity>
        <relationship-role-source>
          <description>category</description>
          <ejb-name>Category</ejb-name>
        </relationship-role-source>
      </ejb-relationship-role>

Description of the tables

Product:
Field Type Null Key Default Extra
product_id int(11)   PRI 0  
product_name varchar(50) YES   NULL  
price double(6,2) YES   NULL  
category_id int(6) YES   NULL  
product_code varchar(20) YES   NULL

Category:
Field       Type Null Key Default Extra
category_id int(6)   PRI 0  
category_name varchar(50) YES   NULL  

CMR fields from the product bean:
public abstract void setCategory(Category category);
public abstract Category getCategory();

CMP fields for categoryId from the product bean:
 public abstract void setCategoryId(String categoryId);
 public abstract String getCategoryId();

CMP fields for categoryId from the category bean:
public abstract void setCategoryId(String categoryId);
public abstract String getCategoryId();


Appreciate all help!

regards,

Asgaut
0
amjolne
Asked:
amjolne
  • 7
  • 7
1 Solution
 
petmagdyCommented:
each CMP bean must have a primary key, if the primary key is one column only then it can be a java object like (java.lang.String or Long or Else) and no need for PK CLass also this must be a valid CMP Field (for example Name categoryId) on the entity bean project and the entity descriptor in ejb-jar.xml must contain the primary key description like this:

      <primkey-field>categoryId</primkey-field>

and CMP field like this:

      <cmp-field>
        <field-name>categoryId</field-name>
      </cmp-field>

and:
      <prim-key-class>java.lang.Long</prim-key-class>
or whatever appropiate java class

in the weblogic-cmp-rdbms-jar.xml descriptor the CMP field must mapped to a DB field like this:
      <field-map>
        <cmp-field>categoryId</cmp-field>
        <dbms-column>CATEGORY_ID</dbms-column>
      </field-map>

for the DB field names always make it uppercase (apply this also on the  <weblogic-rdbms-relation> DB fields names elements)


Cheers!
0
 
amjolneAuthor Commented:
Hi,

THanks for answering petmagdy. All you suggested there, except the uppercase thing, is already in my code. I tried to change the db fields to uppercase, but it didn't help.

By the way, the primary key class is String.

The categoryId field in the Product bean is just a normal CMP field. It is not a foreign key in the database, because I 'm running a MySql database. I can't understand why the compiler says that categoryid has to be a PRIMARY key in the product bean. Of course it is the productId that is the primary key there. The foreign field in the related bean don't have to be a primary key, it is just a normal cmp field. Right?

Any other suggestions what I can do to solve this problem?

thanks,


Asgaut
0
 
petmagdyCommented:
ahhhh ok, I got it ur relation desc. in weblogic-cmp-rdbms-jar.xml should be:
<weblogic-rdbms-relation>
    <relation-name>product-category</relation-name>
    <weblogic-relationship-role>
      <relationship-role-name>CategoryRelationshipRole</relationship-role-name>
      <relationship-role-map>
        <foreign-key-table>product_tbl</foreign-key-table>
        <primary-key-table>category_tbl</primary-key-table>
        <column-map>
          <foreign-key-column>category_id</foreign-key-column>
          <key-column>category_id</key-column>
        </column-map>
      </relationship-role-map>
    </weblogic-relationship-role>
  </weblogic-rdbms-relation>

the forign key table in ur case is Product not Category and the primary key table is category

Cheers!
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
amjolneAuthor Commented:
Hi again :)

I tried your solution, but then I got this error:

"WebShopEar.jar": In relationship 'product-category', role 'CategoryRelationshipRole', a weblogic-relationship-role element contains the wrong number of column mappings. A single column mapping must be given for each primary key column in the bean referenced by the mapping.

You seem like you understand this better then me. Any suggestions on this?


regards
0
 
petmagdyCommented:
amjolne,

let me check with u the following:

1- the Product entity suppose to have a one field PK which is product_id
2- the Category entity suppose to have a one field PK which is category_id

please post the following:

1- From the ejb-jar.xml the <entity>.......</entity> descriptor for Product and category
2- From the ejb-jar.xml the <ejb-relation>.......</ejb-relation> descriptor for this relation
0
 
amjolneAuthor Commented:
hi,

Here is the code from ejb-jar.xml:

 <entity>
      <ejb-name>Product</ejb-name>
      <local-home>au.uts.aip.webshop.ejb.ProductHome</local-home>
      <local>au.uts.aip.webshop.ejb.Product</local>
      <ejb-class>au.uts.aip.webshop.ejb.ProductBean</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.String</prim-key-class>
      <reentrant>False</reentrant>
      <cmp-version>2.x</cmp-version>
      <abstract-schema-name>Product</abstract-schema-name>
      <cmp-field>
        <field-name>productId</field-name>
      </cmp-field>
      <cmp-field>
        <field-name>productName</field-name>
      </cmp-field>
      <cmp-field>
        <field-name>price</field-name>
      </cmp-field>
      <cmp-field>
        <field-name>categoryId</field-name>
      </cmp-field>
      <cmp-field>
        <field-name>productCode</field-name>
      </cmp-field>
      <primkey-field>productId</primkey-field>
      <query>
        <query-method>
          <method-name>findProductsCategoryId</method-name>
          <method-params>
            <method-param>java.lang.String</method-param>
          </method-params>
        </query-method>
        <ejb-ql>SELECT DISTINCT OBJECT(p)
FROM product_tbl p
WHERE p.category_id = ?1</ejb-ql>
      </query>
    </entity>
    <entity>
      <ejb-name>User</ejb-name>
      <local-home>au.uts.aip.webshop.ejb.UserHome</local-home>
      <local>au.uts.aip.webshop.ejb.User</local>
      <ejb-class>au.uts.aip.webshop.ejb.UserBean</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>au.uts.aip.webshop.ejb.UserPK</prim-key-class>
      <reentrant>False</reentrant>
      <cmp-version>2.x</cmp-version>
      <abstract-schema-name>User</abstract-schema-name>
      <cmp-field>
        <field-name>username</field-name>
      </cmp-field>
      <cmp-field>
        <field-name>password</field-name>
      </cmp-field>
    </entity>

<entity>
      <ejb-name>Category</ejb-name>
      <local-home>au.uts.aip.webshop.ejb.CategoryHome</local-home>
      <local>au.uts.aip.webshop.ejb.Category</local>
      <ejb-class>au.uts.aip.webshop.ejb.CategoryBean</ejb-class>
      <persistence-type>Container</persistence-type>
      <prim-key-class>java.lang.String</prim-key-class>
      <reentrant>False</reentrant>
      <cmp-version>2.x</cmp-version>
      <abstract-schema-name>Category</abstract-schema-name>
      <cmp-field>
        <field-name>categoryId</field-name>
      </cmp-field>
      <cmp-field>
        <field-name>categoryName</field-name>
      </cmp-field>
      <primkey-field>categoryId</primkey-field>
      <query>
        <query-method>
          <method-name>findAllCategories</method-name>
          <method-params />
        </query-method>
        <ejb-ql>SELECT OBJECT(p)
FROM category_tbl  p</ejb-ql>
      </query>
    </entity>

the relation....

 <ejb-relation>
      <ejb-relation-name>product-category</ejb-relation-name>
      <ejb-relationship-role>
        <description>product</description>
        <ejb-relationship-role-name>ProductRelationshipRole</ejb-relationship-role-name>
        <multiplicity>One</multiplicity>
        <relationship-role-source>
          <description>product</description>
          <ejb-name>Product</ejb-name>
        </relationship-role-source>
        <cmr-field>
          <description>category</description>
          <cmr-field-name>category</cmr-field-name>
        </cmr-field>
      </ejb-relationship-role>
      <ejb-relationship-role>
        <description>category</description>
        <ejb-relationship-role-name>CategoryRelationshipRole</ejb-relationship-role-name>
        <multiplicity>One</multiplicity>
        <relationship-role-source>
          <description>category</description>
          <ejb-name>Category</ejb-name>
        </relationship-role-source>
      </ejb-relationship-role>
    </ejb-relation>


Find anythiing?


0
 
amjolneAuthor Commented:
that user entity wasn't suppose to be posted. sorry
0
 
petmagdyCommented:
amjolne,

Yes found something missing, try one of the following 2 alternatives:

Alternative 1:
Change:
<weblogic-rdbms-relation>
    <relation-name>product-category</relation-name>
    <weblogic-relationship-role>
      <relationship-role-name>CategoryRelationshipRole</relationship-role-name>
      <relationship-role-map>
        <foreign-key-table>category_tbl</foreign-key-table>
        <primary-key-table>product_tbl</primary-key-table>
        <column-map>
          <foreign-key-column>category_id</foreign-key-column>
          <key-column>category_id</key-column>
        </column-map>
      </relationship-role-map>
    </weblogic-relationship-role>
  </weblogic-rdbms-relation>

to:
<weblogic-rdbms-relation>
    <relation-name>product-category</relation-name>
    <weblogic-relationship-role>
      <relationship-role-name>ProductRelationshipRole</relationship-role-name>
      <relationship-role-map>
        <foreign-key-table>category_tbl</foreign-key-table>
        <primary-key-table>product_tbl</primary-key-table>
        <column-map>
          <foreign-key-column>category_id</foreign-key-column>
          <key-column>category_id</key-column>
        </column-map>
      </relationship-role-map>
    </weblogic-relationship-role>
  </weblogic-rdbms-relation>

Alternative 2:
change:

      <ejb-relationship-role>
        <description>category</description>
        <ejb-relationship-role-name>CategoryRelationshipRole</ejb-relationship-role-name>
        <multiplicity>One</multiplicity>
        <relationship-role-source>
          <description>category</description>
          <ejb-name>Category</ejb-name>
        </relationship-role-source>
      </ejb-relationship-role>
    </ejb-relation>

to:
      <ejb-relationship-role>
        <description>category</description>
        <ejb-relationship-role-name>CategoryRelationshipRole</ejb-relationship-role-name>
        <multiplicity>One</multiplicity>
        <relationship-role-source>
          <description>category</description>
          <ejb-name>Category</ejb-name>
        </relationship-role-source>
         <cmr-field>
          <description>product</description>
          <cmr-field-name>product</cmr-field-name>
        </cmr-field>
     </ejb-relationship-role>
    </ejb-relation>

and to the Category Interface add:

public abstract void setProduct(Product product);
public abstract Product getProduct();
0
 
amjolneAuthor Commented:
Hi

Just gotta say that I really appreciate that yor're trying to help me :)

I tried both your alternatives. The first one resulted in the message (same as last time):
"WebShopEar.jar": In relationship 'product-category', role 'CategoryRelationshipRole', a weblogic-relationship-role element contains the wrong number of column mappings. A single column mapping must be given for each primary key column in the bean referenced by the mapping.

The second one resulted in the message I got to start with.
With this code, you create a bidirectional relationship. That is not needed, I think.

I can't understand what is wrong here. Have used ours googled for it, but can't find nothing that helps.

Can you see anything more??

0
 
petmagdyCommented:
>>I tried both your alternatives. The first one resulted in the message (same as last time):
"WebShopEar.jar": In relationship 'product-category', role 'CategoryRelationshipRole', a weblogic-relationship-role element contains the wrong number of column mappings. A single column mapping must be given for each primary key column in the bean referenced by the mapping.


If u applied Alterantive one then the message should have been changed to:

WebShopEar.jar": In relationship 'product-category', role 'ProductRelationshipRole', a weblogic-relationship-role element contains the wrong number of column mappings. A single column mapping must be given for each primary key column in the bean referenced by the mapping.

And a correction make <weblogic-rdbms-relation>...</weblogic-rdbms-relation> to be:

<weblogic-rdbms-relation>
    <relation-name>product-category</relation-name>
    <weblogic-relationship-role>
      <relationship-role-name>ProductRelationshipRole</relationship-role-name>
      <relationship-role-map>product_tbl
        <foreign-key-table>product_tbl</foreign-key-table>
        <primary-key-table>category_tbl</primary-key-table>
        <column-map>
          <foreign-key-column>category_id</foreign-key-column>
          <key-column>category_id</key-column>
        </column-map>
      </relationship-role-map>
    </weblogic-relationship-role>
  </weblogic-rdbms-relation>




0
 
petmagdyCommented:
amjolne,

stick to Alternative 1 for now and do the modification i required im my last comment
0
 
amjolneAuthor Commented:
hi,

Sorry about the error message I copied in, it was the wrong one.

but...

The last thing you said, worked!! When I changed the name of the <relationship-role-name> from category to product, it suddenly was right (and the foreign key table, and primary key table)! how is that possible? I have another relation that has the same relationship-role-name. Does that matter?

I did the same thing with all the other relationships too. But I have one "one-to-many" that wouldn't compile. I got this error:

"WebShopEar.jar": In one to many relation: 'order-orderline', the role: 'OrderRelationshipRole' specifies a primary-key-table named 'orderline_tbl'. The EJB 'Order' does not list this table in any of its table-map elements. The primary-key-table must be a table-name in a table-map for the ejb-name 'Order'.
"WebShopEar.jar": In one to many relation: 'order-orderline', the role: 'OrderlineRelationshipRole' specifies a foreign-key-table named 'order_tbl'. The EJB 'Orderline' does not list this table in any of its table-map elements. The foreign-key-table must be a table-name in a table-map for the ejb-name 'Orderline'.


Can you use your magic wand on this one too please?





0
 
amjolneAuthor Commented:
Hi,

I didn't have to change

It all seems to compile now. No I have some ejb ql errors, but them I will not bother you with. You have been a really great help!

..now let us just hope it will deploy...


thanks again!


Asgaut
0
 
petmagdyCommented:
Good Luck in ur deployment and welcome :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 7
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now