Ebean在查询中使用And

时间:2015-07-18 12:30:09

标签: java mysql playframework ebean

我正在尝试进行查询,以查看帐户信息是否处于活动状态(boolean = True)以及该用户的信息(来自Model的用户ID)。在SQL中,我会像

一样写
Select * from mst_account where user=1 and active = 1

我想在Play框架中使用Ebean来做这件事。

如何使用ebean编写此类查询。

模型如下所示,用户帐户

    @Table(name="mst_user_account")

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

    public String email="";

    public String username="";

    public String password="";

    @Formats.DateTime(pattern = "yyyy-MM-dd hh:mm:ss")
    public Date createdOn=new Date();

    @Formats.DateTime(pattern="yyyy-MM-dd hh:mm:ss")
    public Date activatedOn;

    public String activationCode="";


    public Boolean activationStatus=false;

    public Boolean accountStatus=false;

    @ManyToOne
    public Roles role;



    public static Finder<Long,UserAccount> find=new Finder<Long,UserAccount>(Long.class,UserAccount.class);

    public static UserAccount findByEmail(String email){

        return UserAccount.find.where().eq("email",email).findUnique();
    }


    public static UserAccount findByUsername(String username){

        return UserAccount.find.where().eq("username",username).findUnique();

    }

    public static UserAccount findByActivationCode(String code){
        return UserAccount.find.where().eq("activationCode",code).findUnique();
    }


    public static UserAccount authenticate(String username,String password){
        return UserAccount.find.where().eq("username",username).eq("password",password).findUnique();
    }

//mod seroney to take care of display of username----
    public static UserAccount isLoggedIn() {
        String uuid = session("id");
        UserAccount user = UserAccount.find.byId(Long.parseLong(uuid));
        return user;


    }

帐户模型

@Entity
@Table(name="mst_account")
public class Account extends Model {
    @Id
    public Long id;

    public String firstName="";

    public String surname = "";

    public String otherName="";

    public String address="";

    public String city="";

    public String country="";

    public String mobile="";

    public String zipcode="";

    public String stateprv ="";

    public String usrtimezone ="";

    public Boolean physicalDisability =false;

    public String physicalDisabilityDesc ="";

    @OneToOne
    public ModeOfStudy modeOfStudy;

    @OneToOne
    public Campus campus;

    @OneToOne
    public CourseMst course;

    public String hschatt ="";

    public String hsgpa ="";

    public Qualifications hschyr;

    public String unvatt ="";

    public String ungpa ="";

    public Qualifications unvyr;

    public Boolean isprocessed = false;

    public Boolean isapproved = false;

    public Boolean isPosted = false;

    @Formats.DateTime(pattern = "yyyy-MM-dd hh:mm:ss")
    public Date createdOn=new Date();

    @OneToOne
    public UserAccount user;
    @OneToOne
    public UserAccount isapprovedby;


    public static Finder<Long,Account> find=new Finder<Long,Account>(Long.class,Account.class);

    public static Account findByMobile(String mob){

        return Account.find.where().eq("mobile",mob).findUnique();
    }

    public static Account findByAltPhone(String phone){

        return Account.find.where().eq("alternatePhone",phone).findUnique();
    }

    public static Account findByCardNumber(String card){

        return Account.find.where().eq("cardNumber",card).findUnique();

    }


    public static Account findByUserId(Long user){

        return find.where().eq("user", user).eq("isPosted", true).findUnique();
    }


}

我正在尝试检查用户是否已将数据发布到mst帐户

1 个答案:

答案 0 :(得分:2)

将以下代码段放入您的帐户模型中。我使用型号名称Account替换您自己的型号名称。

public static Finder<Long,Account> find = new Finder(Long.class, Account.class);

public static Account findByUserID(int user_id){
    return find.where().eq("user", user_id).eq("active", true).findUnique();
}

如果您引用UserAccount ID,则需要按.eq("user.id", user_id)

更改条件
 public static Account findByUserId(Long user){
    return find.where().eq("user.id", user).eq("isPosted", true).findUnique();
}