0

I have following method,

public Project getProjectByUser(long userId)
            throws IOException {

        SqlSession sqlSession = null;
        Project response = null;

        Map<String, Long> projectParam = new HashMap<String, Long>();
        projectParam.put("userId", userId);

        try {

            sqlSession = DBSessionManager.getInstance().getSessionFactory()
                    .openSession();

            LOG.debug("SqlSession opened for Project mapper");

            ProjectMapper projectMapper = sqlSession
                    .getMapper(ProjectMapper.class);

            sqlSession.insert("getProjectByUserId", projectParam);
            sqlSession.commit();

            response = projectMapper.getProjectByUserId(userId);

        } finally {
            if (sqlSession != null) {
                sqlSession.close();
                LOG.debug("SqlSession closed");
            } else {
                System.out.println("_sql session null");
            }

        }

        return response;
    }

And in xml file i have the following code.

<select id="getProjectByUserId" resultMap="projectResultMap"
        parameterType="map" flushCache="false" useCache="false">
        SELECT
        project_id,
        user_id, project_name,
        created_date,
        last_updated_date FROM
        project
        WHERE
        user_id=#{userId}
    </select>

When I replaced (hard coded the value) the user_id=#{userId} part as user_id=1 expected result is returned. But when I pass it from client application though the value is set to the map correctly the query doesn't get it correct and results in a null return. What am I doing wrong here.

My ProjectMapper class's method definition is,

public Project getProjectByUserId(long userIdParam);

Update: Following is the service interface method,

 @GET
 @Path("{userId}/{projectName}")
 @Produces("application/json")
 public Project getProjectByUser(@PathParam("userId") long userId);

and implementation of the above calls the data layer method (first mentioned)

Community
  • 1
  • 1
Isuru Gunawardana
  • 2,847
  • 6
  • 28
  • 60

1 Answers1

2

Try to define your mapper like this:

public Project getProjectByUserId(@Param("userId") long userIdParam); 
AlexW
  • 896
  • 7
  • 9
  • Thanks for the quick reply, I have updated the question, I hv already have this method and I'm passing that value to data layer please see the update – Isuru Gunawardana Apr 06 '15 at 20:34
  • Ok, I see now. But it does not matter - you still need `@Param` annotation on data layer method. `@Param` is needed for myBatis to know how to inject your `userIdParam` into SQL. It has nothing to do with `@PathParam`, which is actually needed for web mapping. – AlexW Apr 06 '15 at 21:20