Re: SimpleORM 3.11: Fails to create m:n relation record

[email protected] Thu, 18 Feb 2010 11:01:17 +1000
Newsgroups gmane.comp.java.orm.simpleorm
Message-ID <[email protected]>
Use SRecordInstance.setDirty.  

Key only records are awkward whatever we do.  We certainly do not want findOrCreate to create records with just keys in the normal case.  There was some debate whether create should setDirty, but I think we were worried about compatibility.  Maybe we should.

Key only records are actually rare in practice.  There is almost always something that you want to set on the m:n record.  Eg. who or when or why is the article in a certain category, or a user comment as to why.

Anthony


/**
 * Sets this record to be dirty so that it will be updated in the database.
 * Normally done implicitly by setting a specific column. 
 * 
 * NOP - But may
 * occasionally be useful after a findOrInsert() to add a record that
 * contains nothing appart from its primary key. ===> isNewRow is here for that
     * @See SDataSet#makeFlushedDirty which also makes dirty.
 */
public void setDirty(boolean val) {

At 07:51 PM 17/02/2010, Roland Tapken wrote:
>Hello,
>
>I think I found a bug in SSession/SDataset, but I'm not sure how to fix it. I have three Databases:
>
>article
>-------
>  article_id (String)
>  title (String)
>  [and some more]
>
>category
>--------
>  category_id (String)
>  title (String)
>
>article_categories
>------------------
>  article_id (String)
>  category_id (String)
>
>Article and Category are working fine, but I cannot create new entries in article_categories. This is the class which should connect these tables:
>
>public class ArticleCategory extends SRecordInstance {
>    public static final SRecordMeta<ArticleCategory> ARTICLE_CATEGORY
>        = new SRecordMeta<DBArticleCategory>(ArticleCategory.class,
>        "article_category");
>        
>    public static final SFieldString ARTICLE_ID = new
>        SFieldString(ARTICLE_CATEGORY, "article_id", 255,
>        SFieldFlags.SPRIMARY_KEY);
>
>    public static final SFieldReference<Article> ARTICLE = new
>        SFieldReference<Article>(ARTICLE_CATEGORY, Article.ARTICLE,
>        "article");
>        
>    public static final SFieldString CATEGORY_ID = new
>        SFieldString(ARTICLE_CATEGORY, "category_id", 255,
>        SFieldFlags.SPRIMARY_KEY);
>
>    public static final SFieldReference<Category> CATEGORY = new
>        SFieldReference<Category>(ARTICLE_CATEGORY, Category.CATEGORY,
>        "category");
>        
>    @Override
>    public SRecordMeta<?> getMeta() {
>        return ARTICLE_CATEGORY;
>    }
>}
>
>When I say:
>
>session.create(ArticleCategory.ARTICLE_CATEGORY, "SomeArticleId", "SomeCategoryId");
>
>or
>
>ArticleCategory artCat = session.findOrCreate(ArticleCategory.ARTICLE_CATEGORY, "SomeArticleId", "SomeCategoryId");
>artCat.assertNewRow();
>
>It fails with the following exception:
>
>simpleorm.utils.SException$Error: Not a new row [ArticleCategory SomeArticleId, SomeCategoryId]
>at simpleorm.dataset.SRecordInstance.assertNewRow(SRecordInstance.java:816)
>at simpleorm.sessionjdbc.SSessionJdbc.create(SSessionJdbc.java:661)
>at de.senertec.common.provider.DataProvider.create(DataProvider.java:97)
>
>Note that the table article_category is empty! This HAS to be a new row.
>
>I've traced down this bug, but I don't know how to fix it. In SDataSet.findOrCreate() the comment in line 77 says "We leave it not new and not dirty.  That is set by the SSession.". Ok, back to SSession... "doFindOrCreate()" asks "needsQuery()" if this might be a new and/or dirty record. But because ArticleCategory has no other fields besides it's primary keys (which are initialized and valid) the result will be FALSE even if this record has never been stored before!
>
>The same problem occurs when all fields have predefined initial values (I've tested that). This is a really critical bug because when using session.findOrCreate() a new record would be dismissed silently.
>
>My workaround is to use session.getDatabase().create instead of session.create() because in this situation I'm quite sure that this entry does not exists. But since session.findOfCreate() has the same problem this really should be fixed :-)
>
>Kind regards,
>
>Roland

Dr Anthony Berglas, [email protected]       Mobile: +61 4 4838 8874
Just because it is possible to push twigs along the ground with ones nose
does not necessarily mean that is the best way to collect firewood.



------------------------------------

Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/SimpleORM/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/SimpleORM/join
    (Yahoo! ID required)

<*> To change settings via email:
    [email protected] 
    [email protected]

<*> To unsubscribe from this group, send an email to:
    [email protected]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/