在h2数据库上执行jparepository.findall(示例)时,我注意到一个invocationexception被返回。
当我试图配置两个表“account”和“transaction”之间的foregin键关系时会发生这种情况(即一个帐户可以有多个事务,但一个事务只能属于一个帐户)。
在我添加@onetomany和@manytoone注解之前,没有任何问题。
欢迎您的帮助,谢谢。
请求:
查询是成功的,但它给出了一个invocationexception,这反过来又给出了一个http500。
服务:
会计服务.java
...
......
public List<Transaction> getAllTransactions(Account account) {
TransactionPK inputTransactionPK = new TransactionPK();
inputTransactionPK.setAccountNum(account.getAccountNum());
Transaction inputTransaction = new Transaction();
inputTransaction.setTransactionPK(inputTransactionPK);
ExampleMatcher matcher = ExampleMatcher.matchingAll().withIgnorePaths("debitAmt", "creditAmt");
Example<Transaction> example = Example.of(inputTransaction, matcher);
List<Transaction> transactionList = transactionRepository.findAll(example);
log.info("##################################################\n"
+ "Retrieved transaction list for account with account number " + account.getAccountNum()
+ "\n##################################################");
return transactionList;
}
...
......
table型号:
帐户.java
package com.somecompany.account.model;
import java.sql.Timestamp;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import lombok.Data;
@Entity
@Data
public class Account {
@OneToMany(mappedBy = "account")
private Set<Transaction> transaction;
@Column(name = "cust_id")
@NotEmpty(message = "Customer ID cannot be null nor empty")
@Pattern(regexp = "^[0-9]+$", message = "Customer ID must be a number")
@Min(value = 1L, message = "Customer ID must not be less than 1")
@Max(value = 9999999999L, message = "Customer ID must not be larger than 9999999999")
private long custId;
@Column(name = "account_num")
@Id
@NotEmpty(message = "Account number cannot be null nor empty")
@Pattern(regexp = "^[0-9]+$", message = "Account number must be a number")
@Min(value = 1L, message = "Account number must not be less than 1")
@Max(value = 9999999999L, message = "Account number must not be larger than 9999999999")
private long accountNum;
@Column(name = "account_name")
@NotEmpty(message = "Account name cannot be null nor empty")
@Size(min = 1, max = 30, message = "Account name must have length between 1 and 30")
private String accountName;
@Column(name = "account_type")
@NotEmpty(message = "Account type cannot be null nor empty")
@Size(min = 1, max = 7, message = "Account type must have length between 1 and 7")
private String accountType;
@Column(name = "balance_date")
@NotEmpty(message = "Balance date cannot be null nor empty")
private Timestamp balanceDate;
@Column(name = "currency")
@NotEmpty(message = "Currency cannot be null nor empty")
@Size(min = 3, max = 3, message = "Currency must have length exactly equal to 3")
private String currency;
@Column(name = "opening_available_balance", columnDefinition = "Decimal(20,2) default '0.0'")
@NotEmpty(message = "Opening available balance cannot be null nor empty")
@Pattern(regexp = "^[0-9.]+$", message = "Opening available balance must be a decimal number")
@DecimalMin(value = "0.0", message = "Opening available balance cannot be negative")
private float openingAvailableBalance;
}
事务.java
package com.somecompany.account.model;
import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.validation.constraints.DecimalMin;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Size;
import lombok.Data;
@Entity
@Data
public class Transaction {
@ManyToOne
@JoinColumn(name = "account_num", referencedColumnName = "account_num", insertable = false, updatable = false, nullable = false)
private Account account;
@EmbeddedId
private TransactionPK transactionPK;
@Column(name = "account_name")
@NotEmpty(message = "Account name cannot be null nor empty")
@Size(min = 1, max = 30, message = "Account name must have length between 1 and 30")
private String accountName;
@Column(name = "currency")
@NotEmpty(message = "Currency cannot be null nor empty")
@Size(min = 3, max = 3, message = "Currency must have length exactly equal to 3")
private String currency;
@Column(name = "debit_amt", columnDefinition = "Decimal(20,2) default '0.0'")
@NotEmpty(message = "Debit amount cannot be null nor empty")
@DecimalMin(value = "0.0", message = "Debit amount cannot be negative")
private float debitAmt;
@Column(name = "credit_amt", columnDefinition = "Decimal(20,2) default '0.0'")
@NotEmpty(message = "Credit amount cannot be null nor empty")
@DecimalMin(value = "0.0", message = "Credit amount cannot be negative")
private float creditAmt;
@Column(name = "debit_credit")
@NotEmpty(message = "Debit/Credit cannot be null nor empty")
@Size(min = 1, max = 6, message = "Debit/Credit must have length between 1 and 6")
private String debitCredit;
@Column(name = "transaction_narrative")
@Size(min = 0, max = 50, message = "Transaction narrative must have length between 0 and 50")
private String transactionNarrative;
}
事务处理pk.java
package com.somecompany.account.model;
import java.io.Serializable;
import java.sql.Timestamp;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
import lombok.Data;
@Embeddable
@Data
public class TransactionPK implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Column(name = "account_num")
@NotEmpty(message = "Account number cannot be null nor empty")
@Pattern(regexp = "^[0-9]+$", message = "Account number must be a number")
@Min(value = 1L, message = "Account number must not be less than 1")
@Max(value = 9999999999L, message = "Account number must not be larger than 9999999999")
private long accountNum;
@Column(name = "value_date")
@NotEmpty(message = "Value date cannot be null nor empty")
private Timestamp valueDate;
}
h2 db主键和外键信息:
springboot应用程序启动时的示例数据库数据(data.sql):
INSERT INTO ACCOUNT (cust_id, account_num, account_name, account_type, balance_date, currency, opening_available_balance) VALUES
(1111111111, 1111111111, 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', 'Savings', TIMESTAMP '2020-11-01 11:01:01', 'SGD', 99999.99),
(2, 2, 'B', 'Savings', TIMESTAMP '2020-11-02 11:02:02', 'AUD', 0.0),
(1111111111, 3333333333, 'CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC', 'Current', TIMESTAMP '2020-11-03 11:03:03', 'USD', 99999.99);
INSERT INTO TRANSACTION (account_num, account_name, value_date, currency, debit_amt, credit_amt, debit_credit, transaction_narrative) VALUES
(1111111111, 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA', TIMESTAMP '2012-11-01 11:01:01', 'SGD', 0.0, 99999.99, 'Credit', 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'),
(2, 'Savings Account', TIMESTAMP '2012-11-02 11:02:02', 'USD', 0.1, 0.0, 'Debit', null),
(1111111111, 'CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC', TIMESTAMP '2012-11-03 11:03:03', 'USD', 99999.99, 0.0, 'Debit', 'CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC');
1条答案
按热度按时间yxyvkwin1#
经过一番调查,我做了以下修改,应用程序终于如期运行了。
帐户.java
事务.java
事务处理pk.java
我在transactionpk中创建了一个“account”字段来替换“account\u num”字段(account对象已经有了“account\u num”信息),并用@manytone对其进行了注解。这是因为关系是“一个账户可以有许多交易(即交易列表),但一个交易只属于一个账户”。关系在对象级别,而不是字段级别。
对于“account”中的“list transactions”和“transactionpk”中的“account account”,它们只是表示外键关系,不必存在于json文件中。如果我们就这样离开它,它将在序列化为json时产生一个无限的递归错误(因为每个元素都有另一个元素,它永远无法完成json的生成)。
为了解决这个问题,我们可以用@jsonignore标记这些字段,这将跳过对这两个字段的序列化。或者如果我们需要显示一个而不是另一个(例如,在事务json中显示“account”,但在account json中不显示“transactions”),那么我们可以用@jsonmanagedreference注解我们想要保留的一个,并用@jsonbackreference标记我们不想在json中显示的一个。
参考:https://www.baeldung.com/jackson-bidirectional-relationships-and-infinite-recursion
我做的另一个改变是不使用@data。在处理实体时,如果它们将与orm(如jpa)一起使用,@data的equals()和hashcode()实现可能会影响对象比较,请避免使用lombok中的@data。
参考:https://thorben-janssen.com/lombok-hibernate-how-to-avoid-common-pitfalls/