我是Objectify的新手,我正在尝试为一个简单的对象创建端点。我遇到的问题是Eclipse自动创建的端点类引用了JPA。我是否必须手动创建自己的端点类而不是使用Eclipse中的“生成端点类”?以下是我设置项目的步骤和源代码:
部署到App Engine并从API Explorer尝试插入记录,我在日志中收到此错误:
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();
}
}
自动生成端点类时由eclipse创建的EFM类:
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 :(得分:0)
自动生成云端点类,仅适用于JPA / JDO,其涵盖在此处:Objectify with Endpoints for android希望有所帮助
答案 1 :(得分: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();
}
}