如何从Java中的Room数据库获取自动生成的ID?

时间:2020-07-26 10:26:20

标签: android

我有一个表,其中有一个客户ID列,该字段在用户插入其详细信息时不会自动生成,而是在数据库中插入一个新的自动生成的ID。我关心的是,一旦用户单击保存详细信息,我如何获取该ID。我之前经历过堆栈溢出,但没有找到任何解决方案。我也想知道我在id的实体类中有Setter方法,但是当我获取值时,它始终显示0。

实体类

@Entity
public class CustomerDetails  {
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "Customer_Id")
    private int CustomerId;

    @NonNull
    @ColumnInfo(name = "Customer_FirstName")
    private  String CustomerFirstName;

    @NonNull
    @ColumnInfo(name = "Customer_LastName")
    private  String CustomerLastName;
    @NonNull
    @ColumnInfo(name = "Customer_PhoneNumber")
    private  String CustomerPhoneNo;
    @NonNull
    @ColumnInfo(name = "Customer_Address")
    private  String CustomerAddress;

    @ColumnInfo(name = "Created_At",defaultValue = "CURRENT_DATETIME")
    @TypeConverters(TimestampConverter.class)
    private Date createdAt;

    public CustomerDetails(@NonNull String customerFirstName, @NonNull String customerLastName, @NonNull String customerPhoneNo, @NonNull String customerAddress, Date createdAt) {
        CustomerFirstName = customerFirstName;
        CustomerLastName = customerLastName;
        CustomerPhoneNo = customerPhoneNo;
        CustomerAddress = customerAddress;
        this.createdAt = createdAt;
    }

    public CustomerDetails() {
    }

    public int getCustomerId() {
        return CustomerId;
    }

    public void setCustomerId(int customerId) {
       CustomerId = customerId;
    }

    @NonNull
    public String getCustomerFirstName() {
        return CustomerFirstName;
    }

    @NonNull
    public String getCustomerLastName() {
        return CustomerLastName;
    }



    @NonNull
    public String getCustomerAddress() {
        return CustomerAddress;
    }

    @NonNull
    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCustomerFirstName(@NonNull String customerFirstName) {
        CustomerFirstName = customerFirstName;
    }

    public void setCustomerLastName(@NonNull String customerLastName) {
        CustomerLastName = customerLastName;
    }


    public void setCustomerAddress(@NonNull String customerAddress) {
        CustomerAddress = customerAddress;
    }

    public void setCreatedAt(@NonNull Date createdAt) {
        this.createdAt = createdAt;
    }

    @NonNull
    public String getCustomerPhoneNo() {
        return CustomerPhoneNo;
    }

    public void setCustomerPhoneNo(@NonNull String customerPhoneNo) {
        CustomerPhoneNo = customerPhoneNo;
    }
}

DAO界面

    @Dao
public interface CustomerDetailsDAO {
    @Insert
    long insertCustomerDetails(CustomerDetails customerDetails);

    @Query("SELECT * FROM CUSTOMERDETAILS ORDER BY Created_At")
    LiveData<List<CustomerDetails>> getCustomerDetails();



}

存储库类

    public class CustomerDetailsRepositary {
    private CustomerDetailsDAO customerDetailsDAO;
    private LiveData<List<CustomerDetails>> customerdetails;
    public CustomerDetailsRepositary(Application application){
        CustomerDatabase customerDatabase = CustomerDatabase.getCustomerDatabaseInstance(application);
        customerDetailsDAO = customerDatabase.customerDetailsDAO();
        customerdetails = customerDetailsDAO.getCustomerDetails();
    }
    public long insert(CustomerDetails customerDetails){

         new InsertCustomerDetailAsyncTask(customerDetailsDAO).execute(customerDetails);
        return customerDetails.getCustomerId();

    }


    public LiveData<List<CustomerDetails>> getCustomerdetails(){
        return customerdetails;
    }
    private static class  InsertCustomerDetailAsyncTask extends AsyncTask<CustomerDetails, Void, Long> {
        private CustomerDetailsDAO customerDetailsDAO;

        private InsertCustomerDetailAsyncTask(CustomerDetailsDAO customerDetailsDAO){
            this.customerDetailsDAO=customerDetailsDAO;
        }
        @Override
        protected Long doInBackground(CustomerDetails... customerDetails) {
            return customerDetailsDAO.insertCustomerDetails(customerDetails[0]);
        }

        @Override
        protected void onPostExecute(Long aLong) {
            super.onPostExecute(aLong);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

“房间插入”功能返回主键。它不会在对象上设置主键。 当insert函数返回时,您必须自己将主键分配给该对象

   @Override
    protected Long doInBackground(CustomerDetails... customerDetails) {
        Long pk = customerDetailsDAO.insertCustomerDetails(customerDetails[0]);
        customerDetails[0].setCustomerId(pk);
        return pk
    }

对于您的示例,它看起来可能像这样。 另外,您还有一个Int作为CustomerId,应该为Long,因为它是主键。