请教高手:(本人SSH2初学者,如果问题比较傻,请大家多包涵)
Comment表有两个外键分别是User表和Goods表的id
CREATE TABLE `comment` (
`Cid` int(11) NOT NULL AUTO_INCREMENT,
`Gid` int(11) DEFAULT NULL,
`Uid` int(11) DEFAULT NULL,
`Rating` int(11) DEFAULT NULL,
`Detailcmt` text,
PRIMARY KEY (`Cid`),
KEY `FK_Relationship_8` (`Uid`),
KEY `FK_Relationship_9` (`Gid`),
CONSTRAINT `FK_Relationship_8` FOREIGN KEY (`Uid`) REFERENCES `user` (`Uid`),
CONSTRAINT `FK_Relationship_9` FOREIGN KEY (`Gid`) REFERENCES `goods` (`Gid`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8
-------------------------------------------------------------------------------------
comment和User是双向一对多(多对一)关系,comment和Goods也是。
comment类、user类、Goods类的部分代码如下
public class Comment implements java.io.Serializable { /**
*
*/
private static final long serialVersionUID = -8938253805027393144L;
// Fields
private Integer cid;
private Goods goods;
private User user;
private Integer rating;
private String detailcmt;
...}
public class User implements java.io.Serializable { /**
*
*/
private static final long serialVersionUID = 6215220620441185142L;
// Fields private Integer uid;
private String uname;
private String password;
private Boolean ustate;
private String email;
private Integer score;
private Set<Comment> comments = new HashSet<Comment>(0);
private Set<Address> addresses = new HashSet<Address>(0);
private Set<Order> orders = new HashSet<Order>(0);
...}public class Goods implements java.io.Serializable { /**
*
*/
private static final long serialVersionUID = 1513877686565642261L;
// Fields private Integer gid;
private String gname;
private Float price;
private Integer discount;
private String image;
private String detailinfo;
private Integer inventory;
private Boolean gstate;
private Set<Singleorder> singleorders = new HashSet<Singleorder>(0);
private Set<Comment> comments = new HashSet<Comment>(0);
...}
-----------------------------------------------------------------------------
映射文件部分代码如下
Comment.hbm.xml
<many-to-one name="goods" class="edu.ws.bean.Goods" fetch="select" cascade="save-update">
<column name="Gid" />
</many-to-one>
<many-to-one name="user" class="edu.ws.bean.User" fetch="select" cascade="save-update">
<column name="Uid" />
</many-to-one>
User.hbm.xml
<set name="comments" inverse="true" cascade="all">
<key>
<column name="Uid" />
</key>
<one-to-many class="edu.ws.bean.Comment" />
</set>
<set name="addresses" inverse="true" cascade="all">
<key>
<column name="Uid" />
</key>
<one-to-many class="edu.ws.bean.Address" />
</set>
<set name="orders" inverse="true" cascade="all">
<key>
<column name="Uid" />
</key>
<one-to-many class="edu.ws.bean.Order" />
</set>
Goods.bm.xml
<set name="singleorders" inverse="true" cascade="all">
<key>
<column name="Gid" />
</key>
<one-to-many class="edu.ws.bean.Singleorder" />
</set>
<set name="comments" inverse="true" cascade="all">
<key>
<column name="Gid" />
</key>
<one-to-many class="edu.ws.bean.Comment" />
</set>
---------------------------------------------------------------------------------------
CommentDaoImpl.java
package edu.ws.dao.impl;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import edu.ws.bean.Comment;
import edu.ws.dao.CommentDao;public class CommentDaoImpl extends HibernateDaoSupport implements CommentDao { public void saveComment(Comment comment) {
this.getHibernateTemplate().save(comment);
}}
------------------------------------------------------------------------------------
我写的action是从User表和Goods表中各取行生成相应的类对象,然后用saveComment方法存进数据库,出了Uid列和Gid列别的都存进去了,只有这两个外键对应的id存不进去。请问到底是哪儿出了问题!纠结中...期待中...请大家帮帮忙O(∩_∩)O~
Comment表有两个外键分别是User表和Goods表的id
CREATE TABLE `comment` (
`Cid` int(11) NOT NULL AUTO_INCREMENT,
`Gid` int(11) DEFAULT NULL,
`Uid` int(11) DEFAULT NULL,
`Rating` int(11) DEFAULT NULL,
`Detailcmt` text,
PRIMARY KEY (`Cid`),
KEY `FK_Relationship_8` (`Uid`),
KEY `FK_Relationship_9` (`Gid`),
CONSTRAINT `FK_Relationship_8` FOREIGN KEY (`Uid`) REFERENCES `user` (`Uid`),
CONSTRAINT `FK_Relationship_9` FOREIGN KEY (`Gid`) REFERENCES `goods` (`Gid`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8
-------------------------------------------------------------------------------------
comment和User是双向一对多(多对一)关系,comment和Goods也是。
comment类、user类、Goods类的部分代码如下
public class Comment implements java.io.Serializable { /**
*
*/
private static final long serialVersionUID = -8938253805027393144L;
// Fields
private Integer cid;
private Goods goods;
private User user;
private Integer rating;
private String detailcmt;
...}
public class User implements java.io.Serializable { /**
*
*/
private static final long serialVersionUID = 6215220620441185142L;
// Fields private Integer uid;
private String uname;
private String password;
private Boolean ustate;
private String email;
private Integer score;
private Set<Comment> comments = new HashSet<Comment>(0);
private Set<Address> addresses = new HashSet<Address>(0);
private Set<Order> orders = new HashSet<Order>(0);
...}public class Goods implements java.io.Serializable { /**
*
*/
private static final long serialVersionUID = 1513877686565642261L;
// Fields private Integer gid;
private String gname;
private Float price;
private Integer discount;
private String image;
private String detailinfo;
private Integer inventory;
private Boolean gstate;
private Set<Singleorder> singleorders = new HashSet<Singleorder>(0);
private Set<Comment> comments = new HashSet<Comment>(0);
...}
-----------------------------------------------------------------------------
映射文件部分代码如下
Comment.hbm.xml
<many-to-one name="goods" class="edu.ws.bean.Goods" fetch="select" cascade="save-update">
<column name="Gid" />
</many-to-one>
<many-to-one name="user" class="edu.ws.bean.User" fetch="select" cascade="save-update">
<column name="Uid" />
</many-to-one>
User.hbm.xml
<set name="comments" inverse="true" cascade="all">
<key>
<column name="Uid" />
</key>
<one-to-many class="edu.ws.bean.Comment" />
</set>
<set name="addresses" inverse="true" cascade="all">
<key>
<column name="Uid" />
</key>
<one-to-many class="edu.ws.bean.Address" />
</set>
<set name="orders" inverse="true" cascade="all">
<key>
<column name="Uid" />
</key>
<one-to-many class="edu.ws.bean.Order" />
</set>
Goods.bm.xml
<set name="singleorders" inverse="true" cascade="all">
<key>
<column name="Gid" />
</key>
<one-to-many class="edu.ws.bean.Singleorder" />
</set>
<set name="comments" inverse="true" cascade="all">
<key>
<column name="Gid" />
</key>
<one-to-many class="edu.ws.bean.Comment" />
</set>
---------------------------------------------------------------------------------------
CommentDaoImpl.java
package edu.ws.dao.impl;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import edu.ws.bean.Comment;
import edu.ws.dao.CommentDao;public class CommentDaoImpl extends HibernateDaoSupport implements CommentDao { public void saveComment(Comment comment) {
this.getHibernateTemplate().save(comment);
}}
------------------------------------------------------------------------------------
我写的action是从User表和Goods表中各取行生成相应的类对象,然后用saveComment方法存进数据库,出了Uid列和Gid列别的都存进去了,只有这两个外键对应的id存不进去。请问到底是哪儿出了问题!纠结中...期待中...请大家帮帮忙O(∩_∩)O~
解决方案 »
- 表关联修改外键异常:identifier of an instance of Object was altered from 1 to 7
- 求助:jsp批量删除功能,请高手帮忙解释下这个功能是怎么实现的
- 求教photoshop基本操作方法,问题一经解决立即结贴给分,在线等。。。
- JSP如何实现文件自动下载到本地临时文件夹
- JSP 生成随机验证码图片在unix系统weblogic应用环境下JSP 生成随机验证码图片是个红叉
- java在线编辑器
- 如何禁止页面刷新,按页面上的刷新也不给刷新?
- 求《JSP设计》(第三版,英文),谁有?或者给一个免费下载的地址,谢谢!
- 各位高手幫忙呀﹗﹗﹗急迫急迫急迫急迫急迫急迫急迫急迫急迫急迫急迫急迫急迫急迫急迫﹗﹗﹗﹗
- JavaMail的具体配置方法
- java正则表达式问题
- applet可不可以根据需要分别引入程序中使用的类?
Hibernate: insert into webshopdb.comment (Gid, Uid, Rating, Detailcmt) values (?, ?, ?, ?)
没有update Gid和Uid的语句 Goods和User对象都是从数据库里取的。