会议室:如何将对象ID链接到成员ID

时间:2018-07-06 08:54:35

标签: android android-room

我使用Android Room数据库。

我有一个实体“ Member”和一个实体“ Transaction”。我想创建一个新的交易记录(例如买书)。然后,我想链接所有成员(每个ID)与交易ID。这意味着如果有5个人购买这本书,那么我想将5个ID与交易ID关联起来。

执行此操作后,会员的余额应更改(例如,会员有20欧元,交易后的书本费用为10欧元-会员应有10欧元)。

我有活动来创建新交易。现在,我想创建一个新的活动,该活动应该在NewTransactionActivity之后启动,以处理成员与事务的链接。

如何将交易ID与多个成员ID关联? 另外,如何使用BigDecimal执行计算? 因此,当我进行新交易时,每个链接的成员的余额都应更改。

现在,我的余额类型为double,但是我要更改它。

集体交易:

@Entity(tableName = "transaction_table")
public class Transaction {

@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "TransactionID")
private long id;

@ColumnInfo(name = "Transaction Name")
private String transactionName;

@ColumnInfo(name = "Transaction Balance")
private double balance;

public double getBalance() {
    return balance;
}

public void setBalance(double balance) {
    this.balance = balance;
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getTransactionName() {
    return transactionName;
}

public void setTransactionName(String transactionName) {
    this.transactionName = transactionName;
}

public Transaction(String transactionName, double balance) {
    this.transactionName = transactionName;
    this.balance = balance;
}

}

班级成员:

@Entity(tableName = "member_table")
public class Member {


@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "MemberID")
private long id;

@ColumnInfo(name = "First Name")
private String firstname;

@ColumnInfo(name = "Surname")
private String surname;

@ColumnInfo(name = "Balance")
private double balance;

public String getFirstname() {
    return firstname;
}

public void setFirstname(String firstname) {
    this.firstname = firstname;
}

public String getSurname() {
    return surname;
}

public void setSurname(String surname) {
    this.surname = surname;
}

public double getBalance() {
    return balance;
}

public void setBalance(double balance) {
    this.balance = balance;
}

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public Member(String firstname, String surname) {
    this.firstname = firstname;
    this.surname = surname;
    this.balance = 0;
}

}

新交易活动:

public class NewTransactionActivity extends AppCompatActivity {

public static final String EXTRA_REPLY = "com.example.android.transactionlistsql.REPLY";
public static final String EXTRA_REPLY2 = "com.example.android.transactionlistsql.REPLY2";

private EditText mEditTextTransaction;
private EditText mEditTextTransaction2;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newtransaction_activity);
    mEditTextTransaction = findViewById(R.id.NewTransactionName);
    mEditTextTransaction2 = findViewById(R.id.NewTransactionBalance);
    mEditTextTransaction2.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
    mEditTextTransaction2.setText("0");
    final Button button = findViewById(R.id.NewTransactionButtonSave);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent replyIntent = new Intent();
            if (TextUtils.isEmpty(mEditTextTransaction.getText())){
                Toast.makeText(getApplicationContext(), R.string.TransactionNameMissing, Toast.LENGTH_LONG).show();
                return;
                //setResult(RESULT_CANCELED, replyIntent);
            }
            else if (TextUtils.isEmpty(mEditTextTransaction2.getText())){
                mEditTextTransaction2.setText("0");
            }
            else {                   
                String newtransactionname = mEditTextTransaction.getText().toString()
                double newtransactionbalance = Double.parseDouble(mEditTextTransaction2.getText().toString()));
                replyIntent.putExtra(EXTRA_REPLY, newtransactionname);
                replyIntent.putExtra(EXTRA_REPLY2, newtransactionbalance);
                setResult(RESULT_OK, replyIntent);
            }
            finish();
        }
    });
}
}

1 个答案:

答案 0 :(得分:0)

在您的Transaction.java实体类中添加MemberId,并用@ForeignKey实体类设置Member.java

Transaction.java

import static android.arch.persistence.room.ForeignKey.CASCADE;

@Entity(tableName = "transaction_table", foreignKeys = @ForeignKey(entity = Member.class,
        parentColumns = "MemberID",
        childColumns = "member_id",
        onDelete = CASCADE))
public class Transaction {


    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "TransactionID")
    private long id;

    @ColumnInfo(name = "member_id")
    private long MemberId;

    public long getMemberId() {
    return MemberId;
    }

    public void setMemberId(long memberId) {
        MemberId = memberId;
    }

    //Rest of the fields
}
  

如何将交易ID与多个成员ID关联?

如果您的应用程序中存在分期付款,那么您必须将交易ID映射到多个成员。

以上代码将在Member和Transaction表之间映射一对多关系。一个会员可以进行很多交易。

您可以通过以下查询获取所有用户的交易记录:

@Query("SELECT * FROM transaction_table tt INNER JOIN member_table mt on tt.member_id = mt.MemberID WHERE member_id =:memberId")
List<Transaction> getMemberTransactions(long memberId);

要更新会员的余额,您必须编写更新余额查询,其中您必须检查会员的可用余额,并且必须从会员的总余额中扣除交易金额,这将反映在member_table的“余额”列中。