jackson-databind icon indicating copy to clipboard operation
jackson-databind copied to clipboard

Cannot serialize object from mybatis and it has a field lazy-loaded

Open formingform opened this issue 1 year ago • 3 comments

Search before asking

  • [X] I searched in the issues and found nothing similar.

Describe the bug

Jackson 2.13.5 Spring Boot 2.17.x Mybatis 3.5.x JDK 17

Here is my Mybatis map.xml


<resultMap id="BaseResultMap" type="User">
	<id property="id" column="id" jdbcType="INTEGER"/>
	<result property="name" column="name" jdbcType="VARCHAR"/>
	<collection property="bookNames"
				ofType="String"
				column="name"
				jdbcType="VARCHAR"
				select="BookMapper.listBookNamesByOwner" fetchType="lazy"/>
</resultMap>

......

<select id="selectByPrimaryKey" parameterType="java.lang.Long" resultMap="BaseResultMap">
	select
	<include refid="Base_Column_List" />
	from user
	where id = #{id,jdbcType=INTEGER}
</select>

jackson cannot serialize the result of selectByPrimaryKey because of fetchType="lazy", error message like this:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.apache.ibatis.executor.loader.javassist.JavassistProxyFactory$EnhancedResultObjectProxyImpl and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

and if set fetchType=“eager" or remove fetchType="lazy", Jackson works fine.

Version Information

Jackson verison: 2.13.5

Reproduction

formingform avatar Aug 28 '24 08:08 formingform

Not sure what you want us to do, here

yawkat avatar Aug 28 '24 08:08 yawkat

We do not generally accept issues where many libs are used. Reproducible cases should generally use only Jackson libs. I believe this should be reported to the myBatis community or on stackoverflow.com.

pjfanning avatar Aug 28 '24 08:08 pjfanning

Yes, "exotic" types by 3rd party libraries/frameworks tend to require special supported, provided often as Jackson datatype modules: for Hibernate, there is https://github.com/FasterXML/jackson-datatype-hibernate/ .

Such libraries would ideally be created and maintained by library/framework maintainers. But we sometimes accept contributions if users want to write implementations. That requires someone with myBatis and Jackson knowledge.

In this case it is very likely that such contribution (PR) would be needed. Or possibly someone sharing way they have used Jackson configuration and/or custom (de)serializers.

cowtowncoder avatar Aug 29 '24 02:08 cowtowncoder

Nothing we can do in jackson-databind: would require dedicated datatype module most likely. Will close: anyone can work on implementing and contributed such module; if so, can file an issue somewhere else; f.ex start a Discussion at

https://github.com/FasterXML/jackson/discussions

cowtowncoder avatar Nov 09 '24 01:11 cowtowncoder