如何解决java.lang.ClassCastException:无法在Spring中将java.lang.String强制转换为java.lang.Integer

时间:2018-11-07 04:49:11

标签: java hibernate-criteria

MenuModel

@Entity
@Table(name="M_MENU", uniqueConstraints={@UniqueConstraint(columnNames={"NAME"})})

public class MenuModel {

private Integer id;
private String code;
private String name;
private String controller;
private Integer parent_id;  

@Id
@Column(name="ID")
@GeneratedValue(strategy=GenerationType.TABLE, generator="M_MENU")
@TableGenerator(name="M_MENU", table="M_SEQUENCE",
                    pkColumnName="SEQUENCE_NAME", pkColumnValue="M_MENU_ID",
                    valueColumnName="SEQUENCE_VALUE", allocationSize=1, initialValue=0
                )
public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}

@Column(name="CODE")
public String getCode() {
    return code;
}
public void setCode(String kode) {
    this.code = kode;
}

@Column(name="NAME")
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}

@Column(name="CONTROLLER")
public String getController() {
    return controller;
}
public void setController(String controller) {
    this.controller = controller;
}

@Column(name="PARENT_ID")
public Integer getParent_id() {
    return parent_id;
}
public void setParent_id(Integer parent_id) {
    this.parent_id = parent_id;
}   
}

UserAccessModel

@Entity
@Table(name="M_USER_ACCESS")
public class UserAccessModel {

private Integer id;

//join table role
private Integer idRole;
private RoleModel roleModel;

//join table menu
private Integer idMenu;
private MenuModel menuModel;

@Id
@Column(name="ID")
@GeneratedValue(strategy=GenerationType.TABLE, generator="M_USER_ACCESS")
@TableGenerator(name="M_USER_ACCESS", table="M_SEQUENCE",
                    pkColumnName="SEQUENCE_NAME", pkColumnValue="M_USER_ACCESS_ID",
                    valueColumnName="SEQUENCE_VALUE", allocationSize=1, initialValue=0
                )
public Integer getId() {
    return id;
}
public void setId(Integer id) {
    this.id = id;
}

@Column(name="ID_ROLE")
public Integer getIdRole() {
    return idRole;
}
public void setIdRole(Integer idRole) {
    this.idRole = idRole;
}

@ManyToOne
@JoinColumn(name="ID_ROLE", nullable=true, updatable=false, insertable=false)
public RoleModel getRoleModel() {
    return roleModel;
}
public void setRoleModel(RoleModel roleModel) {
    this.roleModel = roleModel;
}

@Column(name="ID_MENU")
public Integer getIdMenu() {
    return idMenu;
}
public void setIdMenu(Integer idMenu) {
    this.idMenu = idMenu;
}

@ManyToOne
@JoinColumn(name="ID_MENU", nullable=true, updatable=false, insertable=false)
public MenuModel getMenuModel() {
    return menuModel;
}
public void setMenuModel(MenuModel menuModel) {
    this.menuModel = menuModel;
}
}

MenuDaoImpl

@Override
public List<MenuModel> searchByRole(Integer idRole) {
    // TODO Auto-generated method stub
    Session session = this.sessionFactory.getCurrentSession();      

    List<MenuModel> menuModelListRole = new ArrayList<MenuModel>();

    Criteria userAccessCriteria = session.createCriteria(UserAccessModel.class,"UA");
    Criteria menuCriteria =  userAccessCriteria.createCriteria("menuModel","M");
    userAccessCriteria.add(Restrictions.eq("idRole", ""+idRole+""));

    ProjectionList properties = Projections.projectionList();
    properties.add(Projections.property("M.id"));
    properties.add(Projections.property("M.name"));
    properties.add(Projections.property("M.code"));
    properties.add(Projections.property("M.controller"));
    properties.add(Projections.property("M.parent_id"));

    menuCriteria.setProjection(properties);
    menuModelListRole = menuCriteria.list();


    return menuModelListRole;
}

我想获取以下sql的结果:

  

选择M.ID ID,M.NAME名称,M.CODE CODE,M.CONTROLLER CONTROLLER,   来自M_MENU的M.PARENT_ID PARENT加入UA.ID_MENU上的M_USER_ACCESS UA   = M.ID,其中UA.ID_ROLE =“ + idRole +”

我在searchByRole中的方法menuModelListRole = menuCriteria.list();中出错。我该如何解决该问题?

1 个答案:

答案 0 :(得分:0)

这里private Integer idRole;是Integer类型的,但是您正在idRole中将Restrictions.eq("idRole", ""+idRole+"")作为String传递。所以你得到java.lang.String cannot be cast to java.lang.Integer in Spring

将限制值从Restrictions.eq("idRole", ""+idRole+"")更改为Restrictions.eq("idRole", idRole)应该可以解决您的问题。