读书人

同一事宜 是否能级联插入

发布时间: 2013-07-01 12:33:04 作者: rapoo

同一事务 是否能级联插入
在同一个事务(相同的Connection对象),好像不能级联插入对象。
1.user表 discuss表(包含 userId键)
代码如下,这样好像不行。
应为user没有commit 不会产生 userId 这样没法级联;

各位大神帮帮忙,怎么解决。


conn = dbcp.getConnection();
conn.setAutoCommit(false);
pstat = conn.prepareStatement(sql);
pstat.setString(1, user.getUsername());
pstat.executeUpdate();

Long id = findUserinfoByUsername(user.getUsername()).getUserId();

pstat = conn.prepareStatement(dsql);
pstat.setLong(1, id);
pstat.setString(2, discuss.getDiscussTable());
pstat.executeUpdate();
[解决办法]
Long id = findUserinfoByUsername(user.getUsername()).getUserId();
你的这个id应该为空,所以后面插入无效。

读书人网 >J2EE开发

热点推荐