开发者

Spring ROO GWT Select by reference

I created a GWT Spring ROO project with the following entities: Facture and ItemFacture.

ItemFacture contains a reference to a Facture.

@RooJavaBean
@RooToString
public class ItemFacture {

    @ManyToOne
    private Facture facture;
...

This is the code for Facture :

@RooJavaBean
@RooToString
@RooEntity
public class Facture {

    private String nom;
    private String type;
}

and everything went fine until I wanted to create a custom finder that selects all the ItemFactures that contains a specific Facture :

@SuppressWarnings("unchecked")
public开发者_开发技巧 static List<ItemFacture> findByFacture(Facture facture) {

    Query q = entityManager().createQuery("SELECT o FROM ItemFacture AS o WHERE o.facture = :facture");
    q.setParameter("facture", facture);

    return q.getResultList();
}

When I try to execute this finder, it gives me this error:

Server Error: Field "facture" does not exist in com.test.ItemFacture or is not persistent; nested exception is javax.persistence.PersistenceException: Field "facture" does not exist in com.test.ItemFacture or is not persistent

I created a few custom finders and every one of them worked just fine except this one.

I tried adding @Persistent to the field and @PersistenceCapable(identityType = IdentityType.APPLICATION) to Facture but it still doesn't work.

Anyone know what is the problem?


i guess the reason why this doesn't work is that the ItemFacture class is not an @Entity so JPA has no idea this field exists. with annotating the ItemFacture class with @RooEntity roo generates a ItemFactures_Roo_Entity.aj (STS hides these files in the package explorer by default) which wires the @Entity annotation on your ItemFacture-class together with some other stuff making it an ActiveRecord (http://martinfowler.com/eaaCatalog/activeRecord.html)


In AppEngine, references are made with the ID of the referenced object. So event if ItemFacture contain and attribute facture, it's actually stored as factureId.

So the method to write is this one:

@SuppressWarnings("unchecked")
public static List<ItemFacture> findByFacture(Facture facture) {

    Query q = entityManager().createQuery("SELECT o FROM ItemFacture AS o WHERE o.factureId = :facture");
    q.setParameter("facture", facture.getId());

    return q.getResultList();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