Re: Automatic mapping of immutable POJOs using the constructor

128 views
Skip to first unread message

IvanL

unread,
Sep 27, 2012, 9:02:00 AM9/27/12
to mybati...@googlegroups.com
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;
}

// .....
}

Dridi Boukelmoune

unread,
Sep 27, 2012, 9:39:28 AM9/27/12
to mybati...@googlegroups.com
Hi Ivan,

I hope JDBC guarantees the columns order because I love your idea :)

Dridi
--
Dridi Boukelmoune
Développeur/Formateur

GSM : +33 (0)6 17 91 14 23

Paul Krause

unread,
Sep 28, 2012, 8:03:36 PM9/28/12
to mybati...@googlegroups.com
http://www.mybatis.org/core/sqlmap-xml.html#constructor

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: 

<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

Dridi Boukelmoune

unread,
Oct 1, 2012, 4:38:48 AM10/1/12
to mybati...@googlegroups.com
Hi,

I think Ivan suggested implicit constructor mapping with types, just
like MB would automatically map field names to column names.

Dridi

Tim

unread,
Oct 1, 2012, 12:00:32 PM10/1/12
to mybati...@googlegroups.com
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...

Dridi Boukelmoune

unread,
Oct 1, 2012, 1:13:18 PM10/1/12
to mybati...@googlegroups.com
It looks rather possible !

http://docs.oracle.com/javase/1.4.2/docs/api/java/sql/ResultSetMetaData.html
(I know this is old documentation, but it's the first result I got =)

getColumnCount()
getColumnType(int column)

Dridi

On Mon, Oct 1, 2012 at 6:34 PM, Jens <jens.ne...@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.
>
> +1 for convention, if possible.
>
> -- J.

IvanL

unread,
Oct 3, 2012, 1:40:53 AM10/3/12
to mybati...@googlegroups.com
Hi guys,

With reference to our discussion, would anybody mind if I filed a feature request regarding this?

четверг, 27 сентября 2012 г., 15:51:45 UTC+3 пользователь IvanL написал:
Reply all
Reply to author
Forward
0 new messages