Home > Software design >  Why getting ____ cannot be null
Why getting ____ cannot be null

Time:10-05

I'm on learning stage and just start making little app contains single entity. While test on postman I keep getting an error: Column 'account_number' cannot be null Although I enter accountNumber. I follow entity & related classed hereunder: My excuse if I make something missing due to lack of knowledge. Grateful for an advise

PrimaryAccount

@Entity
@Table(name = "primary_account")
public class PrimaryAccount {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id")
private Long id;
@Column(name = "account_number")
private String accountNumber;
@Column(name = "account_balance")
private BigDecimal accountBalance;
@OneToMany(mappedBy = "primaryAccount", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JsonIgnore
private List<PrimaryTransaction> primaryTransactionList;
Getter&Setting&Constructor

AccountService

public interface AccountService {
ResponseEntity<String> createPrimaryAccount();
}

AccountServiceImpl

@Service
public class AccountServiceImpl implements AccountService {

@Autowired
private PrimaryAccountDao primaryAccountDao;

public ResponseEntity<String> createPrimaryAccount() {
    PrimaryAccount primaryAccount = new PrimaryAccount();
    primaryAccount.setAccountNumber(primaryAccount.getAccountNumber());
    primaryAccount.setAccountBalance(primaryAccount.getAccountBalance());
    primaryAccountDao.save(primaryAccount);
    return ResponseEntity.ok("Record entered");
}

@Controller public class AccountController {

@Autowired
private AccountService accountService;

@PostMapping("/primaryAccount")
public ResponseEntity<String> primaryAccount(PrimaryAccount primaryAccount) {
    return accountService.createPrimaryAccount();
}

PrimarAccountDao

public interface PrimaryAccountDao extends CrudRepository<PrimaryAccount, Long> {
   PrimaryAccount findByAccountNumber (long accountNumber);
}

on postman side I just make as follow:

 {
    "AccountNumber": "89000",
    "AccountBalance": 2000.00
}

Error:

java.sql.SQLIntegrityConstraintViolationException: Column 
'account_number' cannot be null
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException
(SQLError.java:117) ~[mysql-connector-java-8.0.26.jar:8.0.26]
at 
com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException
(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.26.jar:
8.0.26]at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal
(ClientPreparedStatement.java:953) ~[mysql-connector-java- 
8.0.26.jar:8.0.26]
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal
(ClientPreparedStatement.java:1092) ~[mysql-connector-java- 
8.0.26.jar:8.0.26]
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal
(ClientPreparedStatement.java:1040) ~[mysql-connector-java- 
8.0.26.jar:8.0.26]
at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate
(ClientPreparedStatement.java:1350) ~[mysql-connector-java- 
8.0.26.jar:8.0.26]
at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate
(ClientPreparedStatement.java:1025) ~[mysql-connector-java- 
8.0.26.jar:8.0.26]
at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate
(ProxyPreparedStatement.java:61) ~[HikariCP-4.0.3.jar:na]
at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate
(HikariProxyPreparedStatement.java) ~[HikariCP-4.0.3.jar:na]
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.
executeUpdate(ResultSetReturnImpl.java:197) ~[hibernate-core- 
5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.insert
 (AbstractEntityPersister.java:3298) ~[hibernate-core- 
 5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.persister.entity.AbstractEntityPersister.insert
(AbstractEntityPersister.java:3825) ~[hibernate-core- 
5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.action.internal.EntityInsertAction.execute
(EntityInsertAction.java:107) ~[hibernate-core- 
5.4.32.Final.jar:5.4.32.Final]
at 
org.hibernate.engine.spi.ActionQueue.executeActions
(ActionQueue.java:604) 
~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.engine.spi.ActionQueue.lambda$executeActions$1
(ActionQueue.java:478) ~[hibernate-core- 
5.4.32.Final.jar:5.4.32.Final]
at java.base/java.util.LinkedHashMap.forEach(LinkedHashMap.java:684) 
~ na:na]
at org.hibernate.engine.spi.ActionQueue.executeActions
(ActionQueue.java:475) ~[hibernate-core- 
5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.event.internal.AbstractFlushingEventListener.
performExecutions(AbstractFlushingEventListener.java:344) ~ 
[hibernate- 
core-5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush
(DefaultFlushEventListener.java:40) ~[hibernate-core- 
5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.event.service.internal.EventListenerGroupImpl.
fireEventOnEachListener(EventListenerGroupImpl.java:99) ~[hibernate- 
core-5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.internal.SessionImpl.doFlush
(SessionImpl.java:1362) ~[hibernate-core- 
 5.4.32.Final.jar:5.4.32.Final]
at 
org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:453) 
~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at 
org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion
(SessionImpl.java:3212) ~[hibernate-core- 
5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.internal.SessionImpl.beforeTransactionCompletion
(SessionImpl.java:2380) ~[hibernate-core- 
 5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.
beforeTransactionCompletion(JdbcCoordinatorImpl.java:448) ~ 
[hibernate- 
core-5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.resource.transaction.backend.jdbc.internal.
JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletion
Callback(JdbcResourceLocalTransactionCoordinatorImpl.java:183)
~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.resource.transaction.backend.jdbc.internal.
JdbcResourceLocalTransactionCoordinatorImpl.access$300
(JdbcResourceLocalTransactionCoordinatorImpl.java:40) ~[hibernate- 
core- 
5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.resource.transaction.backend.jdbc.internal.
JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriver
ControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.
java:281) 
 ~[hibernate-core-5.4.32.Final.jar:5.4.32.Final]
at org.hibernate.engine.transaction.internal.TransactionImpl.commit
(TransactionImpl.java:101) ~[hibernate-core- 
 5.4.32.Final.jar:5.4.32.Final]
at org.springframework.orm.jpa.JpaTransactionManager.doCommit
(JpaTransactionManager.java:562) ~[spring-orm-5.3.10.jar:5.3.10]
at 
 org.springframework.transaction.support.AbstractPlatformTransaction
Manager.processCommit(AbstractPlatformTransactionManager.java:743) ~ 
 [spring-tx-5.3.10.jar:5.3.10]

CodePudding user response:

You are instantiating the primaryAccount and setting it with the value it already has, which is in fact null. You need to pass the parameter from Controller to Service as follows:

public interface AccountService {
    ResponseEntity<String> createPrimaryAccount(PrimaryAccount primaryAccount);
}
@Service
public class AccountServiceImpl implements AccountService {

@Autowired
private PrimaryAccountDao primaryAccountDao;

public ResponseEntity<String> createPrimaryAccount(PrimaryAccount primaryAccount) {
    primaryAccountDao.save(primaryAccount);
    return ResponseEntity.ok("Record entered");
}
@Controller 
public class AccountController {

   @Autowired
   private AccountService accountService;

   @PostMapping("/primaryAccount")
   public ResponseEntity<String> primaryAccount(@RequestBody PrimaryAccount primaryAccount) {
      return accountService.createPrimaryAccount(primaryAccount);
   }
}

UPDATE

Your JSON must be changed to:

{
    "accountNumber": "89000",
    "accountBalance": 2000.00
}

Why? Unless you use some @JsonProperty annotations, the attributes in the JSON must match the name of each property in your model.

One suggestion: use DTOs instead of the Entities in your Controller layer. You can read some reason here.

CodePudding user response:

You project has basic programming fundamental issue. Update your project like this,

public interface AccountService {
    ResponseEntity<String> createPrimaryAccount(PrimaryAccount primaryAccount);
}

@Service
public class AccountServiceImpl implements AccountService {

   @Autowired
   private PrimaryAccountDao primaryAccountDao;

   public ResponseEntity<String> createPrimaryAccount(PrimaryAccount primaryAccount) {
       primaryAccountDao.save(primaryAccount);
       return ResponseEntity.ok("Record entered");
   }
}
@Autowired
private AccountService accountService;

@PostMapping("/primaryAccount")
public ResponseEntity<String> primaryAccount(@RequestBody PrimaryAccount primaryAccount) {
    return accountService.createPrimaryAccount(primaryAccount);
}
  • Related