2015-09-10 47 views
1

我在Spring-MVC应用程序中使用Objectify 5.1.7和Objectify Spring extensionObjectify抛出IllegalArgumentException:没有注册类'com.app.db.client.model.ProductType'

这里是我的实体类:

Product.java

@Entity 
public class Product extends RelatedDataObject { 

@Parent 
private Ref<Vendor> vendor; 
@Load 
private Ref<ProductCategory> productCategory; 
@Load 
private Ref<ProductType> productType; 

@Index 
private String nativeId; 
private Double costPrice; 
private String modelId; 
private String serviceLocations; 
private Map<String, String> attributes; 

public Double getCostPrice() { 
    return costPrice; 
} 

public String getModelId() { 
    return modelId; 
} 

public String getServiceLocations() { 
    return serviceLocations; 
} 

public Map<String, String> getAttributes() { 
    return attributes; 
} 

public void setCostPrice(Double costPrice) { 
    this.costPrice = costPrice; 
} 

public void setModelId(String modelId) { 
    this.modelId = modelId; 
} 

public void setServiceLocations(String serviceLocations) { 
    this.serviceLocations = serviceLocations; 
} 

public void setAttributes(Map<String, String> attributes) { 
    this.attributes = attributes; 
} 

public void addAttribute(String key, String value) { 
    if(key == null || value == null) { 
     throw new IllegalArgumentException("Key or value is null."); 
    } 
    if(attributes == null) { 
     attributes = new HashMap<String, String>(); 
    } 
    attributes.put(key, value); 
} 

public ProductCategory getProductCategory() { 
    return productCategory.get(); 
} 

public ProductType getProductType() { 
    return productType.get(); 
} 

public String getNativeId() { 
    return nativeId; 
} 

public void setNativeId(String nativeId) { 
    this.nativeId = nativeId; 
} 

public void setProductCategory(ProductCategory productCategory) { 
    this.productCategory = Ref.create(productCategory); 
} 

public void setProductType(ProductType productType) { 
    this.productType = Ref.create(productType); 
} 

public Vendor getVendor() { 
    return vendor.get(); 
} 

public void setVendor(Vendor vendor) { 
    this.vendor = Ref.create(vendor); 
} 



public Key<Product> getKeyByParentVendor() { 

    if (getId() == null) { 
     throw new IllegalArgumentException("Product id is not set."); 
    } 
    if (vendor == null) { 
     throw new IllegalArgumentException("Parent vendor is not set."); 
    } 

    return Key.create(this.vendor.key(), Product.class, getId()); 

    } 
} 

ProductType.java

@Entity 
public class ProductType extends RelatedDataObject { 
} 

RelatedDataObject.java

public class RelatedDataObject extends DataObject { 

private String description; 
private boolean approved; 

public RelatedDataObject() { 
    super(); 
    approved = false; 
} 

public String getDescription() { 
    return description; 
} 

public void setDescription(String description) { 
    this.description = description; 
} 

public boolean isApproved() { 
    return approved; 
} 

public void setApproved(boolean approved) { 
    this.approved = approved; 
} 
} 

DataObject.java

public class DataObject { 

@Id 
private String id; 
@Index 
private String name; 
private boolean inactive; 

public DataObject() { 
    super(); 
    inactive = false; 
} 

public String getId() { 
    return id; 
} 

public String getName() { 
    return name; 
} 

public void setId(String id) { 
    this.id = id; 
} 

public void setName(String name) { 
    this.name = name; 
} 



public boolean isInactive() { 
    return inactive; 
} 

public void setInactive(boolean inactive) { 
    this.inactive = inactive; 
} 
} 

这里是我的spring bean xml配置。我所有的实体类的包里面:com.app.db.client.client.model

<bean id="objectifyFactory" class="com.googlecode.objectify.spring.ObjectifyFactoryBean"> 
    <property name="basePackage" value="com.app.db.client.model"/> 
</bean> 

<bean id="dbClient" class="com.app.db.client.impl.DbClientImpl"> 
    <property name="objectifyFactory" ref="objectifyFactory"/> 
</bean> 

DBClientImpl.java

public class DbClientImpl implements DbClient { 

private ObjectifyFactory objectifyFactory; 

public void setObjectifyFactory(ObjectifyFactory objectifyFactory) { 
    this.objectifyFactory = objectifyFactory; 
} 

@Override 
public <T extends DataObject> void createObject(T object) { 

    Objectify ofy = objectifyFactory.begin(); 
    ofy.save().entity(object).now(); 
} 
} 

