Spring ID Mongodb按ID搜索返回null

时间:2017-11-10 07:43:11

标签: java spring mongodb

我用mongodb创建了一个spring boot项目,当我将数据插入到集合中时,它会被插入但是当我尝试从findOne获取id基于id的插入值时,它总是返回null ,我已经给出了我的模型类和插入方法,请告诉我什么是错的

Account.java

@Document(collection =" account") 公共类帐户{

@Id
private long _id;

@Field("account_name")
private String accountName;

@Field("connector_type")
private String connectorType;

@Field("xsiURI1")
private String xsiURI1;

@Field("xsiURI2")
private String xsiURI2;

@Field("oci1")
private String OCI1;

@Field("oci2")
private String OCI2;

@Field("telcomadmin_username")
private String telcomadminUsername;

@Field("telcomadmin_password")
private String telcomadminPassword;

@Field("password_expdays")
private String passwordExpdays;

@Field("account_email_address")
private String accountEmailAddress;

@DateTimeFormat(iso = ISO.DATE_TIME)
@Field("inserted_date")
private Date insertedDate;

@DateTimeFormat(iso = ISO.DATE_TIME)
@Field("updated_date")
private Date updatedDate;

@Field("isActive")
private Boolean isActive;

public long get_id() {
    return _id;
}

public void set_id(long _id) {
    this._id = _id;
}

public String getXsiURI1() {
    return xsiURI1;
}

public void setXsiURI1(String xsiURI1) {
    this.xsiURI1 = xsiURI1;
}

public String getXsiURI2() {
    return xsiURI2;
}

public void setXsiURI2(String xsiURI2) {
    this.xsiURI2 = xsiURI2;
}

public String getOCI1() {
    return OCI1;
}

public void setOCI1(String oCI1) {
    OCI1 = oCI1;
}

public String getOCI2() {
    return OCI2;
}

public void setOCI2(String oCI2) {
    OCI2 = oCI2;
}

public String getAccountName() {
    return accountName;
}

public void setAccountName(String accountName) {
    this.accountName = accountName;
}

public String getConnectorType() {
    return connectorType;
}

public void setConnectorType(String connectorType) {
    this.connectorType = connectorType;
}

public String getTelcomadminUsername() {
    return telcomadminUsername;
}

public void setTelcomadminUsername(String telcomadminUsername) {
    this.telcomadminUsername = telcomadminUsername;
}

public String getTelcomadminPassword() {
    return telcomadminPassword;
}

public void setTelcomadminPassword(String telcomadminPassword) {
    this.telcomadminPassword = telcomadminPassword;
}

public String getPasswordExpdays() {
    return passwordExpdays;
}

public void setPasswordExpdays(String passwordExpdays) {
    this.passwordExpdays = passwordExpdays;
}

public String getAccountEmailAddress() {
    return accountEmailAddress;
}

public void setAccountEmailAddress(String accountEmailAddress) {
    this.accountEmailAddress = accountEmailAddress;
}

public Date getInsertedDate() {
    return insertedDate;
}

public void setInsertedDate(Date insertedDate) {
    this.insertedDate = insertedDate;
}

public Date getUpdatedDate() {
    return updatedDate;
}

public void setUpdatedDate(Date updatedDate) {
    this.updatedDate = updatedDate;
}

public Boolean getIsActive() {
    return isActive;
}

public void setIsActive(Boolean isActive) {
    this.isActive = isActive;
}

AccountsController.java

@RestController
@RequestMapping("/accounts")
@CrossOrigin("*")
public class AccountsController {
    @Autowired
    AccountsRepository accountsRepository;

    @Autowired
    SequenceRepository sequenceRepository;

    private static final String ACCOUNT_SEQ_KEY = "accountsequence";

@PostMapping("/create")
    public Account createAccount(@Valid @RequestBody Account account) {
        account.set_id(sequenceRepository.getNextSequenceId(ACCOUNT_SEQ_KEY));
        account.setIsActive(true);
        return accountsRepository.save(account);
    }

    @GetMapping(value = "/findByID/{id}")
    public ResponseEntity<Account> getAccountById(@PathVariable("id") String id) {
        Account account = accountsRepository.findOne(id);
        if (account == null) {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        } else {
            return new ResponseEntity<>(account, HttpStatus.OK);
        }
    }
}

AccountsRepository

public interface AccountsRepository {

    List<Account> findAll(Sort sortByCreatedAtDesc);

    Account save(Account account);

    Account findOne(String id);

    void delete(String id);

}

AccountsRepositoryIMPL

@Repository
public class AccountsRepositoryImpl implements AccountsRepository {
    DBOperations dbOperations = new DBOperations();

    @Override
    public List<Account> findAll(Sort sortByCreatedAtDesc) {
        Query q = new Query().with(new Sort(Sort.Direction.ASC, "inserted_date"));
        List<Account> accountList = dbOperations.getMongoOpertion().findAllAndRemove(q, Account.class);
        return accountList;
    }

    @Override
    public Account save(Account account) {
        try {
            dbOperations.getMongoOpertion().save(account);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return account;
    }

    @Override
    public Account fin**strong text**dOne(String id) {
        Account account = dbOperations.getMongoOpertion().findOne(Query.query(Criteria.where("_id").is(id)),
                Account.class, "account");
        return account;
    }

    @Override
    public void delete(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id));
        Account account = dbOperations.getMongoOpertion().findOne(query, Account.class);
        dbOperations.getMongoOpertion().remove(account);

    }

}

DBOperations.java

public class DBOperations {

    ApplicationContext ctx = new GenericXmlApplicationContext("SpringConfig.xml");

    public MongoOperations getMongoOpertion() {
        MongoOperations mongoOperation = (MongoOperations) ctx.getBean("mongoTemplate");
        return mongoOperation;
    }

}

1 个答案:

答案 0 :(得分:0)

看看你的代码。 您已将_id声明为Long类型。

@Id
private long _id;

但是在下面的方法中,您传递String id以符合条件。

所以它不起作用。

@Override
        public Account findOne(String id) {
            Account account = dbOperations.getMongoOpertion().findOne(Query.query(Criteria.where("_id").is(id)),
                    Account.class, "account");
            return account;
        }

@Override
    public void delete(String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("id").is(id));
        Account account = dbOperations.getMongoOpertion().findOne(query, Account.class);
        dbOperations.getMongoOpertion().remove(account);

    }