How to use MyBatis Optional Mapper with a One/Many relationship

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

How to use MyBatis Optional Mapper with a One/Many relationship

barkeldiho

Iam trying to access data in a database via MyBatis in a Java/Spring Boot project. I recognized that when there is no data found in the database, MyBatis will return null, hence as a return type I'd like to use an Optional<CustomType>.

This feature got introduced in version 3.5.0 of mybatis. But unfortunately it does not seem to work with an additionally supplied @Results {@Result} annotation, which I am required to use to resolve a one-to-many etc. relationship for some object attributes with another method call.

Does anyone know a solution for this dilemma?

For simple objects the returntype Optional<CustomObject> works fine if one omits the @Result{@Result} annotation completely.


What works:

CustomObject:

public class CustomObject {

    private Long attribute1;

    private String attribute2;
}

MyBatis DAO:

@Select("some SQL statement with columnnames matching the object attributes")
public Optional<CustomObject> methodName(@Param("input") final String input);

What does not work:

CustomObject:

public class CustomObject {

    private Long attribute1;

    private String attribute2;

    private List<String> attribute3 = new ArrayList<>();
}

with a similar DAO method.

It works though without the Optional in this manner:

@Select("some SQL statement with columnnames matching the object attributes")
@Results(value={
        @Result(property = "attribute1", column = "attribute1"),
        @Result(property = "attribute2", column = "attribute2"),
        @Result(property = "attribute3", column = "attribute3", javaType = List.class, many=@Many(select = "getattribute3Content"))
})
public CustomType methodName(@Param("input") final String input)

Does anyone know how I can possibly set the relationship for the attribute and at the same time return an Optional of the CustomObject?

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/294bce5d-6461-494a-a7fc-0de094c04654%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: How to use MyBatis Optional Mapper with a One/Many relationship

Guy Rouillier-2
On 7/18/2019 3:29:33 PM, "'barkeldiho' via mybatis-user" <[hidden email]> wrote:

Iam trying to access data in a database via MyBatis in a Java/Spring Boot project. I recognized that when there is no data found in the database, MyBatis will return null, hence as a return type I'd like to use an Optional<CustomType>.


This is not always the case.  MyBatis provides an property - returnInstanceForEmptyRow - you can specify if you want an object instance to be created for an empty row.  Not exactly what you were asking, but may suffice for your needs.

--
Guy Rouillier

--
You received this message because you are subscribed to the Google Groups "mybatis-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/mybatis-user/emf7da2a32-d354-4922-891f-b004f24fa3ef%40asus.