Sven <sj1981 RemoveThis @gmail.com> 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