类Classinfo.java
package pojo;
import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;/**
 * The persistent class for the classinfo database table.
 * 
 * @author BEA Workshop Studio
 */
public class Classinfo  implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
private Integer classId;
private String classUrl;
private String classname;
private String isDelete;
private String isNavi;
private Integer rotId;
private Integer pid;
private java.util.Set<Roleinfo> roleInfo;
//private java.util.Set<Roleinfo> roleinfos;    public Classinfo() {
    } public Integer getClassId() {
return this.classId;
}
public void setClassId(Integer classId) {
this.classId = classId;
} public String getClassUrl() {
return this.classUrl;
}
public void setClassUrl(String classUrl) {
this.classUrl = classUrl;
} public String getClassname() {
return this.classname;
}
public void setClassname(String classname) {
this.classname = classname;
} public String getIsDelete() {
return this.isDelete;
}
public void setIsDelete(String isDelete) {
this.isDelete = isDelete;
} public String getIsNavi() {
return this.isNavi;
}
public void setIsNavi(String isNavi) {
this.isNavi = isNavi;
} public Integer getRotId() {
return this.rotId;
}
public void setRotId(Integer rotId) {
this.rotId = rotId;
} public Integer getPid() {
return this.pid;
}
public void setPid(Integer pid) {
this.pid = pid;
} //bi-directional many-to-one association to Roleinfo
/*public java.util.Set<Roleinfo> getRoleinfos() {
return this.roleinfos;
}
public void setRoleinfos(java.util.Set<Roleinfo> roleinfos) {
this.roleinfos = roleinfos;
}*/ public String toString() {
return new ToStringBuilder(this)
.append("classId", getClassId())
.toString();
} public java.util.Set<Roleinfo> getRoleInfo() {
return roleInfo;
} public void setRoleInfo(java.util.Set<Roleinfo> roleInfo) {
this.roleInfo = roleInfo;
}
}
类Roleinfo
package pojo;
import java.io.Serializable;
import org.apache.commons.lang.builder.ToStringBuilder;/**
 * The persistent class for the roleinfo database table.
 * 
 * @author BEA Workshop Studio
 */
public class Roleinfo  implements Serializable {
//default serial version id, required for serializable classes.
private static final long serialVersionUID = 1L;
private Integer id;
private String roleName;
private String description;
private java.util.Set<Classinfo> classInfo;
    public java.util.Set<Classinfo> getClassInfo() {
return classInfo;
} public void setClassInfo(java.util.Set<Classinfo> classInfo) {
this.classInfo = classInfo;
} public Roleinfo() {
    } public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
} public String getRoleName() {
return this.roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
} public String getDescription() {
return this.description;
}
public void setDescription(String description) {
this.description = description;
} public String toString() {
return new ToStringBuilder(this)
.append("id", getId())
.toString();
}
}
类Role2Class.java
package pojo;public class Role2Class {
private Integer roleId;
private Integer classId;
private Integer powerId;
public Integer getPowerId() {
return powerId;
}
public void setPowerId(Integer powerId) {
this.powerId = powerId;
}
public Integer getClassId() {
return classId;
}
public void setClassId(Integer classId) {
this.classId = classId;
}
public Integer getRoleId() {
return roleId;
}
public void setRoleId(Integer roleId) {
this.roleId = roleId;
}
}

解决方案 »

  1.   

    Classinfo.hbm.xml
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping>
    <class 
        name="pojo.Classinfo" 
        table="classinfo"
        lazy="false"
    >
        <id
            name="classId"
            type="integer"
            column="ClassId"
            length="4"
            unsaved-value="0"
        >
            <generator class="increment" />
        </id>
        <property
            name="classUrl"
            type="string"
            column="ClassUrl"
            length="4000"
        />
        <property
            name="classname"
            type="string"
            column="Classname"
            length="20"
        />
        <property
            name="isDelete"
            type="string"
            column="IsDelete"
            length="2"
        />
        <property
            name="isNavi"
            type="string"
            column="IsNavi"
            length="2"
        />
        <property
            name="rotId"
            type="integer"
            column="RotId"
            length="4"
        />
        <property
            name="pid"
            type="integer"
            column="pId"
            length="4"
        />  <set name="roleInfo"
      table="role2class"
      cascade="save-update"
      lazy="true"
      inverse="false">
       <key column="classId"></key>
       <many-to-many
       class="pojo.Roleinfo"
       column="id"
       outer-join="auto">
       </many-to-many>
      </set></class>
    </hibernate-mapping>
    Roleinfo.hbm.xml
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC 
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping>
    <class 
        name="pojo.Roleinfo" 
        table="roleinfo"
        lazy="false"
    >
        <id
            name="id"
            type="integer"
            column="id"
            length="4"
            unsaved-value="0"
        >
            <generator class="increment" />
        </id>
        <property
            name="roleName"
            type="string"
            column="RoleName"
            length="20"
        />
        
        <property
            name="description"
            type="string"
            column="description"
            length="4000"
        />
    <set name="classInfo"
     table="role2class"
     inverse="true"
      lazy="true"
       cascade="save-update">
    <key column="id"></key>
    <many-to-many class="pojo.Classinfo"
     column="classId" outer-join="auto">

    </many-to-many>
    </set>
    </class>
    </hibernate-mapping>RoleDao.java
    package dao.role;import java.util.List;import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.Transaction;
    import org.springframework.beans.factory.BeanFactory;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import pojo.Classinfo;
    import pojo.Role2Class;
    import pojo.Roleinfo;public class RoleDao extends HibernateDaoSupport implements IRoleDao {
    public void saveRole(Roleinfo r) {
    System.out.println(r.getRoleName());
    getSession().saveOrUpdate(r);
    }}
    类RoleService.java
    package service.role;import java.util.List;import org.springframework.beans.factory.BeanFactory;
    import org.springframework.context.support.ClassPathXmlApplicationContext;import dao.role.IRoleDao;import pojo.Classinfo;
    import pojo.Roleinfo;
    import service.role.IRoleService;
    public class RoleService implements IRoleService{
    private IRoleDao roledao=null;
    public void saveRole(Roleinfo r) {
    roledao.saveRole(r);
    } public void setRoledao(IRoleDao roledao) {
    this.roledao = roledao;
    } public static void main(String[] args) {
    BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext*.xml");
    IRoleService roleService = (IRoleService)factory.getBean("roleService");
    Roleinfo r = new Roleinfo();
    //r.setId(1);
    //r.setId(3);
    r.setRoleName("king");
    r.setDescription("king");
    Classinfo c = new Classinfo();
    c.setClassId(1);
    c.setClassname("hello");
    //c.setRoleInfo(roleInfo)
    System.out.println(c);
    r.getClassInfo().add(c);
    Integer[] ids = {4};
    //roleService.deleteRole(ids);
    //r.setRoleName("administrator");
    //roleService.updateRole(r);
    roleService.saveRole(r);
    //System.out.println(roleService.findById(1).getClassinfo().getClassId());
    }

    }执行的时候报CGLIB2 available: proxyTargetClass feature enabled,r.getClassInfo().add(c);这句报空指针,意思是r这个对象为空,但r对象的id是自增的,不可能做出这个对象,请问怎么解决?