سؤال

لدي دورة كيان له مفتاح كيان آخر (مستند) في الداخل.

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class Course{

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

 @Persistent private Key document;

public Document getDocument() {
  if (document != null)
  return new DocumentServiceImpl().getDocumentById(document.getId());
  return null;
 }
public void setDocument(Document document) {
  if (document != null)
   this.document = new DocumentServiceImpl().saveAndGetKey(document);
 }

في بعض رمز الاختبار، أجري كيان دائرة جديدة، وتعيين كيان مستند جديد، وسريع كيان المستند عند ضبط خاصية المستند في الدورة التدريبية. عندما أقصر بالطبع، ستستمر دون خطأ، ولكن بمجرد استمرار ذلك، ستكون خاصية المستندات فارغة.

أيه أفكار؟ هنا هي وظيفة الحفظ الخاصة بي بالطبع:

public Boolean save(Course c){
  Boolean isSaved = false;
  PersistenceManager pm = PMF.get().getPersistenceManager();

  try{   
   pm.makePersistent(c);
   isSaved = true;
  }
  catch(Exception e){
   e.printStackTrace();
   isSaved = false;
  }
  finally{
   pm.close();
  }

  return isSaved;

 }

تحرير لإضافة:

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class Document{
 @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;

 @Persistent private String   data;
 @Persistent private Set<Key>  dTags;
 @Persistent private Date   dateCreated;
 @Persistent private Date   dateEdited;

 public Document(){
  this.dateCreated = new Date();
 }

 public Long getId() {
  if (key == null){
   return null;
  } else {
   return key.getId();
  }
 }
 public void setId(Long id) {
  if (id != null)
  key = KeyFactory.createKey(this.getClass().getSimpleName(), id);
 }

من documentservicesimpl:

public Key saveAndGetKey(Document d) {
  try{
   if (d.getKey() == null){
    save(d);
   }

   return d.getKey();
  } catch (Exception e){
   return null;
  }  
 }

public Boolean save(Document d) {
  Boolean isSaved = false;
  PersistenceManager pm = PMF.get().getPersistenceManager();

  try {
   pm.makePersistent(d);
   isSaved = true;
  } catch (Exception e) {
   e.printStackTrace();
   isSaved = false;
  }finally{pm.close();}

  return isSaved;

 }

الوثيقة العامة GetDocumentByid (معرف طويل) {

persistencemanager PM = PMF.Get (). GetPersistEcmanager ()؛ المستند د = وثيقة جديدة ()؛

جرب {d = pm.getobjectbyid (document.class، ID)؛ } وأخيرا {PM.Close ()؛ }

العودة د؛ }

هل كانت مفيدة؟

المحلول

  • كيف تبدو فئة الوثيقة؟
  • كيف تبدو فئة documentserviceimpl؟
  • كيف يبدو اختبار الوحدة الخاص بك ل SAVEANDGETKEY () هل تحقق من أن قيمة الإرجاع هي مفتاح صالح؟ يمكنك بعد ذلك البحث عن هذا المستند في مؤخرة البيانات؟
  • هل فصول الخدمة الخاصة بك persistencapable، أو persistenceare؟ لست متأكدا مما إذا كان بحاجة إلى أن يكون أو لا يستند فقط على ما أظهرت لنا.

فكرة جديدة لاستكشاف الأخطاء وإصلاحها أدناه: ماذا يحدث إذا حاولت شيئا بسيطا مثل هذا: فقط في الوقت الحالي، قم بعمل دورة. ثم معرفة ما إذا كانت هذه الطريقة الأكثر بساطة لإنشاء أعمال كياناتك.

pm = yourPMfactory.getPersistenceManger();
Course c = new Course();
Document d = new Document();
c.document = d;
pm.makePersistent(c);

Key myKey = c.getKey();
Course c2 = (Course) pm.getObjectById(Course.class, myKey.getId());
assertTrue(c.document != null); //or however your favorite test suite does assertions.
pm.close();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top