如何使用 Spring Boot Crudrepository 将数据插入同一数据库中的 2 个表?
How do I insert data into 2 tables in the same database using Spring Boot Crudrepository?
我希望能够为我的应用程序创建一个新帐户。我有一个帐户 class 代表一个实体,另一个 class 代表帐户的个人信息。为了创建新帐户并将其保存在数据库中,我想将一些信息添加到帐户 table 中,并将一些信息添加到 PersonalInfo table 中,详见下面的 classes .我如何使用 CrudRespository 接口执行此操作。据我了解,crudrepository 可以与数据库中的一个 table 进行交互。在我的示例中,这将是帐户。这很好,因为我的大部分检查和沟通都是与帐户 table 进行的。但是当我创建一个新帐户时,我需要添加将提供给两个 table 的数据。我是否必须进行手动查询并将其作为方法添加到其中?
@Entity
@Component
public class Account {
@Id
private int accountNum;
private String accountType;
private int accountBalance;
private String accountStatus;
@Entity
@Component
public class PersonalInfo {
@Id
private int accountNum;
private String firstName;
private String lastName;
private String SSN;
private String streetName;
private String city;
private String state;
private String zipcode;
@RepositoryRestResource(collectionResourceRel="accounts",path="accounts")
public interface AccountsDB extends CrudRepository<Account, Integer>{
}
只需为 PersonalInfo
创建一个存储库,并分别对两个创建的实体调用两个 save()
方法(分别属于两个不同的存储库)。
只需确保为这两个实体设置相同的 ID (accountNum
)。
或者,您可以创建一个服务来为您做这件事,就像这样:
public interface AccountAndPersonalInfoService {
void save(Account account, PersonalInfo personalInfo);
}
@Service
public class AccountAndPersonalInfoServiceImpl implements AccountAndPersonalInfoService {
@Autowired
private AccountsDB accountsDB;
@Autowired
private PersonalInfoDB personalInfoDB;
@Override
void save(Account account, PersonalInfo personalInfo) {
if (account.getAccountNum() == personalInfo.getAccountNum()) {
accountsDB.save(account);
personalInfoDB.save(personalInfo);
} else throw new IllegalArgumentException("The ids of the entities do not match.");
}
}
我希望能够为我的应用程序创建一个新帐户。我有一个帐户 class 代表一个实体,另一个 class 代表帐户的个人信息。为了创建新帐户并将其保存在数据库中,我想将一些信息添加到帐户 table 中,并将一些信息添加到 PersonalInfo table 中,详见下面的 classes .我如何使用 CrudRespository 接口执行此操作。据我了解,crudrepository 可以与数据库中的一个 table 进行交互。在我的示例中,这将是帐户。这很好,因为我的大部分检查和沟通都是与帐户 table 进行的。但是当我创建一个新帐户时,我需要添加将提供给两个 table 的数据。我是否必须进行手动查询并将其作为方法添加到其中?
@Entity
@Component
public class Account {
@Id
private int accountNum;
private String accountType;
private int accountBalance;
private String accountStatus;
@Entity
@Component
public class PersonalInfo {
@Id
private int accountNum;
private String firstName;
private String lastName;
private String SSN;
private String streetName;
private String city;
private String state;
private String zipcode;
@RepositoryRestResource(collectionResourceRel="accounts",path="accounts")
public interface AccountsDB extends CrudRepository<Account, Integer>{
}
只需为 PersonalInfo
创建一个存储库,并分别对两个创建的实体调用两个 save()
方法(分别属于两个不同的存储库)。
只需确保为这两个实体设置相同的 ID (accountNum
)。
或者,您可以创建一个服务来为您做这件事,就像这样:
public interface AccountAndPersonalInfoService {
void save(Account account, PersonalInfo personalInfo);
}
@Service
public class AccountAndPersonalInfoServiceImpl implements AccountAndPersonalInfoService {
@Autowired
private AccountsDB accountsDB;
@Autowired
private PersonalInfoDB personalInfoDB;
@Override
void save(Account account, PersonalInfo personalInfo) {
if (account.getAccountNum() == personalInfo.getAccountNum()) {
accountsDB.save(account);
personalInfoDB.save(personalInfo);
} else throw new IllegalArgumentException("The ids of the entities do not match.");
}
}