-
Notifications
You must be signed in to change notification settings - Fork 12.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resolve POJO constructor arguments by name rather than position #721
Comments
I have committed the changes. <constructor>
<arg column="col2" name="secondArg" javaType="string" />
<arg column="col1" name="firstArg" javaType="int" />
</constructor> @ConstructorArgs({
@Arg(column = "col2", name = "secondArg", javaType = String.class),
@Arg(column = "col1", name = "firstArg", javaType = Integer.class)
})
@usethe4ce , @reddyalready |
@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? |
After some more digging, I found #968, where you mention a test that uses columnPrefix inside a constructor. This is exactly what I'm looking for, but I clone the mybatis code and was unable to find such a test. Could you please point me to this test? |
Thank you for the feedback, @reddyalready ! The test case I mentioned in #968 was this one . Regarding your question, it is possible with some limitations [1] if I understand correctly. [1] Collections are not supported as you found in #101 and it has low reusability because there is no p.s. public class User {
private Integer id;
private Address address;
public User(Integer id) {
this.id = id;
}
// getters only
} <select id="selectUser" resultMap="userRM">
select
u.id,
a.house_num addr_house_num,
a.line1 addr_line1
from user u, address a
where ...
</select>
<resultMap type="User" id="userRM">
<constructor>
<idArg column="id" javaType="int" />
</constructor>
<association property="address" resultMap="addressRM" columnPrefix="addr_" />
</resultMap>
<resultMap type="Address" id="addressRM">
<constructor>
<arg column="house_num" javaType="int" />
<arg column="line1" javaType="string" />
</constructor>
</resultMap> In this case, you can use |
Thank you very much @harawata, you've been a great help! I tried complex objects in constructor args shortly after and they seem to be working fine so far, I'll switch to your suggested method should things get more complex. |
It would be nice if it supported somewhat more standard approach with |
@dant3 , |
Hi @harawata I have add a test about this issue in this fork. Can u check it? I want MyBatis to map automatically with the constructor by argument names without defining Is this a feature that MyBatis intends to support or has already supported? I am using kotlin's data class, I want mybatis to be automatically mapped, just like I did through the empty constructor and getter setter in java. |
The docs say:
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.
The text was updated successfully, but these errors were encountered: