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的主表,多的关系的配置文件:
<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>下面是在JSP页面,关键是artMain.Art_source.source_id,这样写应该是对的吧?后面的list大家不要看,那个LIST是调用一个方法后反回值,是封装好的,肯定能反回值的:<s:select  name="artMain.Art_source.source_id" list="#select.queryLYWZ()" labelposition="top" listKey="key" listValue="value" headerKey="" headerValue="全部"></s:select>
下面是ACTION 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,并且也GET和SET了,其它的值都可以取到,只是这个外键关联的值取不到。
然后我试着在ACTION中artMain.getArt_source().setSource_id("123");直接赋值操作,但直接就报空指针的错误了。
我在数据库中直接给source_id值,在JSP页面
<s:property value="#artMain.Art_source.source_name"/>是可以取出来值的。求大家帮帮忙,两天了,都没有解决!!
求大家帮助解决啊,真的是整了两天了,都没有解决,

解决方案 »

  1.   

    private Set artMains = new HashSet(0);
    改成private Set<Art_main> artMains = new HashSet<Art_main >();
      

  2.   

    还有artMain.getArt_source().setSource_id("123");
    artMain的Art_source属性本来为空的话   set方法肯定会报空指针错误的
      

  3.   

    这个的话  你是要先把Art_source对象保存到数据库  然后再artMain.setArt_source(Art_source这个对象)就行了
      

  4.   


    用上面这个方法还是不行,外键ID还是存不进去。
    我现在Art_source里面有值,我是在往Art_main中插值时,插入Art_source对应的ID,而Art_source不做任何操作。我是想这样做成一对多的关联,后面取值显示的时候会很方便,只要artMain.Art_source.source_nam这样就可以显示Art_source表里的任何内容了,不过现在关联不上外键ID。
      

  5.   

    直接artMain.setArt_source(Art_source)就行了啊
      

  6.   

    我懂你的意思了artMain.setArt_source()
    设置这个对象里面  你要写一个根据source_id获得Art_source对象的方法  比如叫getById(id)
    然后就可以artMain.setArt_source(Art_source.getById(source_id))
      

  7.   


    我前台是artMain.Art_source.source_id这么写的。如果建立了一对多的关系,不就可以通过这种方式来进行操作的吗?如果按你说的直接artMain.setArt_source(Art_source),但是Art_source这个值从哪来啊?麻烦多解释一下,谢啦!
      

  8.   

    Art_source如果不做任何操作的话  那完全不用设置对应关系了  直接在artMain里面加一个source_id字段的属性就是了    但是这样的话   你在JSP页面获取就不能直接用artMain得到对应的Art_source对象了
      

  9.   

    Art_source这个值要么是你新建的对象  并且已经保存到数据库了 
    要么就是你用方法从数据库获取的对象给你个例子吧  用户和用户组来说User user = new User();
    user.setUserName("名字");
    user.setUserGroup(userGroupDao.getById(1));
    userService.add(user);user设置userGroup的时候  首先要获取一个userGroup对象  而这个对象是用userGroup的dao类中的getById方法获取的  只有这个userGroup对象存在的时候  user设置userGroup时  user表里面的usergroup_id才是userGroup这个对象的ID
      

  10.   


    你的意思是,我得到Art_source的ID;然后再查出Art_source,再SET到Art_main中,然后再ADD操作?这个操作是可以。但是我想通过我上面的方法直接保存外键ID就好了,以前我做过的项目都是这样的,现在出现这个问题,真是不知道问题出在哪。还有就是如果不设置关联,我查出一张表的数据,都都要再拿到source_id去Art_source表中查到NAME再放到LIST的Art_main中,就有些麻烦了。
      

  11.   

    artMain.getArt_source().setSource_id("123");
    这个写我还真没用过 我在想  你这123是不是存在的?