Somasundaram Sekar
Somasundaram Sekar

Reputation: 5524

Spring Data Rest with Jpa relations

Followed this question but did not work

Have two entities Account and 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;
    }
}

and My TransactionRepository is as follows

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

}

I have constructed a json as specified in the stackoverflow post at the top

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

when I try to execute POST with the above json I get

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)

How do I save an entity that has relationships with Spring data rest????

Upvotes: 0

Views: 227

Answers (1)

KLHauser
KLHauser

Reputation: 876

The problem is that with @JoinColumn(name = "accountNumber") you would hard-code the column name in database as accountNumber. Normally the naming-strategy would add embedded underscores instead of having mixed case column names. So it should work if you change the line to @JoinColumn(name = "account_number").

Upvotes: 1

Related Questions