Hi Stephan,

Stephan Wannenwetsch wrote:

Hallo Armin,

the insert error has gone, but now I have again the problem with
recieving the suppliers from an item.

I mean something like that:

Mapping changed to:
  <class-descriptor
        class="demo.myshop.model.ojb2.Supplier"
        table="OJB_SUPPLIER">

add new field for PK of Supplier ==> <field-descriptor > name="supId" > column="SUP_ID" > jdbc-type="INTEGER" > primarykey="true" ==> autoincrement="true"/>

let the FK field to BusinessPartner be anonymous
        <field-descriptor
                name="fk_bpid"
                column="FK_BPID"
                jdbc-type="INTEGER"
<==                  primarykey="true"
==>          primarykey="false"

==>          access="anonymous"
                autoincrement="false"/>

<field-descriptor
name="shippingCost"
column="SHIPPINGCOST"
jdbc-type="INTEGER"/>

<reference-descriptor
name="businessPartner"
class-ref="demo.myshop.model.ojb2.BusinessPartner">
<foreignkey field-ref="fk_bpid"/>
</reference-descriptor>

<collection-descriptor
name="items"
element-class-ref="demo.myshop.model.ojb2.SellableItem"
auto-retrieve="true"
auto-update="true"
indirection-table="OJB_SUPPLIER_ITEM">
<fk-pointing-to-this-class column="SUPPLIERID"/>
<fk-pointing-to-element-class column="ITEMID"/>
</collection-descriptor>
</class-descriptor>


I also added the fk-bpid field to the Supplier.class + getters/setters

setter+getter are only necessary if you want to use a PersistentField implementation which use bean compliant method access.

regards,
Armin

SellableItem:

package demo.myshop.model.ojb2;

import java.util.Collection;

public interface SellableItem {
        
        public String getDescription();

public Integer getItemId();

public Integer getPrice();

public void setDescription(String description);

        public void setPrice(Integer price);
        
        public Collection getSuppliers();

        public void setSuppliers(Collection supplier);
        
        public void addSupplier(Supplier supplier);
        
        public String getConcreteClass();
        
        public String getOjbConcreteClass();
        
        public void setOjbConcreteClass(String ojbConcreteClass);
}

Implemented for example in Service Contract:

package demo.myshop.model.ojb2;
import java.util.Collection;
import java.util.HashSet;

public class ServiceContract implements SellableItem {

private Integer itemId;
protected String ojbConcreteClass;
private String description;
private Integer price;
private Integer period;
private Collection suppliers;

        public ServiceContract() {
                ojbConcreteClass = ServiceContract.class.getName();
                suppliers = new HashSet();      
        }

        public String getDescription() {
                return description;
        }

        public Integer getItemId() {
                return itemId;
        }

        public Integer getPeriod() {
                return period;
        }

        public Integer getPrice() {
                return price;
        }

        public void setDescription(String description) {
                this.description = description;
        }

        public void setPeriod(Integer period) {
                this.period = period;
        }

        public void setPrice(Integer price) {
                this.price = price;
        }


public Collection getSuppliers() { return suppliers; }

        public void setSuppliers(Collection suppliers) {
                this.suppliers = suppliers;
        }
        
        public void addSupplier(Supplier supplier){
                suppliers.add(supplier);
        }
        
        public String getConcreteClass()
        {
                return ojbConcreteClass;
        }

        public String getOjbConcreteClass()
        {
                return ojbConcreteClass;
        }
        
        public void setOjbConcreteClass(String ojbConcreteClass)
        {
                this.ojbConcreteClass = ojbConcreteClass;
        }
}

Test code:

                broker.clearCache();
                System.out.println("Cache cleared"); //for debug only
                
                Criteria crit6 = new Criteria();
                crit6.addEqualTo("itemid", "23");
                System.out.println("Criterias created"); //for debug only
                
                Query query6 = QueryFactory.newQuery(ServiceContract.class, crit6);
                Collection res6 = broker.getCollectionByQuery(query6);
                System.out.println("Query executed"); //for debug only

                Iterator iter6 = res6.iterator();
                System.out.println("Iterator received"); //for debug only
                
                if (iter6.hasNext() == false) System.out.println("Item count = 0"); 
