SSH 添加操作时,外键ID存储为空。
SSH 添加操作时,外键ID存储为空,并且我想GET操作时,直接就会报出空指针的错误!!求高手帮下忙啊,研究两天了。
下面是主表的实体类,多的关系:
public class Art_main implements Serializable{
private String art_id;
private String art_title;;
private Art_source Art_source;
private Art_type Art_type;
public String getArt_id(){
return art_id;
}
public void setArt_id(String art_id){
this.art_id = art_id;
}
public String getArt_title(){
return art_title;
}
public void setArt_title(String art_title){
this.art_title = art_title;
}
public Art_type getArt_type() {
return Art_type;
}
public void setArt_type(Art_type artType) {
Art_type = artType;
}
public Art_source getArt_source() {
return Art_source;
}
public void setArt_source(Art_source artSource) {
Art_source = artSource;
}public class Art_source implements Serializable{
private String source_id;
private String source_name;
private Set artMains = new HashSet(0);
public Set getArtMains() {
return artMains;
}
public void setArtMains(Set artMains) {
this.artMains = artMains;
}
public String getSource_id() {
return source_id;
}
public void setSource_id(String sourceId) {
source_id = sourceId;
}
public String getSource_name() {
return source_name;
}
public void setSource_name(String source_name) {
this.source_name = source_name;
}
}<hibernate-mapping >
<class name="com.xunxi.db.back.Art_main" table="art_main">
<id name="art_id" column="art_id">
<generator class="uuid.hex"/>
</id>
<property name="art_title" column="art_title"/>
<many-to-one name="Art_source" class="com.xunxi.db.back.Art_source" fetch="select">
<column name="source_id" length="50" />
</many-to-one>
<many-to-one name="Art_type" class="com.xunxi.db.back.Art_type" fetch="select">
<column name="type_id" length="50" />
</many-to-one>
</class>
</hibernate-mapping>
<hibernate-mapping >
<class name="com.xunxi.db.back.Art_source" table="art_source">
<id name="source_id" column="source_id">
<generator class="uuid.hex"/>
</id>
<property name="source_name" column="source_name"/>
<set name="artMains" inverse="true">
<key>
<column name="source_id" length="50" />
</key>
<one-to-many class="com.xunxi.db.back.Art_main" />
</set>
</class>
</hibernate-mapping>
<s:select name="artMain.Art_source.source_id" list="#select.queryLYWZ()" labelposition="top" listKey="key" listValue="value" headerKey="" headerValue="全部"></s:select>
public String add() {
// TODO Auto-generated method stub
String id=UUID.randomUUID().toString();
Map session = ActionContext.getContext().getSession();
Normal_user user =(Normal_user) session.get("user");
//artMain.getArt_source().setSource_id("123");
artMain.setArt_id(id);
artMain.setArt_create_date(new Date());
artMain.setArt_operator(user.getId());
artMainService.save(artMain);
return "requery";
}
[其他解释]
直接artMain.setArt_source(Art_source)就行了啊
[其他解释]
我懂你的意思了artMain.setArt_source()
设置这个对象里面 你要写一个根据source_id获得Art_source对象的方法 比如叫getById(id)
然后就可以artMain.setArt_source(Art_source.getById(source_id))
[其他解释]
User user = new User();
user.setUserName("名字");
user.setUserGroup(userGroupDao.getById(1));
userService.add(user);