是这样的,有两张表,一张是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 

解决方案 »

  1.   

    数据库中作外键关联了吗?
    或者映射文件中配置one-to-many吗?
      

  2.   

    回复楼主:错误很明显了' account_id,这个字段有问题,没有默认值
      

  3.   

    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  'account_id' 的问题
      

  4.   

    是不是hibernate的配置有问题.  因为trade这张表的account_id本来就不需要设置的. 他会根据hibernate中的配置来设置。
      

  5.   

    先取得account的信息,获取他的id。
    在插入trade数据的时候:
    Account a = new Account();
    a.set(获取的id);
    Trade t = new Trade();
    t.set..
    t.set..
    t.setAccount(a);
      

  6.   

    先取得account的信息,获取他的id。
    在插入trade数据的时候:
    Account a = new Account();
    a.set(获取的id);
    Trade t = new Trade();
    t.set..
    t.set..
    t.setAccount(a);
      

  7.   

    应该是hibernate映射配置的问题,能否贴出来。