I have two different boolean variables that are set very similarly and I want to create a generic method that can handle two different class objects as the argument.
Note: Area and HuntingField are two entities I've created using ebean.
Currently this is what I do in order to set the two booleans:
boolean isNewArea = (Area.find.where().eq("id", area.id)
.findRowCount() == 1) ? false : true;
if (isNewArea) {
area.save();
}
boolean isNewHuntingField = (HuntingField.find.where()
.eq("id", hf.id).findRowCount() == 1) ? false : true;
if (isNewHuntingField) {
hf.save();
These are the two different find methods defined in the corresponding classes:
public static Finder<String, HuntingField> find = new Finder<String, HuntingField>(
String.class, HuntingField.class);
public static HuntingField find(String searchString) {
return find.where().eq("id", searchString).findUnique();
}
and this:
public static Finder<String, Area> find = new Finder<String, Area>(
String.class, Area.class);
public static Area find(String searchString) {
return find.where().eq("id", searchString).findUnique();
}
Now I want to create a method that takes either a HuntingField type object or an Area type object as a parameter and uses the corresponding find method on that object and returns a boolean value:
public static <T> boolean alreadyExist(Class<T> obj){
boolean isNewArea = (obj.find.where().eq("id", obj.id)
.findRowCount() == 1) ? false : true;
But eclipse is telling me that find and id cannot be resolved or is not a field.
What am I doing wrong?
java.lang.Class doesn't have a field called id. See the javadoc here
You are mistaking the class for the finder.
public static <T> boolean alreadyExist(Finder<String, T> obj){
boolean isNew = (obj.find.where().eq("id", obj.id)
.findRowCount() == 1) ? false : true;
boolean huntingFieldExists = alreadyExist(new Finder<String, HuntingField>());
boolean areaExists = alreadyExist(new Finder<String, Area>());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.