2014-07-24 57 views
0

對於Objectify我很新,我正在嘗試爲一個簡單的對象創建端點。我遇到的問題是Eclipse自動創建的端點類具有對JPA的引用。我是否必須手動創建自己的端點類,而不是在Eclipse中使用「生成端點類」?下面是我建立我的項目的步驟和源代碼:在Eclipse中通過Objectify設置AppEngine端點類的問題

  1. 創建Web應用程序項目在Eclipse(應用程序引擎項目)
  2. 新增物化-5.0.3.jar和番石榴17.0。 jar文件到WEB-INF/lib和項目的類路徑
  3. 創建一個簡單的類Car.java並添加註釋
  4. 註冊於的servlet(ObjectifyService.register(Car.class)的實體;
  5. 右鍵點擊了汽車類和選定的Google App Engine(WPT)>生成雲端點類
  6. 部署到App Engine和API瀏覽器試圖插入一條記錄,我得到這個錯誤日誌中:

     
    https://objectify-example-650.appspot.com/_ah/api/carendpoint/v1/car 
    Method: carendpoint.insertCar 
    Error Code: 400 
    Reason: badRequest 
    Message: java.lang.IllegalArgumentException: Type ("com.appengine.objectify.Car") is not that of an entity but needs to be for this operation 
    

我也看到在應用程序引擎項目日誌此錯誤/警告:

<pre> 
org.datanucleus.metadata.MetaDataManager loadPersistenceUnit: Class com.appengine.objectify.CarEndpoint was specified in persistence-unit transactions-optional but not annotated, so ignoring 
</pre> 

這裏是我的源代碼:

import com.googlecode.objectify.annotation.Entity; 
@Entity 
public class Car { 
    @Id Long id; 
    String vin; 
    String color; 
    private Car() {} 
    public Long getId() { 
     return id; 
    } 
    public void setId(Long id) { 
     this.id = id; 
    } 
    public String getVin() { 
     return vin; 
    } 
    public void setVin(String vin) { 
     this.vin = vin; 
    } 
    public String getColor() { 
     return color; 
    } 
    public void setColor(String color) { 
     this.color = color; 
    } 

} 

釷Ë端點類:

@Api(name = "carendpoint", namespace = @ApiNamespace(ownerDomain = "appengine.com", ownerName = "appengine.com", packagePath = "objectify")) 
public class CarEndpoint { 

@SuppressWarnings({ "unchecked", "unused" }) 
@ApiMethod(name = "listCar") 
public CollectionResponse<Car> listCar(
     @Nullable @Named("cursor") String cursorString, 
     @Nullable @Named("limit") Integer limit) { 

    EntityManager mgr = null; 
    Cursor cursor = null; 
    List<Car> execute = null; 

    try { 
     mgr = getEntityManager(); 
     Query query = mgr.createQuery("select from Car as Car"); 
     if (cursorString != null && cursorString != "") { 
      cursor = Cursor.fromWebSafeString(cursorString); 
      query.setHint(JPACursorHelper.CURSOR_HINT, cursor); 
     } 

     if (limit != null) { 
      query.setFirstResult(0); 
      query.setMaxResults(limit); 
     } 

     execute = (List<Car>) query.getResultList(); 
     cursor = JPACursorHelper.getCursor(execute); 
     if (cursor != null) 
      cursorString = cursor.toWebSafeString(); 

     for (Car obj : execute) 
      ; 
    } finally { 
     mgr.close(); 
    } 

    return CollectionResponse.<Car> builder().setItems(execute) 
      .setNextPageToken(cursorString).build(); 
} 


@ApiMethod(name = "getCar") 
public Car getCar(@Named("id") Long id) { 
    EntityManager mgr = getEntityManager(); 
    Car car = null; 
    try { 
     car = mgr.find(Car.class, id); 
    } finally { 
     mgr.close(); 
    } 
    return car; 
} 

@ApiMethod(name = "insertCar") 
public Car insertCar(Car car) { 
    EntityManager mgr = getEntityManager(); 
    try { 
     if (containsCar(car)) { 
      throw new EntityExistsException("Object already exists"); 
     } 
     mgr.persist(car); 
    } finally { 
     mgr.close(); 
    } 
    return car; 
} 

@ApiMethod(name = "updateCar") 
public Car updateCar(Car car) { 
    EntityManager mgr = getEntityManager(); 
    try { 
     if (!containsCar(car)) { 
      throw new EntityNotFoundException("Object does not exist"); 
     } 
     mgr.persist(car); 
    } finally { 
     mgr.close(); 
    } 
    return car; 
} 

@ApiMethod(name = "removeCar") 
public void removeCar(@Named("id") Long id) { 
    EntityManager mgr = getEntityManager(); 
    try { 
     Car car = mgr.find(Car.class, id); 
     mgr.remove(car); 
    } finally { 
     mgr.close(); 
    } 
} 

private boolean containsCar(Car car) { 
    EntityManager mgr = getEntityManager(); 
    boolean contains = true; 
    try { 
     Car item = mgr.find(Car.class, car.getId()); 
     if (item == null) { 
      contains = false; 
     } 
    } finally { 
     mgr.close(); 
    } 
    return contains; 
} 

private static EntityManager getEntityManager() { 
    return EMF.get().createEntityManager(); 
} 

} 

EFM類由日食autogenerating端點類時創建:

import javax.persistence.EntityManagerFactory; 
import javax.persistence.Persistence; 

public final class EMF { 
    private static final EntityManagerFactory emfInstance = Persistence 
      .createEntityManagerFactory("transactions-optional"); 

    private EMF() { 
    } 

    public static EntityManagerFactory get() { 
     return emfInstance; 
    } 
} 

回答

0

謝謝蒂姆!我最終寫了端點類。正如你所提到的,eclipse不會爲註釋爲Objectity的類創建端點類。

package com.appengine.objectify; 

import static com.appengine.objectify.OfyService.ofy; 
import com.google.api.server.spi.config.Api; 
import com.google.api.server.spi.config.ApiMethod; 
import com.google.api.server.spi.config.ApiNamespace; 
import java.util.ArrayList; 
import java.util.List; 
import javax.inject.Named; 

@Api(name = "carendpoint", namespace = @ApiNamespace(ownerDomain = "appengine.com", ownerName = "appengine.com", packagePath = "objectify")) 
public class CarEndpoint { 

    @ApiMethod(name = "listCar") 
    public List<Car> listCar() { 
     List<Car> result = new ArrayList<Car>(); 
     result = ofy().load().type(Car.class).list(); 
     return result; 
    } 

    @ApiMethod(name = "getCar") 
    public Car getCar(@Named Long id) { 
     Car Car = ofy().load().type(Car.class).id(id).now(); 
     return Car; 
    } 

    @ApiMethod(name = "insertCar") 
    public Car insertCar(Car Car) { 
     ofy().save().entity(Car).now(); 
     return Car; 
    } 

    @ApiMethod(name = "removeCar") 
    public void removeCar(@Named Long id) { 
     ofy().delete().type(Car.class).id(id).now(); 
    } 


}