ibatis-user-java mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From J S <sharpfel...@yahoo.ca>
Subject Mapper behaving differently between Eclipse and Maven
Date Wed, 17 Feb 2010 04:49:16 GMT
Hello there:

first of all a thanks to the authors for a great tool.  I am new to iBatis, but I'm already
getting great value out of it.  

I
am currently having a hard time diagnosing a problem with one of my
mappings.  Things seem to work fine when I run my unit test from within
Eclipse, but the same test fails consistently when I run it through
Maven.  Maybe some of the more experienced eyes can spot what I'm doing
wrong.

Before diving into the details of my code, following is a
sample of the data I'm trying to fetch.  Assume the following sample
data set (apologies for the ugly formatting):

mysql> select * from expense;
+----+---------------------+--------------+--------+-----------------------+-----------+
| id | created_on          | expense_date |
 amount | description           | bucket_id |
+----+---------------------+--------------+--------+-----------------------+-----------+
|  1 | 2010-02-14 14:24:05 | 2010-01-01   |  24.95 | Fruits and Vegetables |        
1 | 
|  2 | 2010-02-14 14:24:45 | 2010-01-25   |  15.00 | Coffee and Tea        |        
1 | 
|  3 | 2010-02-14 14:25:19 | 2010-01-30   |  32.98 | Steak and fish        |        
1 | 
|  4 | 2010-02-14 14:26:50 | 2010-02-04   |  40.95 | Sushi                
|         6 | 
|  5 |
 2010-02-14 14:27:19 | 2010-02-17   | 121.00 | Metro pass            |        
5 | 
|  6 | 2010-02-14 14:28:23 | 2010-03-07   |  39.98 | Book                 
|         7 | 
|  7 | 2010-03-01 16:45:12 | 2010-03-12   |  87.52 | Groceries            
|         1 | 
+----+---------------------+--------------+--------+-----------------------+-----------+
7 rows in set (0.00 sec)

The following query (which I want to define in my mappings) yields the results:

mysql> select expense_date, extract(YEAR_MONTH from expense_date) as period from expense
group by
 period;
+--------------+--------+
| expense_date | period |
+--------------+--------+
| 2010-01-01   | 201001 | 
| 2010-02-04   | 201002 | 
| 2010-03-07   | 201003 | 
+--------------+--------+
3 rows in set (0.00 sec)

mysql> 

Now, on Java land, I got the following:

ExpenseMapping.xml
------------------

<mapper namespace="com.foo.persistence.ibatis.mapper.ExpenseMapper">

... some mappings

    <select id="selectExpensePeriods" resultType="java.util.Date">
        select expense_date, extract(YEAR_MONTH from expense_date) as period from expense
group by period;
    </select>

... some more mappings

</mapper>

ExpenseMapper.java
------------------

public interface ExpenseMapper {
    
    List<Date>
 selectExpensePeriods();    
}

IbatisExpenseDao.java
---------------------

public class IbatisExpenseDao  {

    private SessionManager sessionManager;

    public IbatisExpenseDao( SessionManager sessionManager ) {
        this.sessionManager = sessionManager;
    }
    
    public List<ExpensePeriod> getPeriods() {
        List<Date> dates = mapper().selectExpensePeriods();
        return ExpensePeriod.listFrom( dates );
    }

    private ExpenseMapper mapper() {
        SqlSession session = sessionManager.getCurrentSession();
        return session.getMapper( ExpenseMapper.class );
   
 }
}


SessionManager.java
-------------------

class SessionManager {

    private final SqlSessionFactory sessionFactory;

    private ThreadLocal<SqlSessionWrapper> localSession = new ThreadLocal<SqlSessionWrapper>()
{
        @Override
        protected SqlSessionWrapper initialValue() {
            return newSqlSessionWrapper();
        }
    };
    
    SessionManager( SqlSessionFactory sessionFactory ) {
        this.sessionFactory = sessionFactory;
    }
    
    private SqlSessionWrapper newSqlSessionWrapper() {
        return new SqlSessionWrapper(
 sessionFactory.openSession() );
    }
    
    public SqlSession getCurrentSession() {
        SqlSessionWrapper session = localSession.get();
        if ( session.isTerminated() ) {
            session = newSqlSessionWrapper();
            localSession.remove();
            localSession.set( session );
        }
        return session;
    }
}

The
above class is meant to manage sessions on a per-thread basis; the idea
that each thread can share the same session so long as it's open.  The
moment the current session is terminated (by calling
close/commit/rollback) a new session is opened.

SqlSessionWrapper is just an adapter delegating all
method calls to its private SqlSession instance;  it marks its state as
terminated whenever close(), commit() and rollback() methods are
invoked.

Finally, the unit test that is giving me grief:

IbatisExpenseDaoTest.java
-------------------------

    @Test
    public void testGetPeriods() {
        
        ExpensePeriod[] expectedPeriods = getExpectedPeriods();
        
        List<ExpensePeriod> retrievedPeriods = expenseDao.getPeriods();
        
        assertThat( retrievedPeriods, hasItems( expectedPeriods ) );
    }

As
I mentioned, this test passes all the time in Eclipse, but fails all
the time in Maven (I get an empty list for retrievedPeriods).  At some
point in the past, this test was working on both, Eclipse and Maven.

Does
anyone have any ideas of what I may be doing wrong? or at least can you
suggest  a way in which I may debug this problem (ie is there any
iBatis specific tracing I can turn on to see what's going on?).

Thank you very much for taken the time to read all this.

Best regards.

J.


      __________________________________________________________________
Yahoo! Canada Toolbar: Search from anywhere on the web, and bookmark your favourite sites.
Download it now
http://ca.toolbar.yahoo.com.
Mime
View raw message