Domanda

Ho un corso entità che ha una chiave ad un altro soggetto (documento) all'interno.

@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);
 }

In un certo codice di prova Faccio una nuova entità del corso, e assegnare una nuova entità documento, e l'entità documento viene mantenuto quando ho impostato la proprietà documento sul corso. Quando io insisto Naturalmente, persisterà senza errori, ma una volta che è persistito la proprietà del documento sarà nullo.

Tutte le idee? Qui è la mia funzione di salvataggio per il corso:

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;

 }

Modifica per aggiungere:

@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);
 }

da 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;

 }

pubblica Documento getDocumentById (Long id) {

PersistenceManager pm = PMF.get () getPersistenceManager ().;   Documento d = new Document ();

try {    d = pm.getObjectById (Document.class, id);   } finalmente {    pm.close ();   }

ritorno d;  }

È stato utile?

Soluzione

  • Che cosa significa l'aspetto classe Document come?
  • che cosa fa l'aspetto di classe DocumentServiceImpl come?
  • Che cosa significa il test di unità per saveAndGetKey () simile? Ha verificare che il valore di ritorno è una chiave valida? si può poi cercare quel documento nel datastore?
  • Sono le vostre classi ServiceImpl PersistenceCapable, o PersistenceAware? Non sono sicuro se hanno bisogno di essere o non basata solo su quello che ci hai mostrato.

Risoluzione dei problemi Idea di seguito: Cosa succede se si cerca qualcosa di semplice come questo: Solo per ora, fare Course.document pubblica. Poi vedere se in questo modo più semplice di creare le entità funziona.

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();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top