Resolve POJO constructor arguments by name rather than position
See original GitHub issueThe docs say:
In order to inject the results into the constructor, MyBatis needs to identify the constructor by the type of its parameters. Java has no way to introspect (or reflect) on parameter names. So when creating a constructor element, ensure that the arguments are in order, and that the data types are specified.
But actually there are few ways commonly used to get at the constructor’s parameter names. Spring can wire up immutable beans this way, for example. There’s compiling with debug info (very common nowadays), the older @ConstructorProperties
annotation, explicit annotations on each constructor parameter (e.g. MyBatis’s own @Param
), and in JDK8 the compilation option -parameters
.
It’s a huge advantage, of course, when dealing with immutables to be able to match constructor parameters to fields not positionally, which is quite fragile, but by name.
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (3 by maintainers)
I have committed the changes. You can specify the parameter name in
name
attribute of@Arg
annotation or<idArg />
and<arg />
elements.@Param
annotation or 2) compile with-parameters
option.javaType
can be omitted if there is a property with the same name and type.@usethe4ce , @reddyalready It would be great if you guys could try the latest 3.4.3-SNAPSHOT.
@harawata Just wanted to thank you again for doing this, I finally got to play around with it - using Kotlin/immutability no less, and it works like a charm 😄
I believe it is not possible yet to create complex immutable objects (resulting from joins, for example) using constructor args, e.g.:
I believe this is related to #101
Can you please confirm?