2011-05-10 56 views
0

我創建了一個具有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(); 

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; 
     ... 
    } 
     ... 
} 

如果我改變這樣

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; 
     ... 
    } 
     ... 
} 

事件類以上相同的代碼返回一個列表,在這一個事件(我的預期)

編輯:這是地方類

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(); 

} 

} 

這是User類

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的,但正確的認識它不會做任何有用的,所以我改回了模型。 我使用播放錫耶納1.5玩1.1

+0

你能給我你的用戶代碼,以便我可以試試嗎?什麼是MyModel類也是? – mandubian 2011-05-10 22:41:11

回答

1

我發現你的問題:)

這是一個已知的問題,但我忘了,每次它。
在你的情況下,簡單地宣佈@Column:

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

    @Column("user") 
    public User user; 

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

用戶和地點有一個名爲「ID」和鍵字段名稱默認使用錫耶納時,有沒有@Column每一個關鍵領域, GAE嘗試通過字段「id」查找對象時會發生碰撞。
我會嘗試解決此錫耶納V1.0.0(你已經可以用siena.jar從V1.0.0幹線透明地播放產生的)

問候 帕斯卡爾

+0

謝謝!有用。您應該刪除此行「除@Id之外的其他任何註釋均被忽略。」來自GAE文檔http://www.sienaproject.com/documentation-gae.html再次感謝 – 2011-05-16 14:01:35

+0

我將完全重構文檔,因爲我更願意從新入門版本1.0.0中改變很多東西刮。但如果你使用的是舊版本,不要擔心,向後兼容性已經被測試了很多;) – mandubian 2011-05-16 14:09:17