1

I have to convert the following NamedSqlParameterSource in Hibernate:-

    final List<MenuActionMapping>  menusList;


    MapSqlParameterSource sqlParams = new MapSqlParameterSource();


    menusList = namedParameterJdbcTemplate.query("call sp_proc()",sqlParams ,new RowMapper<MenuActionMapping>() {
                @Override
                public MenuActionMapping mapRow(ResultSet resultset, int i)
                        throws SQLException {

    MenuActionMapping menuActionMapping=new MenuActionMapping();


                    menuActionMapping.setMenuKey(resultset.getString("KMM_MENU_KEY"));

                    menuActionMapping.setDisplayName(resultset.getString("KMM_DISPLAY_NAME"));
                    menuActionMapping.setMenuActionFlag(resultset.getInt("KMM_ACTION_FLAG"));
                    menuActionMapping.setMenuActive(resultset.getInt("KMM_ACTIVE"));
                    menuActionMapping.setMenuLevel(resultset.getInt("str_len"));

String str=resultset.getString("menu_actions");

String [] actions=str.split(",");
if(resultset.getInt("KRMM_ACTIVE")==1)
{
menuActionMapping.setActive(true);
}
else
{
    menuActionMapping.setActive(false);
}

for(String strAct:actions)
{
     if(strAct.equals("ADD"))
    {
    menuActionMapping.setAddCheckBox(true);
    menuActionMapping.setAddCheckBoxDisabled("true");
    }
                            if(strAct.equals("VIEW"))
                            {
                                menuActionMapping.setViewCheckBox(true);
                                menuActionMapping.setViewCheckBoxDisabled("true");
                            }
                            if(strAct.equals("DELETE"))
                            {
                                menuActionMapping.setDeleteCheckBox(true);
                                menuActionMapping.setDeleteCheckBoxDisabled("true");
                            }

                            if(strAct.equals("EDIT"))
                            {
                                menuActionMapping.setEditCheckBox(true);
                                menuActionMapping.setEditCheckBoxDisabled("true");
                            }
                            if(strAct.equals("DOWNLOAD"))
                            {
                                menuActionMapping.setDownloadCheckBox(true);
                                menuActionMapping.setDownloadCheckBoxDisabled("true");
                            }

                        }               

                        return menuActionMapping;
                    }
            });
        System.out.println(menusList);

    return menusList;

I dont have idea about how namedJdbcTemplate and Map Row Works so i am getting a Problem.. I also wrote alternate code in hibernate but it doesnt work:-

                    final List<MenuActionMapping> menusList; 

        Query query= getSession().createSQLQuery("call kyc.sp_proc()");

        menusList=query.list();
        System.out.println(menusList);
        return menusList;

I think I am not setting MenuAction Mapping Object so how to achive the purpose?

Also I want to Manipulate the columns before setting it into the object how can i do it in hibernate....

The main code that is troubling me is this:-

String str=resultset.getString("menu_actions");
String [] actions=str.split(",");
if(resultset.getInt("KRMM_ACTIVE")==1)
{
    menuActionMapping.setActive(true);
}
else
{
    menuActionMapping.setActive(false);
}

for(String strAct:actions)
{
    if(strAct.equals("ADD"))
   {
        menuActionMapping.setAddCheckBox(true);
        menuActionMapping.setAddCheckBoxDisabled("true");
}
    if(strAct.equals("VIEW"))
    {
            menuActionMapping.setViewCheckBox(true);
        menuActionMapping.setViewCheckBoxDisabled("true");
}
    if(strAct.equals("DELETE"))
    {
        menuActionMapping.setDeleteCheckBox(true);
            menuActionMapping.setDeleteCheckBoxDisabled("true");
    }

    if(strAct.equals("EDIT"))
    {
        menuActionMapping.setEditCheckBox(true);
        menuActionMapping.setEditCheckBoxDisabled("true");
    }
    if(strAct.equals("DOWNLOAD"))
    {
        menuActionMapping.setDownloadCheckBox(true);
        menuActionMapping.setDownloadCheckBoxDisabled("true");
    }

How to set mutiple attribute based in 1 column in hibernate...

Ankit Duggal
  • 55
  • 2
  • 13

2 Answers2

1

namedJdbcTemplate helps you to reduce the boilerplate code like getting,closing connection etc while Row mapper helps you to iterate over returned result set and map it to desired Java class.
Check this http://www.mkyong.com/hibernate/how-to-call-store-procedure-in-hibernate/

Ajinkya
  • 22,324
  • 33
  • 110
  • 161
  • I want to **perform operation** on each and **every column** before adding it into object so how can i do it in hibernate... in the link you specify it simply loops over the list and then set the pojo object but I want to manipulate the values before setting into the pojo.. – Ankit Duggal Oct 18 '13 at 10:15
  • @AnkitDuggal: What operation you want to perform? You can iterate over list and do the same – Ajinkya Oct 18 '13 at 10:22
  • I want **set many fields** in **object** based upon the **status** column of the table.. As Row Mapper allows to manipulate every Column While adding mapping it to object attributes – Ankit Duggal Oct 18 '13 at 11:10
0

Thanks To @Pratik on How to map columns in hibernate with class attributes? I got the answer to my question i can achieve the same as row mappper of jdbc template in hibernate using BasicTransformerAdapter in hibernate. My code is as follows:-

 final List<MenuActionMapping>  menusList;
                menusList = getSession().createSQLQuery("CALL kyc.sp_proc()").setResultTransformer(new BasicTransformerAdapter() {


                    private static final long serialVersionUID = 1L;
                    @Override
                      public Object transformTuple(Object[] tuple, String[] aliases)
                    {
                        MenuActionMapping menuActionMapping=new MenuActionMapping();
                        menuActionMapping.setMenuId((Integer)tuple[0]);
                        menuActionMapping.setMenuKey((String)tuple[1]);
                        menuActionMapping.setDisplayName((String)tuple[3]);
                        menuActionMapping.setMenuActionFlag((Integer)tuple[5]);
                        final Boolean b=(Boolean)tuple[6];
                        menuActionMapping.setMenuActive(b? 1 : 0);
                        final BigInteger big=(BigInteger) tuple[9];
                        menuActionMapping.setMenuLevel(big.intValue());
                        String str=(String)tuple[10];
                        String [] actions=str.split(",");
                        if(b==true)
                        {
                            menuActionMapping.setActive(true);
                        }
                        else
                        {
                            menuActionMapping.setActive(false);
                        }

                        for(String strAct:actions)
                        {
                            if(strAct.equals("ADD"))
                            {
                                menuActionMapping.setAddCheckBox(true);
                                menuActionMapping.setAddCheckBoxDisabled("true");
                            }
                            if(strAct.equals("VIEW"))
                            {
                                menuActionMapping.setViewCheckBox(true);
                                menuActionMapping.setViewCheckBoxDisabled("true");
                            }
                            if(strAct.equals("DELETE"))
                            {
                                menuActionMapping.setDeleteCheckBox(true);
                                menuActionMapping.setDeleteCheckBoxDisabled("true");
                            }

                            if(strAct.equals("EDIT"))
                            {
                                menuActionMapping.setEditCheckBox(true);
                                menuActionMapping.setEditCheckBoxDisabled("true");
                            }
                            if(strAct.equals("DOWNLOAD"))
                            {
                                menuActionMapping.setDownloadCheckBox(true);
                                menuActionMapping.setDownloadCheckBoxDisabled("true");
                            }

                        }               

                        return menuActionMapping;

                    }


                }).list();
Community
  • 1
  • 1
Ankit Duggal
  • 55
  • 2
  • 13