xross
xross

Reputation: 648

MyBatis mapping @One annotation issue

I have used myBatis and during the app start up an error occures:

org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for pl.net.manager.dao.ProductDAO.getService()
### The error may exist in pl/net/manager/dao/ProductDAO.java (best guess)
### The error may involve pl.net.manager.dao.ProductDAO.getProduct
### The error occurred while handling results
### SQL: select * from product where id=?
### Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for pl.net.manager.dao.ProductDAO.getService()

I have tables like: Product, Service, Rateplan

I have three DAO's:

ProductDAO:

@Mapper
public interface ProductDAO extends ServiceDAO, RateplanDAO {

    @Select("select * from product")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "productStatus", column = "product_status"),
            @Result(property = "createdBy", column = "created_by"),
            @Result(property = "createdDate", column = "created_date"),
            @Result(property = "modifiedBy", column = "modified_by"),
            @Result(property = "modifiedDate", column = "modified_date"),
            @Result(property = "service", column = "service", one = @One(select = "getService()")),
            @Result(property = "rateplan", column = "rateplan", one = @One(select = "getRateplan()"))
    })
    public List<Product> getProductList();

    @Select("select * from product where id=#{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "productStatus", column = "product_status"),
            @Result(property = "createdBy", column = "created_by"),
            @Result(property = "createdDate", column = "created_date"),
            @Result(property = "modifiedBy", column = "modified_by"),
            @Result(property = "modifiedDate", column = "modified_date"),
            @Result(property = "service", column = "service", one = @One(select = "getService()")),
            @Result(property = "rateplan", column = "rateplan", one = @One(select = "getRateplan()"))
    })
    public Product getProduct(Integer id);

ServiceDAO:

@Mapper
public interface ServiceDAO {

    @Select("select * from service where id=#{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "serviceStatus", column = "service_status"),
            @Result(property = "name", column = "name"),
            @Result(property = "createdBy", column = "created_by"),
            @Result(property = "createdDate", column = "created_date"),
            @Result(property = "modifiedBy", column = "modified_by"),
            @Result(property = "modifiedDate", column = "modified_date")
    })
    public Service getService(Integer id);

RateplanDAO:

@Mapper
public interface RateplanDAO {

    @Select("select * from rateplan where id=#{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "rateplanStatus", column = "rateplan_status"),
            @Result(property = "createdBy", column = "created_by"),
            @Result(property = "createdDate", column = "created_date"),
            @Result(property = "modifiedBy", column = "modified_by"),
            @Result(property = "modifiedDate", column = "modified_date")
    })
    public Rateplan getRateplan(Integer id);

And I have wrote simple test for product:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MainApplication.class)
@SpringBootTest
public class ProductDaoTest {

    @Autowired
    ProductDAO productDAO;

    @Test
    public void getAllProductsTest(){
        List<Product> list = productDAO.getProductList();
        assertNotNull(list);
        assertEquals(2, list.size());
    }

    @Test
    public void getSpecifiedProductTest(){
        Integer id =1;
        Product product = productDAO.getProduct(id);

        assertEquals(id, product.getId());

    }
}

If I understand correctly the problem is with mapping, but according to documentation it should have been working. Do you have an idea, how it can be fixed?

Upvotes: 1

Views: 2186

Answers (1)

Mateusz Stefaniak
Mateusz Stefaniak

Reputation: 905

You need to delete parenthesis after method name in @Result:

@Result(property = "service", column = "service", one = @One(select = "getService()"))

=>

@Result(property = "service", column = "service", one = @One(select = "getService"))

Upvotes: 2

Related Questions