stivlo
stivlo

Reputation: 85546

Alternatives to com.google.appengine.api.datastore.Text?

In a Google App Engine JPA Entity a java.lang.String is limited to 500 character, while a com.google.appengine.api.datastore.Text has an unlimited size (with the caveat that entityManager.persist doesn't work with entities bigger than 1 Mb - I believe, but can't find the reference about it).

However, if I use the Google specific Text type, I would tightly couple my application with Google App Engine. I was wondering if there is a more lightweight way to achieve the same result, such as through some standard annotation.

Maybe, annotating a String with a JSR-303 annotation. If the size is above 500, it would know to use the non-indexed Text type. Example: @Size(max = 3000)

Obvsiously I'm just dreaming, but maybe there is some standard way to avoid App Engine specific data types. Anybody knows?

UPDATE: found issue 10, Support @Lob JPA annotation in datanucleus-appengine DataNucleus plugin for Google App Engine.

Upvotes: 1

Views: 674

Answers (1)

Dave
Dave

Reputation: 6189

I'm guessing you might have thought of this already; but I'm posting this anyway since it adds a point not covered in your question. My solution here was to make it private, and then for my object definition I don't ever return a Text object. I admit this doesn't accomplish your goal of avoiding the App Engine specific data type; but it at least makes it so that you don't have to rewrite code outside of the one class which relies on the Text object should you decide to port your application off of app engine.

So, for example, assuming text is a blogPost variable...

public class BlogPost {
  private Text post;

  // ...

  public String getPost() {
    return post.getValue();
  }

  public void setPost(String val) {
    this.post = new Text(val);
  }
}

If that code feels a little sloppy it might be slightly (I don't know how the Text#getValue method works internally) more efficient to do this...

public class BlogPost {
  private Text post;
  private String postValue;
  // ...

  public String getPost() {
    return postValue;
  }

  public void setPost(String val) {
    this.post = new Text(val);
    this.postValue = val;
  }
}

Upvotes: 2

Related Questions