2

I have a model called 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

And then I have my key class

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

This works fine when searching for a record which doesnt exist, but when searching for one that does exist the object passed to "equals" from Ebean is null and I have no idea why this is, any ideas what I am doing wrong here?

Null checking the rhs value passed into equals stops it crashing, but the equals check is never hit

thanks

Stowelly
  • 1,260
  • 2
  • 12
  • 31
  • 1
    You should use equals for Long comparison: `(userId.equals(((TotalsPK)rhs).getUserId()) && itemId.equals(((TotalsPK)rhs).getItemId()))` in your primary key class. – dcernahoschi Nov 20 '12 at 17:05
  • ah yes of course, thank you (would have worked in c++!!). my problem still persists though with that fixed! – Stowelly Nov 20 '12 at 18:21
  • just for the sake of completeness what is the source code of `Model`? – Boris Treukhov Nov 26 '12 at 16:55
  • It isnt my code so not sure if pasting it here would do much good. but plays API page for it is here: http://www.playframework.org/documentation/api/2.0.4/java/play/db/ebean/Model.html – Stowelly Nov 26 '12 at 17:11
  • 2
    also you should use `@Override` annotation on equals/hashCode(). Equals method should first check for nulls anyway and do type check, as it was shown in Effective Java. – Boris Treukhov Nov 26 '12 at 17:14
  • also why your field id/userId(?) is queried as user_id? maybe this link is relevant http://stackoverflow.com/questions/4084431/jpa-criteria-api-and-embeddedid – Boris Treukhov Nov 26 '12 at 17:20
  • @Stowelly, ah I see that it's playframework class. – Boris Treukhov Nov 26 '12 at 17:23
  • will add the override annotation, i mentioned the null check in my post, it stops it crashing but the == is never hit. and "user_id" is what play / ebean generates based on my variable userId – Stowelly Nov 26 '12 at 17:24
  • is it working with finder `.byId()`? – Boris Treukhov Nov 26 '12 at 17:31
  • tbh `"id"` or `"id.userId"` would look better, what a strange framework :-) – Boris Treukhov Nov 26 '12 at 17:44
  • Could you post the full stacktrace (with the NullPointerException)? – Jasper Nov 27 '12 at 08:51
  • the result is identical when using .byId(). here is the stack trace: TotalsPK.equals(Object) line: 33 CQuery.setLoadedBean(Object, Object) line: 483 SqlTreeNodeRoot.postLoad(DbReadContext, Object, Object) line: 34 SqlTreeNodeRoot(SqlTreeNodeBean).load(DbReadContext, Object, int) line: 365 CQuery.readRow() line: 560 CQuery.readBeanInternal(boolean) line: 594 CQuery.readBean() line: 572 CQueryEngine.find(OrmQueryRequest) line: 277 DefaultOrmQueryEngine.findId(OrmQueryRequest) line: 154 OrmQueryRequest.findId() line: 310 – Stowelly Nov 27 '12 at 08:59
  • DefaultServer.findId(Query, Transaction) line: 1214 DefaultServer.find(Class, Object, Transaction) line: 1118 DefaultServer.find(Class, Object) line: 1105 Model$Finder.byId(I) line: 237 Totals.find(TotalsPK) line: 82 – Stowelly Nov 27 '12 at 08:59

0 Answers0