Re: 1 to N mapping problems

2003-05-28 Thread kraemer
On Wed, May 28, 2003 at 08:32:16PM +0800, Stephen Ting wrote:
> Hi Jens,
> 
> You're right. That's means this setup won't work. If the Log and
> LogReceiveItem persisting to different table then it should be ok.
> Haven't try, just guessing. 
> 
> How to configure the repository to enable two classes(Log and
> LogReceiveItem, if LogReceiveItem inherit from Log) to persist to the
> same table with LOG_ID as primary key?   

That is easy, just define both classes as usual, with all their
attributes, and map them to the same table. Storing items of both
classes to the LOG-Table should work now without problems.

The problem is when fetching items from db, say for class LogReceiveItem,
you will get instances of model.LogReceiveItem with the data from all 
entries in table LOG, including those you inserted as model.Log, since
OJB does not store the information about the class the data came from at 
storing time. 
You would have to prevent this by using appropriate filter criteria 
by yourself, i.e. look where the attributes which only LogReceiveItem
has are null or not.

I think you should separate the data into two tables and define a 1:1
relationship from LOG_RECEIVE_ITEM to LOG. Imho this would make the
model clearer and easier to understand.


Regards,
Jens

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



RE: 1 to N mapping problems

2003-05-28 Thread Stephen Ting
Hi Jens,

You're right. That's means this setup won't work. If the Log and
LogReceiveItem persisting to different table then it should be ok.
Haven't try, just guessing. 

How to configure the repository to enable two classes(Log and
LogReceiveItem, if LogReceiveItem inherit from Log) to persist to the
same table with LOG_ID as primary key?   

Regards,
Stephen



>-Original Message-
>From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 
>Sent: 28 May 2003 17:52
>To: OJB Users List; Stephen Ting Tiew Ung
>Subject: Re: 1 to N mapping problems
>
>
>Hi Stephen,
>
>see comments below...
>
>On Wed, May 28, 2003 at 05:13:28PM +0800, Stephen Ting wrote:
>> The relationship of the classes are
>> 
>> LogReceiving > 1:N LogReceivingItem ---> Log
>> 
>> The LogReceivingItem object actually reference to Log 
>object. If I don't
>> reference it to Log object. It's run perfectly. Whereas if I 
>include the
>>  to Log object it will failed because 
>of multiple
>> insert at LOG table with the same primary key. From the SQL generated
>> trace I found that OJB generate the following SQL command.
>> 
>> 1. INSERT INTO LOG_RECEIVING (with attributes of LogReceiving)
>> 2. INSERT INTO LOG (with attributes of LOG)
>> 3. UPDATE LOG_RECEIVING  (with attributes of LogReceiving)
>> 4. UPDATE LOG (with attributes of LogReceiving)
>> 5. INSERT LOG (with attributes of LogReceivingItem)
>> 
>> The java code I used to persist the LogReceiving object are as follow
>> 
>> Implementation odmg = OJB.getInstance();
>> Database db = odmg.newDatabase();
>> try{ 
>>  LogReceiving document = createDoc();
>>  db.open("site1", Database.OPEN_READ_WRITE);
>>  Transaction tx = odmg.newTransaction();
>>  tx.begin();
>>  tx.lock(document, Database.OPEN_READ_WRITE);
>>  tx.commit();
>>  
>> }catch(ODMGException e){
>>  throw new BDException(e.getMessage());  
>> }
>> 
>> >  class="model.Log" table="LOG" >
>>  >  name="logId"
>>  column="LOG_ID"
>>  jdbc-type="INTEGER" 
>>  primarykey="true" 
>>  autoincrement="true" 
>>  />
>> 
>> 
>> >  class="model.LogReceiving"
>>  table="LOG_RECEIVING">
>>  >  name="logReceivingId"
>>  column="LOG_RECEIVING_ID"
>>  jdbc-type="INTEGER"
>>  primarykey="true" 
>>  autoincrement="true" 
>>  />
>> 
>>  >  name="lineItem"
>>  element-class-ref="model.LogReceivingItem"
>>  auto-retrieve="true"
>>  auto-update="false"
>>  auto-delete="true">
>>  
>>  
>> 
>> 
>> >  class="model.LogReceivingItem" table="LOG">
>>  >  name="logReceivingId"
>>  column="LOG_RECEIVING_ID"
>>  jdbc-type="INTEGER" 
>>  />
>>  >  name="logId"
>>  column="LOG_ID"
>>  jdbc-type="INTEGER"
>>  /> 
>
>I think you should declare logId as primary key here, too (just as you
>did for model.Log above). 
>
>>  >  name="logReceiving"
>>  class-ref="model.LogReceiving"
>>  auto-retrieve="true"
>>  auto-update="true"
>>  auto-delete="true">
>>  
>>   
>>  >  name="log"
>>  class-ref="model.Log"
>>  auto-retrieve="true"
>>  auto-update="true"
>>  auto-delete="true">
>>  
>>   
>
>Imho this reference won't work.
>Imagine you create an instance of LogReceivingItem associated with an
>instance of Log. 
>log.logId then is equal to logReceivingItem.logId.
>Now what happens when you try to insert both items into db ? Two
>insert-Statements are generated, both for table LOG, and both with the
>same value for LOG_ID, which I suppose to be the primary key in table
>LOG.
>
>
>hth,
>Jens
>
>-
>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]



