是这样的,有两张表,一张是account,银行卡账户表,account_id 为主键,其他还有cardNumber,balance等,第二张表是trade表,即交易信息表,有trade_id, 为主键,还有trade_time, information等字段,account表对trade表是一对多的单向关联关系,trade表中有account_id为外键,现在我要进行存钱或者取钱的交易,那么首先要更新account账户,然后往trade表里面插入一条交易信息,我是这样写的。。Account dbAccount = accountDao.findAccount(account_id);
double balance = dbAccount.getBalance();
double endBalance = balance + amount;
Trade trade = new Trade();
System.out.println(trade.getTrade_time());
try {
dbAccount.setBalance(endBalance);
dbAccount.setBegin_balance(balance);
dbAccount.setBegin_balance_time(new Date());
trade.setDescription("存款");
trade.setAmount(amount);
trade.setBalance(balance);
trade.setTrade_time(new Date());
//trade持久类中没有account_id,强行添加一个也不行
//trade.setAccount_id(account_id);
// tradeDao.saveTrade(trade);
dbAccount.getTrades().add(trade);
this.accountDao.updateAccount(dbAccount);
return DEPOSIT_SUCCUESS;
} catch (Exception e) {
e.printStackTrace();
return DEPOSIT_FAILED;
然后总是出现以下问题
Struts Problem Report
Struts has detected an unhandled exception: Messages: Field 'account_id' doesn't have a default value
Hibernate flushing: could not insert: [cn.hnust.luo.EBank.domain.Trade]; uncategorized SQLException for SQL [insert into t_trade (amount, balance, trade_time, description) values (?, ?, ?, ?)]; SQL state [HY000]; error code [1364]; Field 'account_id' doesn't have a default value; nested exception is java.sql.SQLException: Field 'account_id' doesn't have a default value
File: com/mysql/jdbc/SQLError.java
Line number: 1,075
--------------------------------------------------------------------------------Stacktraces
org.springframework.jdbc.UncategorizedSQLException: Hibernate flushing: could not insert: [cn.hnust.luo.EBank.domain.Trade]; uncategorized SQLException for SQL [insert into t_trade (amount, balance, trade_time, description) values (?, ?, ?, ?)]; SQL state [HY000]; error code [1364]; Field 'account_id' doesn't have a default value; nested exception is java.sql.SQLException: Field 'account_id' doesn't have a default value
double balance = dbAccount.getBalance();
double endBalance = balance + amount;
Trade trade = new Trade();
System.out.println(trade.getTrade_time());
try {
dbAccount.setBalance(endBalance);
dbAccount.setBegin_balance(balance);
dbAccount.setBegin_balance_time(new Date());
trade.setDescription("存款");
trade.setAmount(amount);
trade.setBalance(balance);
trade.setTrade_time(new Date());
//trade持久类中没有account_id,强行添加一个也不行
//trade.setAccount_id(account_id);
// tradeDao.saveTrade(trade);
dbAccount.getTrades().add(trade);
this.accountDao.updateAccount(dbAccount);
return DEPOSIT_SUCCUESS;
} catch (Exception e) {
e.printStackTrace();
return DEPOSIT_FAILED;
然后总是出现以下问题
Struts Problem Report
Struts has detected an unhandled exception: Messages: Field 'account_id' doesn't have a default value
Hibernate flushing: could not insert: [cn.hnust.luo.EBank.domain.Trade]; uncategorized SQLException for SQL [insert into t_trade (amount, balance, trade_time, description) values (?, ?, ?, ?)]; SQL state [HY000]; error code [1364]; Field 'account_id' doesn't have a default value; nested exception is java.sql.SQLException: Field 'account_id' doesn't have a default value
File: com/mysql/jdbc/SQLError.java
Line number: 1,075
--------------------------------------------------------------------------------Stacktraces
org.springframework.jdbc.UncategorizedSQLException: Hibernate flushing: could not insert: [cn.hnust.luo.EBank.domain.Trade]; uncategorized SQLException for SQL [insert into t_trade (amount, balance, trade_time, description) values (?, ?, ?, ?)]; SQL state [HY000]; error code [1364]; Field 'account_id' doesn't have a default value; nested exception is java.sql.SQLException: Field 'account_id' doesn't have a default value
或者映射文件中配置one-to-many吗?
在插入trade数据的时候:
Account a = new Account();
a.set(获取的id);
Trade t = new Trade();
t.set..
t.set..
t.setAccount(a);
在插入trade数据的时候:
Account a = new Account();
a.set(获取的id);
Trade t = new Trade();
t.set..
t.set..
t.setAccount(a);