2017-03-09 124 views
0

Followed this question but did not work春數據休息與JPA關係

有兩個實體的帳戶,並UserTransaction的

Account.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); 

} 

我已經建立了一個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我得到

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) 

如何保存執行POST一個與Spring數據有關係的實體

回答

1

問題是,在@JoinColumn(name = "accountNumber")中,您會將數據庫中的列名硬編碼爲accountNumber。通常,命名策略會添加嵌入的下劃線,而不是混合大小寫的列名稱。 所以它應該工作,如果你將行更改爲@JoinColumn(name = "account_number")