Java 与锡耶纳的多重关系
我正在创建一个具有2个一对多关系的实体。事件有一个用户字段和一个地点字段。我试图使用自动查询,但这段代码总是返回一个空列表Java 与锡耶纳的多重关系,java,google-app-engine,playframework,siena,Java,Google App Engine,Playframework,Siena,我正在创建一个具有2个一对多关系的实体。事件有一个用户字段和一个地点字段。我试图使用自动查询,但这段代码总是返回一个空列表 User user = new User("mauriziopz@gmail.com","Maurizio Pozzobon","01","hash","facebook"); user.insert(); Place place = new Place("posto","bel posto",null,null); place.insert
User user = new User("mauriziopz@gmail.com","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;
...
}
...
}
上面的代码返回一个列表,其中包含一个事件(我所期望的)
编辑:这是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();
}
}
公共类场所扩展模型{
@身份证
公共长id;
公共字符串名称;
公共字符串描述;
公共字符串URLIMAGINE;
公共字符串indirizzo;
//里拉齐奥尼
//公共用户;
//@过滤器(“位置”)
//公开查询事件;
私有集idEvents=newhashset();
私有集idPlaceVotes=新哈希集();
私有集idPlaceComments=新HashSet();
公共场所(/*User-User、*/String-nome、String-descripione、String-urlimagine、String-indirizzo){
//this.user=user;
this.nome=nome;
this.descripione=descripione;
this.urlimagine=urlimagine;
this.indirizzo=indirizzo;
}
静态查询全部(){
返回模型.all(Place.class);
}
公共静态位置findById(长id){
返回all().filter(“id”,id).get();
}
公共字符串toString(){
返回nome;
}
公共静态无效删除(长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;
}
}
公共类用户扩展模型{
@身份证
公共长id;
公共字符串名称;
公共字符串电子邮件;
public String webId;//提供程序网站中用户的ID
公共字符串密码哈希;
公共字符串服务;
//里拉齐奥尼
@过滤器(“用户”)
公开查询事件;
公共用户(字符串电子邮件、字符串名称、字符串webId、字符串密码哈希、字符串服务)引发异常{
如果(电子邮件!=null)
this.email=电子邮件;
其他的
抛出新异常(“需要电子邮件”);
if(name!=null)
this.nome=名称;
其他的
抛出新异常(“需要名称”);
if(webId!=null)
this.webId=webId;
其他的
抛出新异常(“需要webId”);
this.passwordHash=passwordHash;
服务=服务;
}
public void setEmail(字符串电子邮件)引发异常{
如果(电子邮件!=null)
this.email=电子邮件;
其他的
抛出新异常(“需要电子邮件”);
}
静态查询全部(){
返回Model.all(User.class);
}
公共静态用户findById(长id){
返回all().filter(“id”,id).get();
}
公共静态用户findByEmail(字符串电子邮件){
return all().filter(“email”,email).get();
}
公共字符串toString(){
返回nome;
}
}
MyModel是siena.Model的一个超级类,但我知道它没有任何用处,所以我把它改回了Model。
我在play 1.1上使用play siena 1.5,我发现了您的问题:)
这是一个已知的问题,但每次我都忘记了。在您的事件中,只需声明@列:
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中纠正这一点(您已经可以在游戏中透明地使用从v1.0.0 trunk生成的Siena.jar) 问候
Pascal你能给我你的用户代码让我试试吗?什么是MyModel课程?谢谢!它起作用了。您可能应该删除这一行“任何其他注释,但@Id被忽略”。在GAE文档中,我将完全重构文档,因为新版本1.0.0中有太多内容发生了更改,所以我更喜欢从头开始。但如果您使用的是较旧的版本,请不要担心,向后兼容性已经过很多测试;)
public class Event extends Model{
@Id
public Long id;
@Column("user")
public User user;
@Column("place")
public Place place;
}