//for debug only
                else System.out.println("Item count > 0"); //for debug only
                
                while (iter6.hasNext()){
                        ServiceContract item6 = (ServiceContract) iter6.next();
                        System.out.println("Item:" + item6.getDescription() +" " + 
item6.getPrice()); //for debug only
                        Iterator iter7 = item6.getSuppliers().iterator();
                        if (iter7.hasNext() == false) System.out.println("Item count = 
0"); //for debug only
                        else System.out.println("Item count > 0"); //for debug only
                        while (iter7.hasNext()){
                                Supplier sup7 = (Supplier) iter7.next();
                                System.out.println("Supplier of Item:" + 
sup7.getBusinessPartner().getName() + " " +                                        
sup7.getShippingCosts()); //for debug only
                        }
                }
                                
                broker.close();
                System.out.println("Brooker closed"); //for debug only                

Debug message output:

Cache cleared
Criterias created
Query executed
Iterator received
Item count > 0
Item:SC2 200
Item count = 0   <== SHOULDN'T BE 0
Brooker closed
Test succeded !!!!

Testdata

OJB_ITEM:
ITEMID  CLASS_NAME                                              DESCRIPTION     PRICE  
 PERIOD  WEIGHT
21              demo.myshop.model.ojb2.Product          P1              100     <NULL> 
   1000
22              demo.myshop.model.ojb2.Product          P2              200     <NULL> 
   2000    
23              demo.myshop.model.ojb2.ServiceContract  SC2             200     20           
   <NULL>
24              demo.myshop.model.ojb2.ServiceContract  SC1             100     10           
   <NULL>

OJB_SUPPLIER:
FK_BPID SHIPPINGCOST
25              120
26              130

OJB_SUPPLIER_ITEM:
ITEMID  SUPPLIERID
21              25
22              25
22              26
23              25      
23              26
24              25

I hope you can find my error ;-)

Thanks,
Stephan

-----Original Message-----
From: Armin Waibel [mailto:[EMAIL PROTECTED] Sent: Wednesday, December 17, 2003 11:13 AM
To: OJB Users List
Subject: Re: Mapping problem



Hi again,


Stephan Wannenwetsch wrote:


Hallo Armin,

1) Nothing changed in mapping for Supplier
2) "fk_bpid" is the primary key for this class/table, but it is also the foreign key of the 1:1 relationship to BusinessPartner


I think this is problematic, because the pc object does not declare such a field (you set to anonymous). OJB try to build a Identity object for each pc object based on the PK fields and the only primarykey field in Supplier is declared anonymous ...
Recommend to introduce a PK field for Supplier and set primarykey false for fk_bpid.



3) I'm not quite sure if I have to declare a field-descriptor for "SUPPLIERID", because in the docs ("Advanced O/R") nothing is mentioned.


yes, you are right - my fault. I'm not an expert for metadata stuff ;-)


4) The SQL-Error occures just after I had changed the mapping of SellableItem, Product, ServiceContract as Wally said, before insert was not the problem. So perhaps the mapping of the Products is the problem !?


But the mapping for these classes looks fine to me.


regards,
Armin


Thanks,

Stephan

