【问题标题】:Spring Data Rest with Jpa relationsSpring Data Rest 与 Jpa 关系
【发布时间】:2017-03-09 05:55:23
【问题描述】:

Followed this question but did not work

有两个实体 Account 和 UserTransaction

帐户.java

@Entity
@Access(AccessType.FIELD)
public class Account {

    @Id
    private Integer accountNumber;
    private String holderName;
    private String mobileNumber;
    private Double balanceInformation;


    public Account(Integer accountNumber, String holderName, String mobileNumber, Double balanceInformation) {
        this.accountNumber = accountNumber;
        this.holderName = holderName;
        this.mobileNumber = mobileNumber;
        this.balanceInformation = balanceInformation;
    }
}

UserTransaction.java

@Entity
@Access(AccessType.FIELD)
@Table(name = "user_transaction")
public class Transaction {

    @Id
    private Long transactionId;
    @ManyToOne
    @JoinColumn(name = "accountNumber")
    private Account accountNumber;
    private Double transactionAmount;

    @Column(nullable = false, columnDefinition = "TINYINT", length = 1)
    private Boolean transactionStatus;

    private String statusMessage;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="timestamp", columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
    private Date timestamp;

    public Transaction(Long transactionId, Account account,
                       Double transactionAmount,
                       Boolean transactionStatus,
                       String statusMessage) {
        this.transactionId = transactionId;
        this.accountNumber = account;
        this.transactionAmount = transactionAmount;
        this.transactionStatus = transactionStatus;
        this.statusMessage = statusMessage;
    }
}

而我的TransactionRepository如下

@RepositoryRestResource(collectionResourceRel = "transactions", path = "transactions")
public interface JpaTransactionRepository extends JpaRepository<Transaction, Long>, TransactionRepository {

    @Query(value =  "select t from Transaction t where t.accountNumber.accountNumber = :accountNumber")
    Iterable<Transaction> findByAccountNumber(@Param("accountNumber") Integer accountNumber);

}

我已经按照顶部的 stackoverflow 帖子中的说明构建了一个 json

{
      "transactionId" : "3213435454342",
      "transactionAmount" : 5.99,
      "transactionStatus" : true,
      "statusMessage" : null,
      "timestamp" : "2017-03-09T05:11:41.000+0000",
      "accountNumber" : "http://localhost:8080/accounts/90188977"
}

当我尝试使用上面的 json 执行 POST 时,我得到了

Caused by: java.sql.SQLIntegrityConstraintViolationException: Column 'account_number' cannot be null
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:533)
        at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:513)
        at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:115)
        at com.mysql.cj.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:1983)

如何保存与 Spring data rest 有关系的实体????

【问题讨论】:

    标签: spring spring-boot spring-data-jpa spring-data-rest


    【解决方案1】:

    问题在于,使用@JoinColumn(name = "accountNumber"),您会将数据库中的列名硬编码为accountNumber。通常命名策略会添加嵌入的下划线,而不是混合大小写的列名。 因此,如果您将行更改为@JoinColumn(name = "account_number"),它应该可以工作。

    【讨论】:

      猜你喜欢
      • 2015-08-08
      • 1970-01-01
      • 2019-03-09
      • 1970-01-01
      • 2017-11-09
      • 2017-10-16
      • 2021-02-10
      • 2021-05-22
      • 2016-10-26
      相关资源
      最近更新 更多