我正在创建一个具有2个一对多关系的实体。事件具有用户字段和位置字段。我正在尝试使用自动查询,但是此代码始终返回一个空列表。

    User user = new User("[email protected]","Maurizio Pozzobon","01","hash","facebook");
    user.insert();
    Place place = new Place("posto","bel posto",null,null);
    place.insert();
    Event e =new Event(user,place, "Festa","Questa è una gran bella festa",null,new Date(),(long) 10,false,null);
    e.insert();
    List<Event> l =user.events.fetch();


事件类是

public class Event extends Model{
    @Id
    public Long id;

    ...

    //Relazioni
    public User user;
    public Place place;

        ...

    public Event(User user, Place place,String nome, String descrizione, String uRLImmagine, Date dataInizio, Long durata, Boolean isRicorrente, Long ricorrenza) {
        this.user=user;
        this.place=place;
        ...
    }
        ...
}


如果我这样更改Event类

public class Event extends Model{
    @Id
    public Long id;

    ...

    //Relazioni
    public User user;
    //public Place place;

        ...

    public Event(User user, Place place,String nome, String descrizione, String uRLImmagine, Date dataInizio, Long durata, Boolean isRicorrente, Long ricorrenza) {
        this.user=user;
        //this.place=place;
        ...
    }
        ...
}


上面的相同代码返回一个包含一个事件的列表(我所期望的)

编辑:这是Place类

public class Place extends Model {



@Id
public Long id;

public String nome;
public String descrizione;
public String uRLImmagine;
public String indirizzo;


//Relazioni
// public User user;
//@Filter("place")
//public Query<Event> events;
private Set<Long> idEvents = new HashSet<Long>();
private Set<Long> idPlaceVotes = new HashSet<Long>();
private Set<Long> idPlaceComments = new HashSet<Long>();


public Place(/*User user,*/ String nome, String descrizione, String uRLImmagine,String indirizzo) {
//  this.user=user;
    this.nome = nome;
    this.descrizione = descrizione;
    this.uRLImmagine = uRLImmagine;
    this.indirizzo = indirizzo;
}


static Query<Place> all() {
    return Model.all(Place.class);
}

public static Place findById(Long id) {
    return all().filter("id", id).get();
}

public String toString() {
    return nome;
}

public static void delete(Long id) {
    findById(id).delete();

}

}


这是用户类

public class User extends Model {

@Id
public Long id;

public String nome;
public String email;
public String webId;    //ID of the user in the provider website
public String passwordHash;
public String service;

//Relazioni
@Filter("user")
public Query<Event> events;

public User(String email, String name,String webId, String passwordHash, String service) throws Exception {
    if (email!=null)
        this.email=email;
    else
        throw new Exception("An email is required");
    if (name!=null)
        this.nome=name;
    else
        throw new Exception("A name is required");

    if (webId!=null)
        this.webId=webId;
    else
        throw new Exception("A webId is required");

    this.passwordHash=passwordHash;
    this.service = service;
}

public void setEmail(String email) throws Exception{
    if (email!=null)
        this.email=email;
    else
        throw new Exception("An email is needed");
}

static Query<User> all() {
    return Model.all(User.class);
}

public static User findById(Long id) {
    return all().filter("id", id).get();
}

public static User findByEmail(String email){
    return all().filter("email", email).get();
}

public String toString() {
    return nome;
}

}


MyModel是siena.Model的超类,但是正确地知道它没有任何用处,因此我将其改回了Model。
我在播放1.1上使用play-siena 1.5

最佳答案

我发现了你的问题:)

这是一个已知问题,但我每次都会忘记。
在您的事件中,只需声明@Column:

public class Event extends Model{
    @Id
    public Long id;

    @Column("user")
    public User user;

    @Column("place")
    public Place place;
}


由于User和Place都有一个名为“ id”的键字段,并且当没有@Column时,Siena默认使用键字段名,因此当GAE尝试通过字段“ id”查找对象时会发生冲突。
我将尝试在Siena v1.0.0中更正此问题(并且您已经可以在Play中透明地使用从v1.0.0干线生成的siena.jar)

问候
帕斯卡

09-28 11:56