会议室:如何将对象ID链接到成员ID [英] Room: How to link an object id to a member id

查看:81
本文介绍了会议室:如何将对象ID链接到成员ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Android Room数据库.

I work with Android Room database.

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

I have an entity "Member" and an entity "Transaction". I want to create a new Transaction (e.g. buying a book). Then I want to link all members (per id) with the transaction id. That means if 5 people buy the book, then I want to link the 5 ids with the transaction id.

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

After I do that, the balance in member should change (e.g. a member has 20 euros and the book costs 10, after transaction - the member should have 10 euros).

我有创建新交易的活动.现在,我想创建一个新的活动,该活动应该在NewTransactionActivity之后开始,以处理成员到事务的链接.

I have the activity to create a new transaction. Now I want to create a new activity, which should be started after the NewTransactionActivity to handle the linking of the members to the transaction.

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

How do I link a transaction id with multiple member ids? Also, how I can perform calculations with BigDecimal? So when I make a new transaction, every linked member's balance should change.

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

Right now, I have the balance in type double, but I'm gonna change it.

集体交易:

@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();
        }
    });
}
}

推荐答案

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

Add MemberId in your Transaction.java entity class and also set @ForeignKey with Member.java entity class.

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关联?

How do I link a transaction id with multiple member ids?

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

If there is split payment in your application then you have to map a transaction id to multiple members.

以上代码将映射Member和Transaction表之间的一对多关系.一个会员可以完成许多交易.

Above code will map one to many relationships between Member and Transaction table. One Member can do many transactions.

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

You can get all user's transaction by following query:

@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的余额"列中.

To update the balance of Member, you have to write update balance query in which you have to check the available balance of Member and have to deduct transaction amount from member's total balance which will be reflected Balance column of member_table.

这篇关于会议室:如何将对象ID链接到成员ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