Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
М_указания к лабам_230400.68_совр_тенденции.doc
Скачиваний:
0
Добавлен:
01.07.2025
Размер:
9.28 Mб
Скачать

6.2 Creating the data model classes

With Objectify, you create classes whose instances will represent datastore entities in your code. Objectify does the work of translating these Java objects to datastore entities.

Create a Greeting class to represent a message posted by a user. In guestbook/src/main/java/com/example/guestbook/, create a file named Greeting.java with the following contents:

package com.example.guestbook; import com.googlecode.objectify.Key; import com.googlecode.objectify.annotation.Entity; import com.googlecode.objectify.annotation.Id; import com.googlecode.objectify.annotation.Index; import com.googlecode.objectify.annotation.Parent; import java.lang.String; import java.util.Date; import java.util.List; /**  * The @Entity tells Objectify about our entity.  We also register it in {@link OfyHelper}  * Our primary key @Id is set automatically by the Google Datastore for us.  *  * We add a @Parent to tell the object about its ancestor. We are doing this to support many  * guestbooks.  Objectify, unlike the AppEngine library requires that you specify the fields you  * want to index using @Index.  Only indexing the fields you need can lead to substantial gains in  * performance -- though if not indexing your data from the start will require indexing it later.  *  * NOTE - all the properties are PUBLIC so that can keep the code simple.  **/ @Entity public class Greeting {   @Parent Key<Guestbook> theBook;   @Id public Long id;   public String author_email;   public String author_id;   public String content;   @Index public Date date;   /**    * Simple constructor just sets the date    **/   public Greeting() {     date = new Date();   }   /**    * A convenience constructor    **/   public Greeting(String book, String content) {     this();     if( book != null ) {       theBook = Key.create(Guestbook.class, book);  // Creating the Ancestor key     } else {       theBook = Key.create(Guestbook.class, "default");     }     this.content = content;   }   /**    * Takes all important fields    **/   public Greeting(String book, String content, String id, String email) {     this(book, content);     author_email = email;     author_id = id;   } }

We also need a Guestbook class to represent an entire guestbook. While this example doesn't explicitly create a Guestbook object in the datastore, it uses this class as part of the datastore key for the Greeting objects. This demonstrates how you might define keys so that all of the greetings in a guestbook could be updated in a single datastore transaction.

Create a file named Guestbook.java with the following contents:

package com.example.guestbook; import com.googlecode.objectify.annotation.Entity; import com.googlecode.objectify.annotation.Id; /**  * The @Entity tells Objectify about our entity.  We also register it in  * OfyHelper.java -- very important.  *  * This is never actually created, but gives a hint to Objectify about our Ancestor key.  */ @Entity public class Guestbook {   @Id public String book; }