Comments (6)
or maybe just good description in documentation.
from modelmapper.
Enums are mappable so long as the source and destination enum names are the same. Is there an example you can share that is giving you trouble?
from modelmapper.
First of all, thanks for fast replay ;)
Code:
public class TreeNodeMap extends PropertyMap<AttributeStructureNode, TreeNode> {
@Override
protected void configure() {
final ValueType type;
switch (source.getAttributeKind()) {
case TEXT_TYPE:
type = ValueType.STRING;
break;
case FLOAT_TYPE:
type = ValueType.DECIMAL;
break;
case INTEGER_TYPE:
type = ValueType.INTEGER;
break;
case LOGICAL_TYPE:
type = ValueType.BOOLEAN;
break;
case DATE_TYPE:
type = ValueType.DATE;
break;
default:
type = null;
break;
}
map().setAttributeKind(type);
}
}
When I'm calling source.getAttributeKind() it throws me exception:
ModelMapper configuration errors:
- An error occured while building mappings
1 error
RequestURI=/sdlconsole/MainInterface
Caused by:
org.modelmapper.ConfigurationException: ModelMapper configuration errors:
- An error occured while building mappings
1 error
at org.modelmapper.internal.Errors.throwConfigurationExceptionIfErrorsExist(Errors.java:224)
at org.modelmapper.internal.MappingBuilderImpl.build(MappingBuilderImpl.java:138)
at org.modelmapper.internal.TypeMapImpl.addMappings(TypeMapImpl.java:66)
at org.modelmapper.internal.TypeMapStore.getOrCreate(TypeMapStore.java:93)
at org.modelmapper.ModelMapper.addMappings(ModelMapper.java:90)
at pl.dandelite.empik.sdl.manager.server.service.MainInterfaceServiceImpl.(MainInterfaceServiceImpl.java:77)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at java.lang.Class.newInstance0(Class.java:355)
at java.lang.Class.newInstance(Class.java:308)
at org.mortbay.jetty.servlet.Holder.newInstance(Holder.java:153)
at org.mortbay.jetty.servlet.ServletHolder.getServlet(ServletHolder.java:339)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:463)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:324)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:843)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:647)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)
Caused by: org.modelmapper.internal.ErrorsException
at org.modelmapper.internal.Errors.toException(Errors.java:237)
at org.modelmapper.internal.ProxyFactory.proxyFor(ProxyFactory.java:85)
at org.modelmapper.internal.MappingInterceptor.intercept(MappingInterceptor.java:41)
at pl.dandelite.empik.sdl.core.domain.dataStructure.AttributeStructureNode$$EnhancerByModelMapper$$a6c9de64.getAttributeKind()
at pl.dandelite.empik.sdl.manager.server.modelmapper.TreeNodeMap.configure(TreeNodeMap.java:15)
at org.modelmapper.PropertyMap.configure(PropertyMap.java:270)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.modelmapper.internal.MappingBuilderImpl.build(MappingBuilderImpl.java:123)
... 29 more
from modelmapper.
PropertyMap.source
isn't meant to be used for anything other than defining mappings. This is why you're getting an exception when getAttributeKind is called. Mapping actual enum values isn't supported yet by the mapping API, but you can accomplish the same thing by creating a Converter
:
modelMapper.createTypeMap(Source.class, Dest.class).setConverter(new Converter<Source, Dest>() {
public Dest convert(MappingContext<Source, Dest> context) {
switch (context.getSource()) {
case A: return Dest.One;
case B: return Dest.Two;
case C: return Dest.Three;
default: return null;
}
}
});
I actually just pushed a small change to make this possible. I'll be pushing a release today that will include that change. Let me know if this doesn't resolve your use case.
from modelmapper.
ok, thanks for example. This time I used converter, but know I have another exception:
Failed to instantiate instance of destination pl.dandelite.empik.sdl.manager.shared.model.ValueType. Ensure that pl.dandelite.empik.sdl.manager.shared.model.ValueType has a non-private no-argument constructor.
Of course its not possible to create public constructor for enum type ;)
from modelmapper.
Can you post some example mapping code that led to that exception? You may be hitting on a different scenario than I've encountered. This test works fine:
from modelmapper.
Related Issues (20)
- ModelMapper mapping is updating table primary key instead of foreign key. HOT 1
- Error mapping, then reboot, works for a while
- How to skip not loaded Lazy fields?
- Test fails after upgrading from 3.1.0 to 3.1.1
- Java 21 support HOT 18
- Support of Java Module System
- Custom mapping in nested objects HOT 4
- Skip field of generic class HOT 1
- Mapping Source object to Destination object
- Mapping nested generic object with generic HOT 1
- Use specific converter for one field
- Model Mapper does not map values correctly (Intermittent - once in a few tries)
- Model Mapper Low performance
- Jackson Module: @JsonTypeInfo support for automatically handling inheritance HOT 1
- ModelMapper fails to map nested entity user field HOT 1
- When condition does not work for null-field
- How to ignore mapping errors HOT 1
- ModelMapper not working with nested generics
- modelmapper Hot deployment HOT 1
- How to Throw Validation exception when Source has a property that cannot be mapped to Destination HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from modelmapper.