headgrowe
headgrowe

Reputation: 529

makePersistent for owned one-to-many rs. not working reliably in JDO

the makePersistent method of the PersistenceManager is not working reliably. one and the same junit-test is working and after a while its failing again?! i have an object that includes a collection of other objects. that means an 1-n relationship. my problem is that the junit-test of my classes is failing sometimes because the objects in the collection are not persisted properly. i am using usually transactions but the persistanceManager is transaction-optional

i tried checking the objectState after each createObject and commit... i figured out that the objects in the collection after a successful commit have no systemId (should be auto-generated) but are in state hollow/persistent-nontransactional.

that means sometimes they are (if they are persisted):

Comment [systemId=Project(1)/Comment(6), JDO-ObjectState=hollow/persistent-nontransactional]

and sometimes they are (if they are not persisted and a refach of the parent-object contains an empty collection) Comment [systemId=null, JDO-ObjectState=hollow/persistent-nontransactional]

off course i could check manually if all the stored objects have an systemId but this approach is not nice at all. the commit should just fail!!

i do not understand that it is sometimes working and sometimes not! that means my code should not be wrong and this is a bug... pls help me out

PS: i can post some code if necessary!

@PersistenceCapable
public class Ble implements Serializable, JDOObject<Ble> {

/**
 * 
 */
private static final long serialVersionUID = 1L;

// NotNull
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key systemId;

// NotNull
@Persistent
private Key parentId;

// NotNull
@Persistent
@Extension(vendorName = "datanucleus", key = "gae.parent-pk", value = "true")
private Key projectId;

// NotNull
@Persistent
private String title;

@Persistent
private int position;

@Persistent
private boolean hasChildren;

@Persistent
private BleData requirementData;

@Persistent
private List<Comment> comments;


//getter/setter
}

childObject

    @PersistenceCapable
public class Comment implements Serializable, JDOObject<Comment> {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key systemId;

@Persistent
private String text;

@Persistent
private long createdTimestamp;

//getter//Setter    
}

Upvotes: 0

Views: 462

Answers (2)

headgrowe
headgrowe

Reputation: 529

the Log if i try to add a comment and it’s NOT working

05.09.2012 15:02:06 org.datanucleus.jdo.metadata.JDOMetaDataManager$MetaDataRegisterClassListener registerClass
INFO: Listener found initialisation for persistable class com.Comment
05.09.2012 15:02:06 org.datanucleus.store.appengine.MetaDataValidator validate
INFO: Performing appengine-specific metadata validation for com.Comment
05.09.2012 15:02:06 org.datanucleus.store.appengine.MetaDataValidator validate
INFO: Finished performing appengine-specific metadata validation for com.Comment
Comment [systemId=null, text=testAddCommentToBle - comment , createdTimestamp=1346850126819, JDO-Status=hollow/persistent-nontransactional]

the Log if i try to add a comment and it IS working

05.09.2012 15:00:25 org.datanucleus.jdo.metadata.JDOMetaDataManager$MetaDataRegisterClassListener registerClass
INFO: Listener found initialisation for persistable class com.Comment
05.09.2012 15:00:26 org.datanucleus.store.appengine.MetaDataValidator validate
INFO: Performing appengine-specific metadata validation for com.Comment
05.09.2012 15:00:26 org.datanucleus.store.appengine.MetaDataValidator validate
INFO: Finished performing appengine-specific metadata validation for com.Comment
Comment [systemId=Project(1)/Comment(6), text=testAddCommentToBle - comment , createdTimestamp=1346850025996, JDO-Status=hollow/persistent-nontransactional]

the add method looks like this.

public Boolean addCommentToBle(Key systemKey, Comment comment)
        throws Exception {
    PersistenceManagerFactory PMF = PersistenceUtil.getPersistenceManagerFactory();
    PersistenceManager pm = PMF.getPersistenceManager();
    pm.currentTransaction().begin();
    Ble ble= pm.getObjectById(Ble.class, systemKey);
    System.out.println(ble);
    ble.getComments().add(comment);
    pm.makePersistent(ble);
    pm.currentTransaction().commit();
    return true;
}

Upvotes: 0

headgrowe
headgrowe

Reputation: 529

the solution is always to use a new persistanceManager for each transaction. never reuse a pm!!

Upvotes: 1

Related Questions