Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Beginner question about JDO best practice

 
   Database Forums (Home) -> Java RSS
Next:  Profiler stored procedure parameter display  
Author Message
Sven

External


Since: Dec 20, 2007
Posts: 3



(Msg. 1) Posted: Thu Dec 20, 2007 5:26 am
Post subject: Beginner question about JDO best practice
Archived from groups: comp>lang>java>databases (more info?)

Dear all,

this is the first time I'm using JDO/ORM (the JPOX implementation) and
so have a question about best practice.

Let's assume we have a class "Tag" with the only property "String
name" and according setters and getters. The related table has two
columns "PRIMARY BIGINT tag_id" and "UNIQUE VARCHAR name". Note that
the column "name" is unique.

Let's further assume that we manually create some Tag objects during
application runtime. We now want to persist these objects but we don't
care if they are already persisted in the database. If this is the
case, JDO should just attach the object to the database instance. What
is the best practice to accommodate this? Do I have to manually check
every object for existence first? I would like to avoid this.

Let's say the tag "coffee" is already available in the database. I'm
looking for something which works like this:

--8<-snip->8--

Tag tag1 = new Tag( "java" );
Tag tag2 = new Tag( "means" );
Tag tag3 = new Tag( "coffee" );

// no problem
pm.makePersistent( tag1 );

// no problem
pm.makePersistent( tag2 );

// throws SQLException DUPLICATE KEY but should just attach/update
tag3 to database instance
pm.makePersistent( tag3 );

--8<-snip->8--

Thanks for any hint!

 >> Stay informed about: Beginner question about JDO best practice 
Back to top
Login to vote
Sven

External


Since: Dec 20, 2007
Posts: 3



(Msg. 2) Posted: Thu Dec 20, 2007 7:53 am
Post subject: Re: Beginner question about JDO best practice [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On 20 Dez., 16:02, Dyreatn....TakeThisOut@sun.com wrote:

> But presumably the 'name' column is declared 'UNIQUE' for a reason?
> Allowing you to just add tag3 would violate the unique-ness,
> wouldn't it? If you don't really need them to be unique after all,
> shouldn't you change the table definition to reflect that?

To simplify matters I didn't explain the whole situation but I guess
this is necessary now:

Let's say we have another class, for example a "BlogEntry" class,
which contains - among other things - a Set of Tag objects. Since
there can be many BlogEntry and Tag instances I want the tags to be
unique in the database scheme. blog entries and tags are related to
each other via a join table which contains the columns "blog_id" and
"tag_id".

What I was hoping for is that JDO automatically handles the uniqueness
of tags when persisting a BlogEntry object with n Tag objects and
inserts the right ids into the join table.

 >> Stay informed about: Beginner question about JDO best practice 
Back to top
Login to vote
Sven

External


Since: Dec 20, 2007
Posts: 3



(Msg. 3) Posted: Thu Dec 20, 2007 10:24 am
Post subject: Re: Beginner question about JDO best practice [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Update: I've created a method which returns a Tag object from the
database based on a name, if available. This method detaches the Tag
object. Also the Tag class was not defined as detachable before which
I changed.

Now, when I persist a BlogEntry object which contains new and existing
Tag objects no more exception occurs, but the existing Tags are not
joined to the BlogEntry object in the join table.
 >> Stay informed about: Beginner question about JDO best practice 
Back to top
Login to vote
Dyreatnews

External


Since: Jun 08, 2006
Posts: 23



(Msg. 4) Posted: Thu Dec 20, 2007 1:00 pm
Post subject: Re: Beginner question about JDO best practice [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Sven writes:

> Dear all,
>
> this is the first time I'm using JDO/ORM (the JPOX implementation) and
> so have a question about best practice.
>
> Let's assume we have a class "Tag" with the only property "String
> name" and according setters and getters. The related table has two
> columns "PRIMARY BIGINT tag_id" and "UNIQUE VARCHAR name". Note that
> the column "name" is unique.
>
> Let's further assume that we manually create some Tag objects during
> application runtime. We now want to persist these objects but we don't
> care if they are already persisted in the database. If this is the
> case, JDO should just attach the object to the database instance. What
> is the best practice to accommodate this? Do I have to manually check
> every object for existence first? I would like to avoid this.
>
> Let's say the tag "coffee" is already available in the database. I'm
> looking for something which works like this:
>
> --8<-snip->8--
>
> Tag tag1 = new Tag( "java" );
> Tag tag2 = new Tag( "means" );
> Tag tag3 = new Tag( "coffee" );
>
> // no problem
> pm.makePersistent( tag1 );
>
> // no problem
> pm.makePersistent( tag2 );
>
> // throws SQLException DUPLICATE KEY but should just attach/update
> tag3 to database instance
> pm.makePersistent( tag3 );

But presumably the 'name' column is declared 'UNIQUE' for a reason?
Allowing you to just add tag3 would violate the unique-ness,
wouldn't it? If you don't really need them to be unique after all,
shouldn't you change the table definition to reflect that?

--
dt
 >> Stay informed about: Beginner question about JDO best practice 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
jdbc software architecture design, best practice - hi all, I hadn't used any jdbc for some time, but I had to come back at it most recently. so I dug out some code of mine which I had used a few years ago - both mysql (which I'm using) and jdbc connector have evolved somewhat in the meantime. probably a...

JDeveloper question - Hello, I'm using JDeveloper and just starting out. How do I create a datasource? The application server I'm using is JBoss 3.2. Thanks, - Dave

sql select question - Hi all this is more of an SQL question than a Java question. I have a table with a SPEED column and some other columns like this: SPEED SOME_OTHER_COLUMNS.... ----- ------------------ 100 some values here... 34 97 68 13 27 44 now I want to..

JDBC oracle question - Hello, I'm building a wireless application with Java J2ME (CDC profile). My application needs a direct connection with a remote Oracle database. For this purpose I think it's best to use JDBC. But which JDBC driver should I use? - Should I use an..

Another Tomcat DataSource Question - I have been searching and searching for the answer to this, and despite seeing many similar questions, I have yet to find the answer. I have a struts 1.1 application using Tomcat 4.x that I will eventually move to Tomcat 5.0. I have the..
   Database Forums (Home) -> Java All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]