You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Mark Spritzler <by...@yahoo.com> on 2004/05/07 23:59:42 UTC

RE: Child Table insert at same time as Parent and Auto generated PK

Yes, I think it is backwards myself.
 
I made the change and the Parent record got inserted. This is my first inserted record so far using OJB, after over two weeks of trying.
 
Unfortunately, the child record never got inserted.
 
Back after 1 hour.
 
Here is what I did to make it insert into both tables. Here is my insert method of my DAO object
 
public void insert(ValueObject insertRecord) throws DataAccessException {
        log.info("************Entering the TourDAO.insert***************");
        PersistenceBroker broker = null;
        try {
            broker = ServiceLocator.getInstance().findBroker();
            TourVO tour = (TourVO)insertRecord;
            AssumptionVO assumptionVO = tour.getAssumptions();
            broker.beginTransaction();
            broker.store(tour);
            assumptionVO.setTour(tour);
            broker.store(assumptionVO);            
            broker.commitTransaction();
      .......
     
Of course the other remaining code is just the catch and finally parts.
 
I thought that OJB was set up that it would have automatically saves the child record, instead of me having to call store() a second time on the broker.
 
But I am happy that I got the records inserted, I can finally move on in my project to the next steps. As I had said I have been stuck trying to get OJB to work for over two weeks, and this is a simple project I wrote.
 
Thanks for you help.
 
Mark

"McCaffrey, John G." <Jo...@kraft.com> wrote:

yes, (my understanding) if you have a 1-1 relationship, but there is a FK
constraint that indicates a parent > child relationship, then OJB wants the
Child Mapping to contain a reference to the parent. 

I think the basic rule is like this
mapping for Object A
contains 
ref to B
ref to C
collection ref to D

SQL queries will go like this
insert B
insert C
insert A
insert D

That was a suprise to me, and I wish it wasn't like that. (I have asked if I
am just not understanding this, but i haven't gotten a response that says I
am wrong yet)

What I don't like about it is that if you have a 1-1 child relationship you
have to invert you mapping to make OJB happy, but it might not make sense
for yuo object

ex (this is what I would like to do):
company obj mapping (parent)
contains
primary manager obj (only one reference and this is truly a child of
company)
primary address obj (same deal)

but this would fail because OJB will try to insert the manager and address
first
instead I think you have to do this:

Manager
contains 
a company
(I don't even know what will happen with the address)

So if I am understanding this, it really makes it hard for you to use
composite VOs when you have 1-1 parent-child relationships.

any thoughts? (someone please tell me that I am wrong, and that there is a
better way to do this)



-----Original Message-----
From: Mark Spritzler [mailto:bytor99999@yahoo.com]
Sent: Friday, May 07, 2004 4:21 PM
To: OJB Users List
Subject: RE: Child Table insert at same time as Parent and Auto
generated PK


Hi John, thanks. Actually I have already added the sequence manager to the
jdbc-connection-descriptor, and also the settings for the primary key.

So are you saying that my reference-descriptor in the Parent table needs to
be removed and instead add it to my child table? If not then, what do you
mean. :)

Thanks

Mark

"McCaffrey, John G." wrote:
Search the archives for SequenceManger and Idenity threads.

Here is what I needed to do:
add a sequence manager to my jdbc-connection-descriptor
className="org.apache.ojb.broker.util.sequence.SequenceManagerNativeImpl">


Make sure these were set on my primary key
primarykey="true"
autoincrement="true"
access="readonly"

Then I had to flip my mapping because I had
Parent > contains a reference to a single Child (not a collection)
but OJB wants Child > contains reference to Parent

HTH

-----Original Message-----
From: Mark Spritzler [mailto:bytor99999@yahoo.com]
Sent: Friday, May 07, 2004 4:05 PM
To: ojb-user@db.apache.org
Subject: Child Table insert at same time as Parent and Auto generated PK


I am using MySql.

OK. I have a Parent-Child relationship which is 1:1 relationship. I am
inserting into each table at the same time. Obviously, the Parent gets
inserted first. But the Primary Key is auto-generated by MySQL in an
auto-generated field. So when the child record gets inserted, I need that
newly created ID to be in the child record. But it isn't getting it, and the
ID in the child is Null, so the Database returns an error for it being Null.

Does anyone know a way to fix this problem? Putting the child fields into
the Parent table is not a possibility and wouldn't be a good db design
decision.

Thanks everyone

Mark


---------------------------------
Do you Yahoo!?
Win a $20,000 Career Makeover at Yahoo! HotJobs 

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org



Franziska says, "Keep eating healthy"

she also says that the above line is not actually gramatically correct. 

She wants you all to know that healthy cannot be there; it really should be
an adverb like healthfully.


---------------------------------
Do you Yahoo!?
Win a $20,000 Career Makeover at Yahoo! HotJobs 

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org

		
---------------------------------
Do you Yahoo!?
Win a $20,000 Career Makeover at Yahoo! HotJobs 

Parent Table insert

Posted by "Martin I. Levi" <ml...@labtie.mmt.upc.es>.
Hi Armin,

There's one thing which is not right clear to me.
If I have one object A which has 1:1 references to B,C,D
which have many 1:1 references, and I want to insert a
new A object which makes reference to a B, C, D objects 
(Which already exist). I mean, I just want to insert an A object which
refers to other existent objects.

What I try to do (and doesn't work) is:

ClassA a=new A();
ClassB b=getTheBObjectIWant(); //this works, I have my B Object!
a.setB(b);
service.Insert(a);


/**being service.Insert:**/

public void Insert(ClassA a)
{
	odmg = OJB.getInstance();
	db = odmg.newDatabase();
	Transaction tx = odmg.newTransaction();
        ComponentBO obj=(ComponentBO)newItem;
        tx.lock(obj, Transaction.WRITE);
}

/**and my repository_user.xml has**/
            auto-update="false"
            auto-delete="false"
            auto-retrieve="true"
/*in every single reference-descriptor*/


Am I doing this Insert right or am i loosing something?

I get the following exception:

etc. etc. etc...

"Caused by: java.sql.SQLException: ERROR:  Cannot insert a duplicate key
into unique index compmake_name_key"

being compmake an object from the third group (not referred directly
from A but from B)

Any help is welcome! :)


Greets,


Martin Ivan Levi

Universistat Politecnica de Catalunya
Centre Tecnologic de Transferencia de Calor



On Mon, 2004-05-17 at 14:28, Armin Waibel wrote:
> Hi Martin,
> 
> Martin I. Levi wrote:
> 
> > Oops!
> > 
> > I already found where the auto-xxx settings should be placed, sorry!
> > Anyway I still think you should put all these auto-xxx information
> > together.
> > 
> 
> The auto-xxx settings differ in each type of relation (1:1, 1:n, m:n) 
> and belongs only to the associated relation, thus we describe these 
> settings in sub-sections of relation section. I think to put all 
> auto-xxx setting together will be complex.
> 
> The auto-retrieve setting is equal in all relations and described together
> http://db.apache.org/ojb/tutorial3.html#auto-retrieve%20setting
> 
> An introduction to auto-xxx setting can be found her
> http://db.apache.org/ojb/tutorial3.html#Setting%20Load,%20Update,%20and%20Delete%20Cascading
> 
> regards,
> Armin



---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Child Table insert at same time as Parent and Auto generated PK

Posted by Armin Waibel <ar...@apache.org>.
Hi Martin,

Martin I. Levi wrote:

> Oops!
> 
> I already found where the auto-xxx settings should be placed, sorry!
> Anyway I still think you should put all these auto-xxx information
> together.
> 

The auto-xxx settings differ in each type of relation (1:1, 1:n, m:n) 
and belongs only to the associated relation, thus we describe these 
settings in sub-sections of relation section. I think to put all 
auto-xxx setting together will be complex.

The auto-retrieve setting is equal in all relations and described together
http://db.apache.org/ojb/tutorial3.html#auto-retrieve%20setting

An introduction to auto-xxx setting can be found her
http://db.apache.org/ojb/tutorial3.html#Setting%20Load,%20Update,%20and%20Delete%20Cascading

regards,
Armin

> Greets,
> 
> 
> Martin Ivan Levi
> 
> Universistat Politecnica de Catalunya
> Centre Tecnologic de Transferencia de Calor
> 
> On Sat, 2004-05-08 at 03:13, Armin Waibel wrote:
> 
>>Hi Mark,
>>
>>Mark Spritzler wrote:
>>
>>
>>>Yes, I think it is backwards myself.
>>> 
>>>I made the change and the Parent record got inserted. This is my first inserted record so far using
>>
>>  OJB, after over two weeks of trying.
>>
>>> 
>>
>>wow! Thanks for your patience! Hope we can improve documentation to 
>>prevent user from such pain.
>>
>>Using Identity columns is a special case, but nevertheless OJB should be 
>>able to handle this too (and it works, see test case 
>>...broker.sequence.NativeIdentifierTest in test suite).
>>
>>I will try to describe how OJB handles relations. Say we have a base 
>>class A with a 1:1 reference to B and a 1:n reference to C.
>>
>>If you set auto-update 'object/true' in the reference- and 
>>collection-descriptor OJB will handle an insert of an A with populated 
>>references automatic
>>...
>>A = new A
>>A.setB(new B)
>>List listOfC = getC's
>>A.setAllCs(listOfC)
>>broker.store(A)
>>...
>>That's all.
>>
>>If you set auto-update 'none/false' you have to do all object insert by 
>>hand and have to set all FK (not possible when anonymous key was used 
>>for 1:1 FK) or better use linking (see
>>http://db.apache.org/ojb/tutorial3.html#Link%20references)
>>
>>Detailed info about the auto-xxx settings can be found in the reference 
>>sections
>>(e.g. http://db.apache.org/ojb/tutorial3.html#Link%20references)
>>
>>To insert objects with references by hand the following order is mandatory:
>>
>>First insert all 1:1 referenced objects (insert B)
>>link A (set FK to B)
>>Second insert the main object (insert A)
>>link C's (set FK to A)
>>Third insert all 1:n collections (insert all C's)
>>
>>On delete it's vice versa.
>>
>>Hope this information is helpfully.
>>Any hints how to improve documentation are welcome.
>>
>>
>>regards,
>>Armin
>>
>>
>>
>>>Unfortunately, the child record never got inserted.
>>> 
>>>Back after 1 hour.
>>> 
>>>Here is what I did to make it insert into both tables. Here is my insert method of my DAO object
>>> 
>>>public void insert(ValueObject insertRecord) throws DataAccessException {
>>>        log.info("************Entering the TourDAO.insert***************");
>>>        PersistenceBroker broker = null;
>>>        try {
>>>            broker = ServiceLocator.getInstance().findBroker();
>>>            TourVO tour = (TourVO)insertRecord;
>>>            AssumptionVO assumptionVO = tour.getAssumptions();
>>>            broker.beginTransaction();
>>>            broker.store(tour);
>>>            assumptionVO.setTour(tour);
>>>            broker.store(assumptionVO);            
>>>            broker.commitTransaction();
>>>      .......
>>>     
>>>Of course the other remaining code is just the catch and finally parts.
>>> 
>>>I thought that OJB was set up that it would have automatically saves the child record, instead of me having to call store() a second time on the broker.
>>> 
>>>But I am happy that I got the records inserted, I can finally move on in my project to the next steps. As I had said I have been stuck trying to get OJB to work for over two weeks, and this is a simple project I wrote.
>>> 
>>>Thanks for you help.
>>> 
>>>Mark
>>>
>>>"McCaffrey, John G." <Jo...@kraft.com> wrote:
>>>
>>>yes, (my understanding) if you have a 1-1 relationship, but there is a FK
>>>constraint that indicates a parent > child relationship, then OJB wants the
>>>Child Mapping to contain a reference to the parent. 
>>>
>>>I think the basic rule is like this
>>>mapping for Object A
>>>contains 
>>>ref to B
>>>ref to C
>>>collection ref to D
>>>
>>>SQL queries will go like this
>>>insert B
>>>insert C
>>>insert A
>>>insert D
>>>
>>>That was a suprise to me, and I wish it wasn't like that. (I have asked if I
>>>am just not understanding this, but i haven't gotten a response that says I
>>>am wrong yet)
>>>
>>>What I don't like about it is that if you have a 1-1 child relationship you
>>>have to invert you mapping to make OJB happy, but it might not make sense
>>>for yuo object
>>>
>>>ex (this is what I would like to do):
>>>company obj mapping (parent)
>>>contains
>>>primary manager obj (only one reference and this is truly a child of
>>>company)
>>>primary address obj (same deal)
>>>
>>>but this would fail because OJB will try to insert the manager and address
>>>first
>>>instead I think you have to do this:
>>>
>>>Manager
>>>contains 
>>>a company
>>>(I don't even know what will happen with the address)
>>>
>>>So if I am understanding this, it really makes it hard for you to use
>>>composite VOs when you have 1-1 parent-child relationships.
>>>
>>>any thoughts? (someone please tell me that I am wrong, and that there is a
>>>better way to do this)
>>>
>>>
>>>
>>>-----Original Message-----
>>>From: Mark Spritzler [mailto:bytor99999@yahoo.com]
>>>Sent: Friday, May 07, 2004 4:21 PM
>>>To: OJB Users List
>>>Subject: RE: Child Table insert at same time as Parent and Auto
>>>generated PK
>>>
>>>
>>>Hi John, thanks. Actually I have already added the sequence manager to the
>>>jdbc-connection-descriptor, and also the settings for the primary key.
>>>
>>>So are you saying that my reference-descriptor in the Parent table needs to
>>>be removed and instead add it to my child table? If not then, what do you
>>>mean. :)
>>>
>>>Thanks
>>>
>>>Mark
>>>
>>>"McCaffrey, John G." wrote:
>>>Search the archives for SequenceManger and Idenity threads.
>>>
>>>Here is what I needed to do:
>>>add a sequence manager to my jdbc-connection-descriptor
>>>className="org.apache.ojb.broker.util.sequence.SequenceManagerNativeImpl">
>>>
>>>
>>>Make sure these were set on my primary key
>>>primarykey="true"
>>>autoincrement="true"
>>>access="readonly"
>>>
>>>Then I had to flip my mapping because I had
>>>Parent > contains a reference to a single Child (not a collection)
>>>but OJB wants Child > contains reference to Parent
>>>
>>>HTH
>>>
>>>-----Original Message-----
>>>From: Mark Spritzler [mailto:bytor99999@yahoo.com]
>>>Sent: Friday, May 07, 2004 4:05 PM
>>>To: ojb-user@db.apache.org
>>>Subject: Child Table insert at same time as Parent and Auto generated PK
>>>
>>>
>>>I am using MySql.
>>>
>>>OK. I have a Parent-Child relationship which is 1:1 relationship. I am
>>>inserting into each table at the same time. Obviously, the Parent gets
>>>inserted first. But the Primary Key is auto-generated by MySQL in an
>>>auto-generated field. So when the child record gets inserted, I need that
>>>newly created ID to be in the child record. But it isn't getting it, and the
>>>ID in the child is Null, so the Database returns an error for it being Null.
>>>
>>>Does anyone know a way to fix this problem? Putting the child fields into
>>>the Parent table is not a possibility and wouldn't be a good db design
>>>decision.
>>>
>>>Thanks everyone
>>>
>>>Mark
>>>
>>>
>>>---------------------------------
>>>Do you Yahoo!?
>>>Win a $20,000 Career Makeover at Yahoo! HotJobs 
>>>
>>>---------------------------------------------------------------------
>>>To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
>>>For additional commands, e-mail: ojb-user-help@db.apache.org
>>>
>>>
>>>
>>>Franziska says, "Keep eating healthy"
>>>
>>>she also says that the above line is not actually gramatically correct. 
>>>
>>>She wants you all to know that healthy cannot be there; it really should be
>>>an adverb like healthfully.
>>>
>>>
>>>---------------------------------
>>>Do you Yahoo!?
>>>Win a $20,000 Career Makeover at Yahoo! HotJobs 
>>>
>>>---------------------------------------------------------------------
>>>To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
>>>For additional commands, e-mail: ojb-user-help@db.apache.org
>>>
>>>		
>>>---------------------------------
>>>Do you Yahoo!?
>>>Win a $20,000 Career Makeover at Yahoo! HotJobs 
>>
>>---------------------------------------------------------------------
>>To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
>>For additional commands, e-mail: ojb-user-help@db.apache.org
>>
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 
> 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Child Table insert at same time as Parent and Auto generated PK

Posted by "Martin I. Levi" <ml...@labtie.mmt.upc.es>.
Oops!

I already found where the auto-xxx settings should be placed, sorry!
Anyway I still think you should put all these auto-xxx information
together.

Greets,


Martin Ivan Levi

Universistat Politecnica de Catalunya
Centre Tecnologic de Transferencia de Calor

On Sat, 2004-05-08 at 03:13, Armin Waibel wrote:
> Hi Mark,
> 
> Mark Spritzler wrote:
> 
> > Yes, I think it is backwards myself.
> >  
> > I made the change and the Parent record got inserted. This is my first inserted record so far using
>   OJB, after over two weeks of trying.
> >  
> 
> wow! Thanks for your patience! Hope we can improve documentation to 
> prevent user from such pain.
> 
> Using Identity columns is a special case, but nevertheless OJB should be 
> able to handle this too (and it works, see test case 
> ...broker.sequence.NativeIdentifierTest in test suite).
> 
> I will try to describe how OJB handles relations. Say we have a base 
> class A with a 1:1 reference to B and a 1:n reference to C.
> 
> If you set auto-update 'object/true' in the reference- and 
> collection-descriptor OJB will handle an insert of an A with populated 
> references automatic
> ...
> A = new A
> A.setB(new B)
> List listOfC = getC's
> A.setAllCs(listOfC)
> broker.store(A)
> ...
> That's all.
> 
> If you set auto-update 'none/false' you have to do all object insert by 
> hand and have to set all FK (not possible when anonymous key was used 
> for 1:1 FK) or better use linking (see
> http://db.apache.org/ojb/tutorial3.html#Link%20references)
> 
> Detailed info about the auto-xxx settings can be found in the reference 
> sections
> (e.g. http://db.apache.org/ojb/tutorial3.html#Link%20references)
> 
> To insert objects with references by hand the following order is mandatory:
> 
> First insert all 1:1 referenced objects (insert B)
> link A (set FK to B)
> Second insert the main object (insert A)
> link C's (set FK to A)
> Third insert all 1:n collections (insert all C's)
> 
> On delete it's vice versa.
> 
> Hope this information is helpfully.
> Any hints how to improve documentation are welcome.
> 
> 
> regards,
> Armin
> 
> 
> > Unfortunately, the child record never got inserted.
> >  
> > Back after 1 hour.
> >  
> > Here is what I did to make it insert into both tables. Here is my insert method of my DAO object
> >  
> > public void insert(ValueObject insertRecord) throws DataAccessException {
> >         log.info("************Entering the TourDAO.insert***************");
> >         PersistenceBroker broker = null;
> >         try {
> >             broker = ServiceLocator.getInstance().findBroker();
> >             TourVO tour = (TourVO)insertRecord;
> >             AssumptionVO assumptionVO = tour.getAssumptions();
> >             broker.beginTransaction();
> >             broker.store(tour);
> >             assumptionVO.setTour(tour);
> >             broker.store(assumptionVO);            
> >             broker.commitTransaction();
> >       .......
> >      
> > Of course the other remaining code is just the catch and finally parts.
> >  
> > I thought that OJB was set up that it would have automatically saves the child record, instead of me having to call store() a second time on the broker.
> >  
> > But I am happy that I got the records inserted, I can finally move on in my project to the next steps. As I had said I have been stuck trying to get OJB to work for over two weeks, and this is a simple project I wrote.
> >  
> > Thanks for you help.
> >  
> > Mark
> > 
> > "McCaffrey, John G." <Jo...@kraft.com> wrote:
> > 
> > yes, (my understanding) if you have a 1-1 relationship, but there is a FK
> > constraint that indicates a parent > child relationship, then OJB wants the
> > Child Mapping to contain a reference to the parent. 
> > 
> > I think the basic rule is like this
> > mapping for Object A
> > contains 
> > ref to B
> > ref to C
> > collection ref to D
> > 
> > SQL queries will go like this
> > insert B
> > insert C
> > insert A
> > insert D
> > 
> > That was a suprise to me, and I wish it wasn't like that. (I have asked if I
> > am just not understanding this, but i haven't gotten a response that says I
> > am wrong yet)
> > 
> > What I don't like about it is that if you have a 1-1 child relationship you
> > have to invert you mapping to make OJB happy, but it might not make sense
> > for yuo object
> > 
> > ex (this is what I would like to do):
> > company obj mapping (parent)
> > contains
> > primary manager obj (only one reference and this is truly a child of
> > company)
> > primary address obj (same deal)
> > 
> > but this would fail because OJB will try to insert the manager and address
> > first
> > instead I think you have to do this:
> > 
> > Manager
> > contains 
> > a company
> > (I don't even know what will happen with the address)
> > 
> > So if I am understanding this, it really makes it hard for you to use
> > composite VOs when you have 1-1 parent-child relationships.
> > 
> > any thoughts? (someone please tell me that I am wrong, and that there is a
> > better way to do this)
> > 
> > 
> > 
> > -----Original Message-----
> > From: Mark Spritzler [mailto:bytor99999@yahoo.com]
> > Sent: Friday, May 07, 2004 4:21 PM
> > To: OJB Users List
> > Subject: RE: Child Table insert at same time as Parent and Auto
> > generated PK
> > 
> > 
> > Hi John, thanks. Actually I have already added the sequence manager to the
> > jdbc-connection-descriptor, and also the settings for the primary key.
> > 
> > So are you saying that my reference-descriptor in the Parent table needs to
> > be removed and instead add it to my child table? If not then, what do you
> > mean. :)
> > 
> > Thanks
> > 
> > Mark
> > 
> > "McCaffrey, John G." wrote:
> > Search the archives for SequenceManger and Idenity threads.
> > 
> > Here is what I needed to do:
> > add a sequence manager to my jdbc-connection-descriptor
> > className="org.apache.ojb.broker.util.sequence.SequenceManagerNativeImpl">
> > 
> > 
> > Make sure these were set on my primary key
> > primarykey="true"
> > autoincrement="true"
> > access="readonly"
> > 
> > Then I had to flip my mapping because I had
> > Parent > contains a reference to a single Child (not a collection)
> > but OJB wants Child > contains reference to Parent
> > 
> > HTH
> > 
> > -----Original Message-----
> > From: Mark Spritzler [mailto:bytor99999@yahoo.com]
> > Sent: Friday, May 07, 2004 4:05 PM
> > To: ojb-user@db.apache.org
> > Subject: Child Table insert at same time as Parent and Auto generated PK
> > 
> > 
> > I am using MySql.
> > 
> > OK. I have a Parent-Child relationship which is 1:1 relationship. I am
> > inserting into each table at the same time. Obviously, the Parent gets
> > inserted first. But the Primary Key is auto-generated by MySQL in an
> > auto-generated field. So when the child record gets inserted, I need that
> > newly created ID to be in the child record. But it isn't getting it, and the
> > ID in the child is Null, so the Database returns an error for it being Null.
> > 
> > Does anyone know a way to fix this problem? Putting the child fields into
> > the Parent table is not a possibility and wouldn't be a good db design
> > decision.
> > 
> > Thanks everyone
> > 
> > Mark
> > 
> > 
> > ---------------------------------
> > Do you Yahoo!?
> > Win a $20,000 Career Makeover at Yahoo! HotJobs 
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> > For additional commands, e-mail: ojb-user-help@db.apache.org
> > 
> > 
> > 
> > Franziska says, "Keep eating healthy"
> > 
> > she also says that the above line is not actually gramatically correct. 
> > 
> > She wants you all to know that healthy cannot be there; it really should be
> > an adverb like healthfully.
> > 
> > 
> > ---------------------------------
> > Do you Yahoo!?
> > Win a $20,000 Career Makeover at Yahoo! HotJobs 
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> > For additional commands, e-mail: ojb-user-help@db.apache.org
> > 
> > 		
> > ---------------------------------
> > Do you Yahoo!?
> > Win a $20,000 Career Makeover at Yahoo! HotJobs 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Child Table insert at same time as Parent and Auto generated PK

Posted by "Martin I. Levi" <ml...@labtie.mmt.upc.es>.
Hi

As a hint to improve documentation:

When reading the 1:1 auto-xxx settings
(http://db.apache.org/ojb/tutorial3.html) i found that there's no place
explaining where exactly in the repository_user.xml these settings
should be specified (I suppose this place is the reference descriptor,
am i right?)

On the other cases (1:n and n:m) you can figure out from the examples
that they should be placed inside the collection-descriptor but in 1:1
relations there's no collection-descriptor and no example either.

Hope this helps anyway!

Greets,


Martin Ivan Levi

Universistat Politecnica de Catalunya
Centre Tecnologic de Transferencia de Calor


On Sat, 2004-05-08 at 03:13, Armin Waibel wrote:
> If you set auto-update 'object/true' in the reference- and 
> collection-descriptor OJB will handle an insert of an A with populated 
> references automatic
> ...
> A = new A
> A.setB(new B)
> List listOfC = getC's
> A.setAllCs(listOfC)
> broker.store(A)
> ...
> That's all.
> 
> Any hints how to improve documentation are welcome.
> 
> 
> regards,
> Armin
> 



---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org


Re: Child Table insert at same time as Parent and Auto generated PK

Posted by Armin Waibel <ar...@apache.org>.
Hi Mark,

Mark Spritzler wrote:

> Yes, I think it is backwards myself.
>  
> I made the change and the Parent record got inserted. This is my first inserted record so far using
  OJB, after over two weeks of trying.
>  

wow! Thanks for your patience! Hope we can improve documentation to 
prevent user from such pain.

Using Identity columns is a special case, but nevertheless OJB should be 
able to handle this too (and it works, see test case 
...broker.sequence.NativeIdentifierTest in test suite).

I will try to describe how OJB handles relations. Say we have a base 
class A with a 1:1 reference to B and a 1:n reference to C.

If you set auto-update 'object/true' in the reference- and 
collection-descriptor OJB will handle an insert of an A with populated 
references automatic
...
A = new A
A.setB(new B)
List listOfC = getC's
A.setAllCs(listOfC)
broker.store(A)
...
That's all.

If you set auto-update 'none/false' you have to do all object insert by 
hand and have to set all FK (not possible when anonymous key was used 
for 1:1 FK) or better use linking (see
http://db.apache.org/ojb/tutorial3.html#Link%20references)

Detailed info about the auto-xxx settings can be found in the reference 
sections
(e.g. http://db.apache.org/ojb/tutorial3.html#Link%20references)

To insert objects with references by hand the following order is mandatory:

First insert all 1:1 referenced objects (insert B)
link A (set FK to B)
Second insert the main object (insert A)
link C's (set FK to A)
Third insert all 1:n collections (insert all C's)

On delete it's vice versa.

Hope this information is helpfully.
Any hints how to improve documentation are welcome.


regards,
Armin


> Unfortunately, the child record never got inserted.
>  
> Back after 1 hour.
>  
> Here is what I did to make it insert into both tables. Here is my insert method of my DAO object
>  
> public void insert(ValueObject insertRecord) throws DataAccessException {
>         log.info("************Entering the TourDAO.insert***************");
>         PersistenceBroker broker = null;
>         try {
>             broker = ServiceLocator.getInstance().findBroker();
>             TourVO tour = (TourVO)insertRecord;
>             AssumptionVO assumptionVO = tour.getAssumptions();
>             broker.beginTransaction();
>             broker.store(tour);
>             assumptionVO.setTour(tour);
>             broker.store(assumptionVO);            
>             broker.commitTransaction();
>       .......
>      
> Of course the other remaining code is just the catch and finally parts.
>  
> I thought that OJB was set up that it would have automatically saves the child record, instead of me having to call store() a second time on the broker.
>  
> But I am happy that I got the records inserted, I can finally move on in my project to the next steps. As I had said I have been stuck trying to get OJB to work for over two weeks, and this is a simple project I wrote.
>  
> Thanks for you help.
>  
> Mark
> 
> "McCaffrey, John G." <Jo...@kraft.com> wrote:
> 
> yes, (my understanding) if you have a 1-1 relationship, but there is a FK
> constraint that indicates a parent > child relationship, then OJB wants the
> Child Mapping to contain a reference to the parent. 
> 
> I think the basic rule is like this
> mapping for Object A
> contains 
> ref to B
> ref to C
> collection ref to D
> 
> SQL queries will go like this
> insert B
> insert C
> insert A
> insert D
> 
> That was a suprise to me, and I wish it wasn't like that. (I have asked if I
> am just not understanding this, but i haven't gotten a response that says I
> am wrong yet)
> 
> What I don't like about it is that if you have a 1-1 child relationship you
> have to invert you mapping to make OJB happy, but it might not make sense
> for yuo object
> 
> ex (this is what I would like to do):
> company obj mapping (parent)
> contains
> primary manager obj (only one reference and this is truly a child of
> company)
> primary address obj (same deal)
> 
> but this would fail because OJB will try to insert the manager and address
> first
> instead I think you have to do this:
> 
> Manager
> contains 
> a company
> (I don't even know what will happen with the address)
> 
> So if I am understanding this, it really makes it hard for you to use
> composite VOs when you have 1-1 parent-child relationships.
> 
> any thoughts? (someone please tell me that I am wrong, and that there is a
> better way to do this)
> 
> 
> 
> -----Original Message-----
> From: Mark Spritzler [mailto:bytor99999@yahoo.com]
> Sent: Friday, May 07, 2004 4:21 PM
> To: OJB Users List
> Subject: RE: Child Table insert at same time as Parent and Auto
> generated PK
> 
> 
> Hi John, thanks. Actually I have already added the sequence manager to the
> jdbc-connection-descriptor, and also the settings for the primary key.
> 
> So are you saying that my reference-descriptor in the Parent table needs to
> be removed and instead add it to my child table? If not then, what do you
> mean. :)
> 
> Thanks
> 
> Mark
> 
> "McCaffrey, John G." wrote:
> Search the archives for SequenceManger and Idenity threads.
> 
> Here is what I needed to do:
> add a sequence manager to my jdbc-connection-descriptor
> className="org.apache.ojb.broker.util.sequence.SequenceManagerNativeImpl">
> 
> 
> Make sure these were set on my primary key
> primarykey="true"
> autoincrement="true"
> access="readonly"
> 
> Then I had to flip my mapping because I had
> Parent > contains a reference to a single Child (not a collection)
> but OJB wants Child > contains reference to Parent
> 
> HTH
> 
> -----Original Message-----
> From: Mark Spritzler [mailto:bytor99999@yahoo.com]
> Sent: Friday, May 07, 2004 4:05 PM
> To: ojb-user@db.apache.org
> Subject: Child Table insert at same time as Parent and Auto generated PK
> 
> 
> I am using MySql.
> 
> OK. I have a Parent-Child relationship which is 1:1 relationship. I am
> inserting into each table at the same time. Obviously, the Parent gets
> inserted first. But the Primary Key is auto-generated by MySQL in an
> auto-generated field. So when the child record gets inserted, I need that
> newly created ID to be in the child record. But it isn't getting it, and the
> ID in the child is Null, so the Database returns an error for it being Null.
> 
> Does anyone know a way to fix this problem? Putting the child fields into
> the Parent table is not a possibility and wouldn't be a good db design
> decision.
> 
> Thanks everyone
> 
> Mark
> 
> 
> ---------------------------------
> Do you Yahoo!?
> Win a $20,000 Career Makeover at Yahoo! HotJobs 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 
> 
> 
> Franziska says, "Keep eating healthy"
> 
> she also says that the above line is not actually gramatically correct. 
> 
> She wants you all to know that healthy cannot be there; it really should be
> an adverb like healthfully.
> 
> 
> ---------------------------------
> Do you Yahoo!?
> Win a $20,000 Career Makeover at Yahoo! HotJobs 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 
> 		
> ---------------------------------
> Do you Yahoo!?
> Win a $20,000 Career Makeover at Yahoo! HotJobs 

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org