I really like the idea of convention over configuration and MyBatis has been helpful in this so far. It can automatically map result sets to beans with little if any mapping info. Anw I would like to know whether it is possible (can be implemented) to work with immutable POJOs with minimum of mapping info as well. For instance:
<select id="getMenu" resultType="com.test.ChapterItem"> select id, name, rank from chapter </select>
public class ChapterItem { private int id; private String name; private int rank; public ChapterItem(int id, String name, int rank) { super(); this.id = id; this.name = name; this.rank = rank;
}
// .....
}
It could rely on the result set's columns order and the order of parameters in the constructor to make an instance. I would be grateful if somebody shed some light on this. I could try to make a patch for MyBatis if somebody highlighted the places to look at in the code. Thanks
On Thu, Sep 27, 2012 at 3:02 PM, IvanL <larionov.i...@gmail.com> wrote:
> I meant fields to be final. Sorry.
> public class ChapterItem {
> private final int id;
> private final String name;
> private final int rank;
> public ChapterItem(int id, String name, int rank) {
> super();
> this.id = id;
> this.name = name;
> this.rank = rank;
> }
On Thursday, September 27, 2012 8:51:45 AM UTC-4, IvanL wrote:
> Hi guys,
> I really like the idea of convention over configuration and MyBatis has > been helpful in this so far. It can automatically map result sets to beans > with little if any mapping info. Anw I would like to know whether it is > possible (can be implemented) to work with immutable POJOs with minimum of > mapping info as well. For instance:
> public class ChapterItem { > private int id; > private String name; > private int rank; > public ChapterItem(int id, String name, int rank) { > super(); > this.id = id; > this.name = name; > this.rank = rank; > }
> // ..... > }
> It could rely on the result set's columns order and the order of > parameters in the constructor to make an instance. > I would be grateful if somebody shed some light on this. I could try to > make a patch for MyBatis if somebody highlighted the places to look at in > the code. > Thanks
> On Thursday, September 27, 2012 8:51:45 AM UTC-4, IvanL wrote:
>> Hi guys,
>> I really like the idea of convention over configuration and MyBatis has
>> been helpful in this so far. It can automatically map result sets to beans
>> with little if any mapping info. Anw I would like to know whether it is
>> possible (can be implemented) to work with immutable POJOs with minimum of
>> mapping info as well. For instance:
>> public class ChapterItem {
>> private int id;
>> private String name;
>> private int rank;
>> public ChapterItem(int id, String name, int rank) {
>> super();
>> this.id = id;
>> this.name = name;
>> this.rank = rank;
>> }
>> // .....
>> }
>> It could rely on the result set's columns order and the order of
>> parameters in the constructor to make an instance.
>> I would be grateful if somebody shed some light on this. I could try to
>> make a patch for MyBatis if somebody highlighted the places to look at in
>> the code.
>> Thanks
I don't like the idea of relying on column ordering. It's a very loose way
to define column mappings.
Constructor mapping is a very clean and easy way to manage this. It's also
powerful enough to be used for immutable complex objects as well.
For example:
FruitBowl {
private final Apple apple;
private final Banana banana;
etc...
On Mon, Oct 1, 2012 at 3:38 AM, Dridi Boukelmoune <
> > On Thursday, September 27, 2012 8:51:45 AM UTC-4, IvanL wrote:
> >> Hi guys,
> >> I really like the idea of convention over configuration and MyBatis has
> >> been helpful in this so far. It can automatically map result sets to
> beans
> >> with little if any mapping info. Anw I would like to know whether it is
> >> possible (can be implemented) to work with immutable POJOs with minimum
> of
> >> mapping info as well. For instance:
> >> public class ChapterItem {
> >> private int id;
> >> private String name;
> >> private int rank;
> >> public ChapterItem(int id, String name, int rank) {
> >> super();
> >> this.id = id;
> >> this.name = name;
> >> this.rank = rank;
> >> }
> >> // .....
> >> }
> >> It could rely on the result set's columns order and the order of
> >> parameters in the constructor to make an instance.
> >> I would be grateful if somebody shed some light on this. I could try to
> >> make a patch for MyBatis if somebody highlighted the places to look at
> in
> >> the code.
> >> Thanks
> I don't like the idea of relying on column ordering. It's a very loose way > to define column mappings. > Constructor mapping is a very clean and easy way to manage this.
With constructor mapping you also depend on ordering:
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.
So it does not make much of a difference if you define the mapping in xml or let it be defined via convention based on the ResultSet metadata column information. In both cases you depend on ordering just because Java does not give you enough information.
On Mon, Oct 1, 2012 at 6:34 PM, Jens <jens.nehlme...@gmail.com> wrote:
>> I don't like the idea of relying on column ordering. It's a very loose way
>> to define column mappings.
>> Constructor mapping is a very clean and easy way to manage this.
> With constructor mapping you also depend on ordering:
> 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.
> So it does not make much of a difference if you define the mapping in xml or
> let it be defined via convention based on the ResultSet metadata column
> information. In both cases you depend on ordering just because Java does not
> give you enough information.
> I really like the idea of convention over configuration and MyBatis has > been helpful in this so far. It can automatically map result sets to beans > with little if any mapping info. Anw I would like to know whether it is > possible (can be implemented) to work with immutable POJOs with minimum of > mapping info as well. For instance:
> public class ChapterItem {
> private int id;
> private String name;
> private int rank;
> public ChapterItem(int id, String name, int rank) {
> super();
> this.id = id;
> this.name = name;
> this.rank = rank;
> }
> // .....
> }
> It could rely on the result set's columns order and the order of > parameters in the constructor to make an instance.
> I would be grateful if somebody shed some light on this. I could try to > make a patch for MyBatis if somebody highlighted the places to look at in > the code.
> Thanks