Re: 1 to N mapping problems

2003-05-28 Thread kraemer
Hi Stephen,

see comments below...

On Wed, May 28, 2003 at 05:13:28PM +0800, Stephen Ting wrote:
> The relationship of the classes are
> 
> LogReceiving > 1:N LogReceivingItem ---> Log
> 
> The LogReceivingItem object actually reference to Log object. If I don't
> reference it to Log object. It's run perfectly. Whereas if I include the
>  to Log object it will failed because of multiple
> insert at LOG table with the same primary key. From the SQL generated
> trace I found that OJB generate the following SQL command.
> 
> 1. INSERT INTO LOG_RECEIVING (with attributes of LogReceiving)
> 2. INSERT INTO LOG (with attributes of LOG)
> 3. UPDATE LOG_RECEIVING  (with attributes of LogReceiving)
> 4. UPDATE LOG (with attributes of LogReceiving)
> 5. INSERT LOG (with attributes of LogReceivingItem)
> 
> The java code I used to persist the LogReceiving object are as follow
> 
> Implementation odmg = OJB.getInstance();
> Database db = odmg.newDatabase();
> try{  
>   LogReceiving document = createDoc();
>   db.open("site1", Database.OPEN_READ_WRITE);
>   Transaction tx = odmg.newTransaction();
>   tx.begin();
>   tx.lock(document, Database.OPEN_READ_WRITE);
>   tx.commit();
>   
> }catch(ODMGException e){
>   throw new BDException(e.getMessage());  
> }
> 
>class="model.Log" table="LOG" >
>  name="logId"
>   column="LOG_ID"
>   jdbc-type="INTEGER" 
>   primarykey="true" 
>   autoincrement="true" 
>   />
> 
> 
>class="model.LogReceiving"
>   table="LOG_RECEIVING">
>  name="logReceivingId"
>   column="LOG_RECEIVING_ID"
>   jdbc-type="INTEGER"
>   primarykey="true" 
>   autoincrement="true" 
>   />
> 
>  name="lineItem"
>   element-class-ref="model.LogReceivingItem"
>   auto-retrieve="true"
>   auto-update="false"
>   auto-delete="true">
>   
>   
> 
> 
>class="model.LogReceivingItem" table="LOG">
>  name="logReceivingId"
>   column="LOG_RECEIVING_ID"
>   jdbc-type="INTEGER" 
>   />
>  name="logId"
>   column="LOG_ID"
>   jdbc-type="INTEGER"
>   /> 

I think you should declare logId as primary key here, too (just as you
did for model.Log above). 

>  name="logReceiving"
>   class-ref="model.LogReceiving"
>   auto-retrieve="true"
>   auto-update="true"
>   auto-delete="true">
>   
>
>  name="log"
>   class-ref="model.Log"
>   auto-retrieve="true"
>   auto-update="true"
>   auto-delete="true">
>   
>

Imho this reference won't work.
Imagine you create an instance of LogReceivingItem associated with an
instance of Log. 
log.logId then is equal to logReceivingItem.logId.
Now what happens when you try to insert both items into db ? Two
insert-Statements are generated, both for table LOG, and both with the
same value for LOG_ID, which I suppose to be the primary key in table
LOG.


hth,
Jens

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



1 to N mapping problems

2003-05-28 Thread Stephen Ting
The relationship of the classes are

LogReceiving > 1:N LogReceivingItem ---> Log

The LogReceivingItem object actually reference to Log object. If I don't
reference it to Log object. It's run perfectly. Whereas if I include the
 to Log object it will failed because of multiple
insert at LOG table with the same primary key. From the SQL generated
trace I found that OJB generate the following SQL command.

1. INSERT INTO LOG_RECEIVING (with attributes of LogReceiving)
2. INSERT INTO LOG (with attributes of LOG)
3. UPDATE LOG_RECEIVING  (with attributes of LogReceiving)
4. UPDATE LOG (with attributes of LogReceiving)
5. INSERT LOG (with attributes of LogReceivingItem)

The java code I used to persist the LogReceiving object are as follow

Implementation odmg = OJB.getInstance();
Database db = odmg.newDatabase();
try{
LogReceiving document = createDoc();
db.open("site1", Database.OPEN_READ_WRITE);
Transaction tx = odmg.newTransaction();
tx.begin();
tx.lock(document, Database.OPEN_READ_WRITE);
tx.commit();

}catch(ODMGException e){
throw new BDException(e.getMessage());  
}















 


 


 


Any helps are very much appreciated..

Thanks

Regards,
Stephen


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