当GAE devserver靴子我的春天MVC应用程序,所有的实体类被加载。下面是日志消息:

[INFO] 2015-09-10 13:20:15 INFO ObjectifyFactoryBean:115 - Registered entity class [com.app.db.client.model.Product] 
[INFO] 2015-09-10 13:20:15 INFO ObjectifyFactoryBean:115 - Registered entity class [com.app.db.client.model.ProductCategory] 
[INFO] 2015-09-10 13:20:15 INFO ObjectifyFactoryBean:115 - Registered entity class [com.app.db.client.model.ProductType] 
[INFO] 2015-09-10 13:20:15 INFO ObjectifyFactoryBean:115 - Registered entity class [com.app.db.client.model.Vendor] 

当我试图保存产品实体:

Product product = new Product(); 
product.setName("new product"); 
product.setProductType(productType); 
product.setProductCategory(productCategory); 
product.setNativeId(productNativeId); 
product.setCostPrice(createProductParam.getCostPrice()); 
dbclient.createObject(product); 

我从客体得到这个错误:

[INFO] java.lang.IllegalArgumentException: No class 'com.app.db.client.model.ProductType' was registered 
[INFO] at com.googlecode.objectify.impl.Registrar.getMetadataSafe(Registrar.java:120) 
[INFO] at com.googlecode.objectify.impl.Keys.getMetadataSafe(Keys.java:53) 
[INFO] at com.googlecode.objectify.impl.Keys.getMetadataSafe(Keys.java:62) 
[INFO] at com.googlecode.objectify.impl.Keys.rawKeyOf(Keys.java:36) 
[INFO] at com.googlecode.objectify.impl.Keys.keyOf(Keys.java:29) 
[INFO] at com.googlecode.objectify.Key.create(Key.java:62) 
[INFO] at com.googlecode.objectify.Ref.create(Ref.java:31) 
[INFO] at com.app.db.client.model.Product.setProductType(Product.java:93) 

请帮我解决这个问题。

回答

0

自2012年以来,弹簧扩展并未更新,因此它完全可能不适用于当前版本的Objectify。我不知道 - 我会联系作者。

问题是您的ProductType实体尚未注册。据推测,春季延长应该这样做,但不是。

0

像@stickfigure已经提到过,这个库已经很久没有更新。这就是说它所依赖的Objectify版本是 - 2.2.x.

但是从您的日志记录看来,实体似乎已被注册。

为了验证它是否具有最新的物化版本你可以工作:

  1. 克隆从https://github.com/marceloverdijk/objectify-appengine-spring
  2. 更新的lib的客观化版本
  3. 运行测试

如果这样的作品你至少知道这个lib可以使用最新的Objectify版本。

+0

它适用于具有简单属性的实体。产品实体发生此问题,其中它具有对其他实体的引用(Ref)。 – Amit

1

我得到了同样的问题,并在下面的步骤解决。 1)编写你自己的ObjectifyFactoryBean(从https://github.com/marceloverdijk/objectify-appengine-spring复制)并更新afterPropertiesSet()方法中的一行。

this.objectifyFactory = new ObjectifyFactory(); 
// Set the factory to ObjectifyService 
ObjectifyService.setFactory(objectifyFactory); 

2)春季

<bean id="objectifyFactory" class="com.yourcompany.ObjectifyFactoryBean" > 
     <property name="basePackage" value="com.yourcompany.model" /> 
    </bean> 

3)使用objectifyFactory在你的DAO类如春豆使用此功能。

4)在你的web.xml中添加过滤器。

<!-- ObjectifyFilter filter --> 
<filter> 
    <filter-name>objectifyFilter</filter-name> 
    <filter-class>com.googlecode.objectify.ObjectifyFilter</filter-class> 
</filter> 
<filter-mapping> 
    <filter-name>objectifyFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

说明:我不知道物化工作如何旧版本,并基于该ObjectifyFactoryBean写的,但在物化5.x中的最新版本,它在内部使用ObjectifyService在参考操作,这是使用不同objectifyFactory因此,为了使它在整个应用程序中使用objectifyFactory的同一个实例,我们在ObjectifyFactoryBean类中设置了ObjectifyService.setFactory(objectifyFactory)。

在Web应用程序中也需要过滤器ObjectifyFilter,因为此过滤器将为Objectify会话调用ObjectifyService.begin(),通常只有在我们执行数据存储操作时调用ObjectifyService.begin(),但遇到类似情况Ref操作,ObjectifyFilter将为我们完成这项工作。

希望能解决这个问题!

相关问题