I'm trying to make following JDO entity in GAE/J (I'm using Gilead).
package test.domains;
import java.io.Serializable;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import net.sf.gilead.pojo.java5.LightEntity;
import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Key;
#PersistenceCapable(identityType=IdentityType.APPLICATION, detachable="true")
public class Banner extends LightEntity implements Serializable
{
private static final long serialVersionUID = 1058354709157710766L;
// Fields
#PrimaryKey
#Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
private Key id;
#Persistent
private String name;
#Persistent
private String sizeX;
#Persistent
private String sizeY;
#Persistent
private String description;
#Persistent
private Blob img;
// Getters and Setters
}
And encountering following problem:
[ERROR] Line 40: No source code is
available for type
com.google.appengine.api.datastore.Blob;
did you forget to inherit a required
module?
What can cause this problem? The code compiles fine without Blob object. By the way I tried to follow this example.
As far as i can tell, it is Gilead that does not have support for com.google.appengine.api.datastore.Blob.
The adapter4appengine-1.0M2.jar on contains an emulator class for 'com.google.appengine.api.datastore.Key'
Are you keeping that file in the client side? That's the only reason I can think GWT is not finding the Blob .class file.
Give it a shot.
Jaime E
Related
I'm using spring-data -mongodb to do crud operations (create , read , update , delete), but
the delete function doesn't work and I don't know why? . Here is my code.
import org.bson.types.Binary;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
import org.springframework.data.mongodb.core.mapping.Field;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
#ToString
#Getter
#Setter
#Document(collection = "patterns")
public class Pattern {
#Id
#Field
private String id;
#Field
#Indexed(unique = true)
private String name;
#Field
private String status;
#Field
private Binary patternFile;
}
import java.util.List;
import org.springframework.data.mongodb.repository.MongoRepository;
import fod.pfe7.administratorportal.domain.Pattern;
public interface PatternRepository extends MongoRepository<Pattern, String> {
List<Pattern> findByName(String name);
}
in my controller I do .
patternRepository.findByName(patternName).forEach(pattern -> {
patternRepository.deleteById(pattern.getId());
result.put("status", 0);
result.put("message", "pattern deleted successfuly");
});
Your data might have been created by other system, which use different presentation of "_id" (says String). With newer version of spring data mongodb, you can use #MongoId instead of #Id to control this.
In my case, the later deletes record correctly.
#Id private String id; produces
Remove using query: { "_id" : { "$oid" : "60ed51ce597826297941ade4"}} in collection: sample.
#MongoId(FieldType.STRING) private String id; produces
Remove using query: { "_id" : "60ed51ce597826297941ade4"} in collection: sample.
Maybe a little late...
I got the same error and saw that the delete query did not include the Object_Id, so the findByID method was not working either.
The solution for me was to include the targetType in the #Field annotation:
#Field(name = "_id", targetType = FieldType.OBJECT_ID)
Greendao not generating import of ToMany joiner dao. How can I do this?
I'm creating Book and BookStore, trying save list of books in book store by Custom joiner. After build trying generated Joiner JoinBookStoreWithBookDao not importing in BookStoreDao but exists.
Sources
Book.java
package com.example.valery.stackoverflowsample.dao;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Generated;
#Entity
public class Book {
#Id
private long id;
public Book() {
}
}
BookStore.java
package com.example.valery.stackoverflowsample.dao;
import com.example.valery.stackoverflowsample.dao.joiner.DaoSession;
import com.example.valery.stackoverflowsample.dao.joiner.JoinBookStoreWithBook;
import org.greenrobot.greendao.DaoException;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.JoinEntity;
import org.greenrobot.greendao.annotation.ToMany;
import java.util.ArrayList;
import java.util.List;
#Entity
public class BookStore {
#Id
private long id;
#ToMany
#JoinEntity(
entity = JoinBookStoreWithBook.class,
sourceProperty = "bookStoreId",
targetProperty = "bookId"
)
private List<Book> mBooks;
}
JoinBookStoreWithBook.java
package com.example.valery.stackoverflowsample.dao.joiner;
import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Generated;
#Entity
public class JoinBookStoreWithBook {
#Id
private long id;
private long bookId;
private long bookStoreId;
}
I found reason. Joiner should be inside of package for "parent", he can't be in another package.
My Class heirarchy is as follows
School - contains list of Employees - which contains list of qualifications
Employees is an Embedded list in School. I can persist a School with it's employees no problem. Now when I add the list of qualifications to an employee as an embedded field I get the following error
You cannot nest multiple #Embedded arrays or collections
The objectify documentation seems to indicate I should be able to do this provided the objects are serializable which they are. Am I missing something? If this is the way it works is there a way around it?
Update:
School Class
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Embedded;
import javax.persistence.Id;
import com.googlecode.objectify.annotation.Entity;
#Entity
#SuppressWarnings("serial")
public class School implements Serializable
{
#Id
private String title;
#Embedded
private List<Employee> employees = new ArrayList<Employee>();
public School ()
{
}
public School (String title)
{
this.title = title;
}
public void addEmployee( Employee employee )
{
this.employees.add(employee);
}
}
Employee Class
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Embedded;
import javax.persistence.Id;
import com.googlecode.objectify.annotation.Entity;
#Entity
#SuppressWarnings("serial")
public class Employee implements Serializable
{
#Id
private String title;
#Embedded
private List<String> qualifications = new ArrayList<String>();
public Employee ()
{
}
public Employee (String title)
{
this.title = title;
}
public void addQualification( String qualification )
{
this.qualifications.add(qualification);
}
}
Caused by: java.lang.IllegalStateException: You cannot nest multiple #Embedded arrays or collections. A second was found at private java.util.List com.app.nquizitive.shared.Employee.qualifications
at com.googlecode.objectify.impl.save.EmbeddedMultivalueFieldSaver.<init>(EmbeddedMultivalueFieldSaver.java:36)
at com.googlecode.objectify.impl.save.EmbeddedCollectionFieldSaver.<init>(EmbeddedCollectionFieldSaver.java:21)
at com.googlecode.objectify.impl.save.ClassSaver.<init>(ClassSaver.java:64)
at com.googlecode.objectify.impl.save.EmbeddedMultivalueFieldSaver.<init>(EmbeddedMultivalueFieldSaver.java:43)
at com.googlecode.objectify.impl.save.EmbeddedCollectionFieldSaver.<init>(EmbeddedCollectionFieldSaver.java:21)
at com.googlecode.objectify.impl.save.ClassSaver.<init>(ClassSaver.java:64)
at com.googlecode.objectify.impl.save.ClassSaver.<init>(ClassSaver.java:29)
at com.googlecode.objectify.impl.Transmog.<init>(Transmog.java:322)
at com.googlecode.objectify.impl.ConcreteEntityMetadata.<init>(ConcreteEntityMetadata.java:75)
at com.googlecode.objectify.impl.Registrar.register(Registrar.java:69)
at com.googlecode.objectify.ObjectifyFactory.register(ObjectifyFactory.java:209)
at com.googlecode.objectify.ObjectifyService.register(ObjectifyService.java:38)
at com.app.nquizitive.server.dao.SchoolDao.<clinit>(SchoolDao.java:12)
There are two different annotations:
#Embed (#Embedded in ofy3)
#Serialize (#Serialized in ofy3)
If you want something to serialize, use the second. If you want something embedded, use the first. You can't nest #Embed(ded) lists, but you can put a #Serialize(d) list inside an embedded list.
Which of the classes above are annotated with #Entity? It sounds like School is a datastore entity, while Employees are not (i.e. they are just serialized into School) and qualifications are not (i.e. they are just serialized into Employees).
The Objectify annotation of #Embedded isn't needed/relevant/allowed, in a non-Entity class.
I upgraded my application to use Objectify4, but I can't get the ordering get working.
Here is what I did:
I have a class Offer which I want to query. This class is extended from Mail and from Model. The attribute for order should be the datetime which is indexed in the Mail-Class.
import com.googlecode.objectify.annotation.EntitySubclass;
import com.googlecode.objectify.annotation.Serialize;
#EntitySubclass(index=true)
public class Offer extends Mail {
private static final long serialVersionUID = -6210617753276086669L;
#Serialize private Article debit;
#Serialize private Article credit;
private boolean accepted;
...
}
import com.googlecode.objectify.annotation.EntitySubclass;
import com.googlecode.objectify.annotation.Index;
#EntitySubclass(index=true)
public class Mail extends Model {
private static final long serialVersionUID = 8417328804276215057L;
#Index private Long datetime;
#Index private String sender;
#Index private String receiver;
...}
import java.io.Serializable;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Ignore;
import com.googlecode.objectify.annotation.Index;
#Entity
public class Model implements Serializable {
private static final long serialVersionUID = -5821221296324663253L;
#Id Long id;
#Index String name;
#Ignore transient private Model parent;
#Ignore transient private boolean changed;
...}
import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
public class DatabaseService {
static {
ObjectifyService.register(Model.class);
ObjectifyService.register(Mail.class);
ObjectifyService.register(Offer.class);
}
public static Objectify get() {
return ObjectifyService.ofy();
}
}
and that's what I want to do:
Query<Offer> result = DatabaseService.get().load().type(Offer.class).order("-datetime");
Unfortunely, the result is always NOT sorted.
Has anyone a hint?
At the low-level, this load operation has two parts:
filter by ^i = Offer
order by datetime desc
In order to make it work, you will need a multiproperty index like this:
<datastore-index kind="Model" ancestor="false">
<property name="^i" direction="asc"/>
<property name="datetime" direction="desc"/>
</datastore-index>
However, you are almost certainly abusing the datastore by making all your entities extend a polymorphic Model. You will have many problems in the future if you try to cram all of your entities into a single Kind; for one thing, practically every query will require a multiproperty index including the discriminator.
You can have a common base class, just don't make it the Kind. Keep the inheritance hierarchy, but move the #Entity up to (say) Mail. Offer can still have #EntitySubclass if you want a true polymorphic hierarchy there.
Read the objectify Concepts docs carefully and pick your Kinds carefully.
This question already has answers here:
Creating Indexes on DB with Hibernate #Index Annotation
(4 answers)
Closed 7 years ago.
i have a java class used as an entity that has 2 classes that inherit from it. this class has some indices but these indices didn't appear in the database. this is my java super class code
import java.io.Serializable;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.persistence.Version;
import javax.persistence.OneToMany;
import org.hibernate.annotations.GenericGenerator;
#Entity
#Table(name="service", uniqueConstraints = {#UniqueConstraint(columnNames={"name"})})
#org.hibernate.annotations.Table(appliesTo = "service",
indexes = { #Index(name = "service_name", columnNames = { "name" }),
#Index(name = "service_description", columnNames = { "description" }),
#Index(name = "service_accessNumber", columnNames = { "access_number" })
})
public class Service implements Serializable {
#Column(name="access_number",length = 95,nullable=false)
String accessNumber;
#Column(length=80,nullable=false)
String name;
#Column(length=140)
String description;
}
does any one know what is my problem
Note: i have this problem in my all java classes but this is one of them. the code in all class is the same of this
Edit: i build an xml file and put it in a grails project, and when i run this project, database created
Would a single #Table annotation work? I haven't tried it, I guess the Hibernate #Table might be overridden by JPA #Table.
You may also try #Index annotation on the column fields:
public class Service implements Serializable {
#Index(name="service_accessnumber")
#Column(name="access_number",length = 95,nullable=false)
String accessNumber;
#Index(name="service_name")
#Column(length=80,nullable=false)
String name;
#Index(name="service_description")
#Column(length=140)
String description;
}
i have the same problem, but i found it's solution and it works fine with me try it, it may help you
in your DataSource.groovy file in your grails project make sure that under
environment dbCreate is not equal to "update": if it is equal to "update", change
it to "create".
This works fine just try it