Hi, thanks for your fast response.
The provider solves the problem of constructing the object, but not the one
of filling it with data. I will copy the code of an example to illustrate
my domain model.
Foo class:
public class Foo {
private final String name;
private final Bar bar;
private Foo(Builder builder) {
name = builder.name;
bar = builder.bar;
}
public String getName() {
return name;
}
public Bar getBar() {
return bar;
}
public static final class Builder {
private String name;
private Bar bar;
public Builder() {
}
public Builder withName(String name) {
this.name = name;
return this;
}
public Builder withBar(Bar bar) {
this.bar = bar;
return this;
}
public Foo build() {
return new Foo(this);
}
}
}
The Bar class:
public class Bar {
private final String randomData;
private final String randomUser;
private Bar(Builder builder) {
randomData = builder.randomData;
randomUser = builder.randomUser;
}
public String getRandomData() {
return randomData;
}
public String getRandomUser() {
return randomUser;
}
public static final class Builder {
private String randomData;
private String randomUser;
public Builder() {
}
public Builder withRandomData(String randomData) {
this.randomData = randomData;
return this;
}
public Builder withRandomUser(String randomUser) {
this.randomUser = randomUser;
return this;
}
public Bar build() {
return new Bar(this);
}
}
}
With this model, using the provider i can write my own provider:
Provider<Bar> barProvider = new AbstractProvider<Bar>() {
@Override
protected Bar get() {
return new Bar.Builder().build();
}
};
But when i perform the mapping...
ModelMapper mapper = new ModelMapper();
mapper.getConfiguration().setProvider(barProvider);
return mapper.map(record, Bar.class);
It just can't found the setters, because the setters are in the builder,
not in the entity.
What I am trying to find is how to replace the "Entity.set" per
"EntityBuilder.setter" and after all setters are done call the build method.
Thank you!
El miércoles, 21 de enero de 2015, 18:19:07 (UTC+1), [email protected]
escribió:
>
> Hi - It looks like you want control over how Bar is constructed, using
> Bar.Builder, instead of letting ModelMapper construct it. If that is the
> case you'll want to use a Provider. Providers can be configured per
> property <http://modelmapper.org/user-manual/property-mapping/#providers>
> or more broadly <http://modelmapper.org/user-manual/providers/>.
>
> Cheers,
> Jonathan
>
> On Wednesday, January 21, 2015 at 2:24:36 AM UTC-8, [email protected]
> wrote:
>>
>> Hi, first of all i want to say that I am glad of all the jOOQ developers,
>> I've started the project using it and I like it very much.
>>
>> By the way, I'm facing a problem: I am trying to integrate the
>> modelMapper with jOOQ in my application but I am using the builder pattern
>> to build my entities. Is there an easy way to use the builder pattern with
>> ModelMapper?
>>
>> With single entities I have no problem, I just write:
>> Entity e = mapper.map(record, Builder.class).build();
>>
>> The problem is when I have entities inside them, for example:
>> class Foo {
>> private final Bar bar;
>> }
>>
>> The mapper tries to construct directly the Bar entity instead using the
>> Bar.Builder class.
>>
>> Thank you
>>
>
--
You received this message because you are subscribed to the Google Groups "jOOQ
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.