Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 播放2.0使用复合(嵌入)键创建查找器:工作不正常_Java_Jpa_Playframework 2.0_Ebean - Fatal编程技术网

Java 播放2.0使用复合(嵌入)键创建查找器:工作不正常

Java 播放2.0使用复合(嵌入)键创建查找器:工作不正常,java,jpa,playframework-2.0,ebean,Java,Jpa,Playframework 2.0,Ebean,我有一个叫Totals的模型 @Entity public class Totals extends Model{ @EmbeddedId private TotalsPK id; public Totals(TotalsPK key, Integer _count) { id = key; count = _count; } public static Finder<TotalsPK,Totals> find = new Finder<TotalsPK, T

我有一个叫Totals的模型

@Entity
public class Totals extends Model{
@EmbeddedId
private TotalsPK id;
public Totals(TotalsPK key, Integer _count)
{
    id = key;
    count = _count;
}

public static Finder<TotalsPK,Totals> find = new Finder<TotalsPK, Totals> (
        TotalsPK.class, Totals.class
);

public static Totals find(TotalsPK id) 
 {
    //try this way instead of relying on find.byId working..... same error though!
    return find.where().eq("user_id", id.getUserId()).eq("item_id", id.getItemId()).findUnique();
//  return find.byId(id);
 }
........... etc
当搜索一个不存在的记录时,这很好,但是当搜索一个确实存在的记录时,从Ebean传递给“equals”的对象是空的,我不知道这是为什么,知道我这里做错了什么吗

Null检查传递给equals的rhs值可以阻止它崩溃,但是equals检查永远不会被命中


谢谢< /P>你应该使用等长的比较:<代码>(UsRID)(((ToSSPK)RHS).GETUSERID())和(.To.ToMeIdId)(((ToSSPK)RHS).GETITEMID())在主键类中。当然,谢谢,(将在C++中工作!)我的问题仍然存在,尽管已修复!为了完整起见,

Model
的源代码是什么?它不是我的代码,所以不确定粘贴在这里是否有好处。但播放API页面,因为它在这里:您还应该在equals/hashCode()上使用
@Override
注释。Equals方法应该首先检查空值,并进行类型检查,正如在有效的Java中所示。
@Embeddable
public class TotalsPK {

private Long userId;
private Long itemId;

public TotalsPK(Long _userId, Long _itemId)
{
    userId = _userId;
    itemId = _itemId;
}

public boolean equals(Object rhs)
{
    return (userId.equals(((TotalsPK)rhs).getUserId()) && itemId.equals(((TotalsPK)rhs).getItemId()));
}

public int hashCode()
{
    //from Effective Java Chapter 3
    int result = (int) (userId ^ (userId >>> 32));
    result = 31 * result + (int) (itemId ^ (itemId >>> 32));
    return result;  
}