-----Original Message-----
From: Armin Waibel [mailto:[EMAIL PROTECTED]
Sent: Wednesday, December 17, 2003 10:29 AM
To: OJB Users List
Subject: Re: Mapping problem


Hi Stephan,


hmm, how does your mapping for demo.myshop.model.ojb2.Supplier
look like (nothing changed from your first post)?

From your first post

>>  <class-descriptor
>>        class="demo.myshop.model.ojb2.Supplier"
>>        table="OJB_SUPPLIER">
>>        <field-descriptor
>>                name="fk_bpid"
>>                column="FK_BPID"
>>                jdbc-type="INTEGER"
>>                primarykey="true"

Why you set primarykey 'true' for this anonymous field?

>>                autoincrement="false"
>>                access="anonymous"/>
>>                
>>        <field-descriptor
>>                name="shippingCost"
>>                column="SHIPPINGCOST"
>>                jdbc-type="INTEGER"/>
>>        
>>        <reference-descriptor
>>                name="businessPartner"
>>                class-ref="demo.myshop.model.ojb2.BusinessPartner">
>>                <foreignkey field-ref="fk_bpid"/>
>>        </reference-descriptor>
>>        
>>        <collection-descriptor
>>                name="items"
>>                element-class-ref="demo.myshop.model.ojb2.SellableItem"
>>                auto-retrieve="true"
>>                auto-update="true"
>>                indirection-table="OJB_SUPPLIER_ITEM">
>>                <fk-pointing-to-this-class column="SUPPLIERID"/>

Can't see a field-descriptor with SUPPLIERID column in this
class-descriptor.

>>                <fk-pointing-to-element-class column="ITEMID"/>
>>      </collection-descriptor>
>>
>>  </class-descriptor>

regards,
Armin

Stephan Wannenwetsch wrote:


Hi Armin,

I checked it but nothing changed. I also removed all data from the
DB-tables but nothing changed, too.

Thanks,
Stephan

-----Original Message-----
From: Armin Waibel [mailto:[EMAIL PROTECTED]
Sent: Monday, December 15, 2003 6:03 PM
To: OJB Users List
Subject: Re: Mapping problem


Hi,


maybe sequence-name "algorithm" had problems. Try to remove related
row in OJB_HL_SEQ table (name of the first found extent class table name). You can do 
a quick check using SequenceManagerInMemoryImpl in your test.
If your test pass, it's a H/L sequence manager problem.

regards,
Armin

Stephan Wannenwetsch wrote:



Hi Wally,

I'm using the following sequence-manager:

<sequence-manager 
className="org.apache.ojb.broker.util.sequence.SequenceManagerHighLowImpl">
      <attribute attribute-name="grabSize" attribute-value="20"/>
      <attribute attribute-name="autoNaming" attribute-value="true"/>
      <attribute attribute-name="globalSequenceId" attribute-value="false"/>
      <attribute attribute-name="globalSequenceStart"
attribute-value="10000"/> </sequence-manager>

Stephan


-----Original Message----- From: Gelhar, Wallace Joseph [mailto:[EMAIL PROTECTED] Sent: Monday, December 15, 2003 5:35 PM To: OJB Users List Subject: RE: Mapping problem


Hi Stephan,


Sounds like a sequence manager configuration problem. A PK must be unique within an extent hierarchy, so you cannot use an IDENTITY column for a PK. What sequence manager are you using?

Wally



-----Original Message-----
From: Stephan Wannenwetsch [mailto:[EMAIL PROTECTED]
Sent: Monday, December 15, 2003 10:16 AM
To: OJB Users List
Subject: RE: Mapping problem


Hallo Wally,


I've changed the mapping into:
<class-descriptor class="demo.myshop.model.ojb2.SellableItem">
<extent-class class-ref="demo.myshop.model.ojb2.ServiceContract" />
<extent-class class-ref="demo.myshop.model.ojb2.Product" /> </class-descriptor>


<class-descriptor
        class="demo.myshop.model.ojb2.ServiceContract"
        table="OJB_SELLABLEITEM">
                
        <field-descriptor
                name="itemId"
                column="ITEMID"
                jdbc-type="INTEGER"
                primarykey="true"
                autoincrement="true"/>     
                
        <field-descriptor
                name="ojbConcreteClass"
        column="CLASS_NAME"
        jdbc-type="VARCHAR"/>
                
        <field-descriptor
                name="description"
                column="DESCRIPTION"
                jdbc-type="VARCHAR"/>
        
        <field-descriptor
                name="price"
                column="PRICE"
                jdbc-type="INTEGER"/>
        
        <field-descriptor
                name="period"
                column="PERIOD"
                jdbc-type="INTEGER"/>

        <collection-descriptor
        name="suppliers"
        element-class-ref="demo.myshop.model.ojb2.Supplier"
        auto-retrieve="true"
        auto-update="true"
        indirection-table="OJB_SUPPLIER_ITEM">
        <fk-pointing-to-this-class column="ITEMID"/>
        <fk-pointing-to-element-class column="SUPPLIERID"/>
   </collection-descriptor>
</class-descriptor>

<class-descriptor
        class="demo.myshop.model.ojb2.Product"
        table="OJB_SELLABLEITEM">
                
        <field-descriptor
                name="itemId"
                column="ITEMID"
                jdbc-type="INTEGER"
                primarykey="true"
                autoincrement="true"/>     
                
        <field-descriptor
                name="ojbConcreteClass"
        column="CLASS_NAME"
        jdbc-type="VARCHAR"/>
                
        <field-descriptor
                name="description"
                column="DESCRIPTION"
                jdbc-type="VARCHAR"/>
        
        <field-descriptor
                name="price"
                column="PRICE"
                jdbc-type="INTEGER"/>
        
        <field-descriptor
                name="weight"
                column="WEIGHT"
                jdbc-type="INTEGER"/>
        <collection-descriptor
        name="suppliers"
        element-class-ref="demo.myshop.model.ojb2.Supplier"
        auto-retrieve="true"
        auto-update="true"
        indirection-table="OJB_SUPPLIER_ITEM">
        <fk-pointing-to-this-class column="ITEMID"/>
        <fk-pointing-to-element-class column="SUPPLIERID"/>
   </collection-descriptor>
</class-descriptor>



But now I get an error when I store a supplier (primary key - error, pk not unique in OJB_SUPPLIER_ITEM, can't insert). Transaction started


[org.apache.ojb.broker.accesslayer.JdbcAccessImpl] ERROR: SQLException during the execution of the Update SQL query (for a demo.myshop.model.ojb2.Supplier): [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Verletzung der PRIMARY KEY-Einschr�nkung 'PK_OJB_SUPPLIER_ITEM'. Ein doppelter Schl�ssel kann in das OJB_SUPPLIER_ITEM-Objekt nicht eingef�gt werden. [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Verletzung der PRIMARY KEY-Einschr�nkung 'PK_OJB_SUPPLIER_ITEM'. Ein doppelter Schl�ssel kann

i n das OJB_SUPPLIER_ITEM-Objekt nicht eingef�gt werden.




java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for 
JDBC][SQLServer]Verletzung der PRIMARY KEY-Einschr�nkung 'PK_OJB_SUPPLIER_ITEM'. Ein 
doppelter Schl�ssel kann in das OJB_SUPPLIER_ITEM-Objekt nicht eingef�gt werden.
        at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
        at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown
Source)

Some more ideas ???

Thanks,
Stephan








-----Original Message----- From: Gelhar, Wallace Joseph [mailto:[EMAIL PROTECTED] Sent: Monday, December 15, 2003 3:44 PM To: OJB Users List Subject: RE: Mapping problem


Hi Stephan,


OJB does not support class descriptor inheritance for <extent-class> elements. Try moving your "common" fields from Sellable item into each of the concrete class descriptors and making Sellable item non-concrete (it is only an interface after all) such as:

<class-descriptor class="demo.myshop.model.ojb2.SellableItem" >
<extent-class class-ref="demo.myshop.model.ojb2.ServiceContract"
/>
<extent-class class-ref="demo.myshop.model.ojb2.Product" /> </class-descriptor>


Hope this helps.

Wally

-----Original Message-----
From: Stephan Wannenwetsch [mailto:[EMAIL PROTECTED]
Sent: Monday, December 15, 2003 4:16 AM
To: [EMAIL PROTECTED]
Subject: Mapping problem


Hallo,


I've got a problem retrieving objects from a m:n relationship:

Object model:

class Supplier;
=>Collection items;

Interface SellableItem;
=>Collection suppliers;

class Product implements SellableItem;
=> Collection suppliers;

class ServiceContract implements SellableItem;
=> Collection suppliers;

Of course there are some more attributes in each class with the corresponding getter and setters.

I use an itermediate table OJB_SUPPLIER_ITEM to realise the m:n relationship.

Every thing works fine coming from the supplier side and retriving the related items of a supplier but I'm not able to retrieve the related suppliers of an item.

I hope someone can help me.

Thanks,
Stephan

Mapping:

<class-descriptor
class="demo.myshop.model.ojb2.Supplier"
table="OJB_SUPPLIER">
<field-descriptor
name="fk_bpid"
column="FK_BPID"
jdbc-type="INTEGER"
primarykey="true"
autoincrement="false"
access="anonymous"/>

<field-descriptor
name="shippingCost"
column="SHIPPINGCOST"
jdbc-type="INTEGER"/>

<reference-descriptor
name="businessPartner"
class-ref="demo.myshop.model.ojb2.BusinessPartner">
<foreignkey field-ref="fk_bpid"/>
</reference-descriptor>

<collection-descriptor
name="items"
element-class-ref="demo.myshop.model.ojb2.SellableItem"
auto-retrieve="true"
auto-update="true"
indirection-table="OJB_SUPPLIER_ITEM">
<fk-pointing-to-this-class column="SUPPLIERID"/>
<fk-pointing-to-element-class column="ITEMID"/>
</collection-descriptor>
</class-descriptor>


<class-descriptor
        class="demo.myshop.model.ojb2.SellableItem"
        table="OJB_SELLABLEITEM">
        
        <extent-class class-ref="demo.myshop.model.ojb2.ServiceContract"
/>
        <extent-class class-ref="demo.myshop.model.ojb2.Product" />
        
        <field-descriptor
                name="itemId"
                column="ITEMID"
                jdbc-type="INTEGER"
                primarykey="true"
                autoincrement="true"/>     
                
        <field-descriptor
                name="ojbConcreteClass"
        column="CLASS_NAME"
        jdbc-type="VARCHAR"/>
                
        <field-descriptor
                name="description"
                column="DESCRIPTION"
                jdbc-type="VARCHAR"/>
        
        <field-descriptor
                name="price"
                column="PRICE"
                jdbc-type="INTEGER"/>
        
        <collection-descriptor
        name="suppliers"
        element-class-ref="demo.myshop.model.ojb2.Supplier"
        auto-retrieve="true"
        auto-update="true"
        indirection-table="OJB_SUPPLIER_ITEM">
        <fk-pointing-to-this-class column="ITEMID"/>
        <fk-pointing-to-element-class column="SUPPLIERID"/>
   </collection-descriptor>
</class-descriptor>

<class-descriptor
        class="demo.myshop.model.ojb2.ServiceContract"
        table="OJB_SELLABLEITEM">
                
        <field-descriptor
                name="itemId"
                column="ITEMID"
                jdbc-type="INTEGER"
                primarykey="true"
                autoincrement="true"/>     
                
        <field-descriptor
                name="ojbConcreteClass"
        column="CLASS_NAME"
        jdbc-type="VARCHAR"/>
                
        <field-descriptor
                name="description"
                column="DESCRIPTION"
                jdbc-type="VARCHAR"/>
        
        <field-descriptor
                name="price"
                column="PRICE"
                jdbc-type="INTEGER"/>
        
        <field-descriptor
                name="period"
                column="PERIOD"
                jdbc-type="INTEGER"/>
</class-descriptor>

<class-descriptor
        class="demo.myshop.model.ojb2.Product"
        table="OJB_SELLABLEITEM">
                
        <field-descriptor
                name="itemId"
                column="ITEMID"
                jdbc-type="INTEGER"
                primarykey="true"
                autoincrement="true"/>     
                
        <field-descriptor
                name="ojbConcreteClass"
        column="CLASS_NAME"
        jdbc-type="VARCHAR"/>
                
        <field-descriptor
                name="description"
                column="DESCRIPTION"
                jdbc-type="VARCHAR"/>
        
        <field-descriptor
                name="price"
                column="PRICE"
                jdbc-type="INTEGER"/>
        
        <field-descriptor
                name="weight"
                column="WEIGHT"
                jdbc-type="INTEGER"/>
</class-descriptor>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]







---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]







---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]







---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]






--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to