You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Zbynek Vavros <zb...@gmail.com> on 2019/02/04 12:51:13 UTC
Model map with enum as a key
I got this class that has map with enums as keys:
public class MasterClass {
private Map<MyEnum, String> config = Maps.newHashMap();
}
enum is classic:
public enum MyEnum {
VALUE1,
VALUE2
}
now I would like to use one entry of this map as a model for TextField:
new RequiredTextField<>("componentId", new
PropertyModel<>(modelObject.getConfig(), "VALUE1")))
but when saved to DB Hibernate says the key is String and Enum is expected:
java.lang.ClassCastException: java.lang.String cannot be cast to
java.lang.Enum
Using ChainingModel or
Model.of(modelObject.getConfig().get(MyEnum.VALUE1))) causes the value not
to be bind at all.
I know I can use "side" property and fill this map manually in onSubmit
just before saving to DB but would like to avoid that.
Thanks
Re: Model map with enum as a key
Posted by Francois Meillet <fr...@gmail.com>.
you are saving the value and not the key, which is why hibernate complains.
WHy not choose a DropDownBox ?
François
> Le 4 févr. 2019 à 14:03, Zbynek Vavros <zb...@gmail.com> a écrit :
>
> Do you mean to manually iterate through the map and converting the string
> keys
> Wicket binds to enum keys? Even if I try to iterate through the map I get
> the same ClassCastException.
> Somehow Wicket managed to insert String as a key instead of enum.
>
> Zbynek
>
>
>
> On Mon, Feb 4, 2019 at 1:55 PM Francois Meillet <fr...@gmail.com>
> wrote:
>
>> use MyEnum.valueOf("your string ») before saving the data
>>
>> François
>>
>>
>>
>>> Le 4 févr. 2019 à 13:51, Zbynek Vavros <zb...@gmail.com> a écrit
>> :
>>>
>>> I got this class that has map with enums as keys:
>>>
>>> public class MasterClass {
>>>
>>> private Map<MyEnum, String> config = Maps.newHashMap();
>>> }
>>>
>>> enum is classic:
>>>
>>> public enum MyEnum {
>>> VALUE1,
>>> VALUE2
>>> }
>>>
>>> now I would like to use one entry of this map as a model for TextField:
>>>
>>> new RequiredTextField<>("componentId", new
>>> PropertyModel<>(modelObject.getConfig(), "VALUE1")))
>>>
>>> but when saved to DB Hibernate says the key is String and Enum is
>> expected:
>>> java.lang.ClassCastException: java.lang.String cannot be cast to
>>> java.lang.Enum
>>>
>>> Using ChainingModel or
>>> Model.of(modelObject.getConfig().get(MyEnum.VALUE1))) causes the value
>> not
>>> to be bind at all.
>>>
>>> I know I can use "side" property and fill this map manually in onSubmit
>>> just before saving to DB but would like to avoid that.
>>>
>>> Thanks
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
>> For additional commands, e-mail: users-help@wicket.apache.org
>>
>>
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Model map with enum as a key
Posted by Zbynek Vavros <zb...@gmail.com>.
Do you mean to manually iterate through the map and converting the string
keys
Wicket binds to enum keys? Even if I try to iterate through the map I get
the same ClassCastException.
Somehow Wicket managed to insert String as a key instead of enum.
Zbynek
On Mon, Feb 4, 2019 at 1:55 PM Francois Meillet <fr...@gmail.com>
wrote:
> use MyEnum.valueOf("your string ») before saving the data
>
> François
>
>
>
> > Le 4 févr. 2019 à 13:51, Zbynek Vavros <zb...@gmail.com> a écrit
> :
> >
> > I got this class that has map with enums as keys:
> >
> > public class MasterClass {
> >
> > private Map<MyEnum, String> config = Maps.newHashMap();
> > }
> >
> > enum is classic:
> >
> > public enum MyEnum {
> > VALUE1,
> > VALUE2
> > }
> >
> > now I would like to use one entry of this map as a model for TextField:
> >
> > new RequiredTextField<>("componentId", new
> > PropertyModel<>(modelObject.getConfig(), "VALUE1")))
> >
> > but when saved to DB Hibernate says the key is String and Enum is
> expected:
> > java.lang.ClassCastException: java.lang.String cannot be cast to
> > java.lang.Enum
> >
> > Using ChainingModel or
> > Model.of(modelObject.getConfig().get(MyEnum.VALUE1))) causes the value
> not
> > to be bind at all.
> >
> > I know I can use "side" property and fill this map manually in onSubmit
> > just before saving to DB but would like to avoid that.
> >
> > Thanks
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>
Re: Model map with enum as a key
Posted by Francois Meillet <fr...@gmail.com>.
use MyEnum.valueOf("your string ») before saving the data
François
> Le 4 févr. 2019 à 13:51, Zbynek Vavros <zb...@gmail.com> a écrit :
>
> I got this class that has map with enums as keys:
>
> public class MasterClass {
>
> private Map<MyEnum, String> config = Maps.newHashMap();
> }
>
> enum is classic:
>
> public enum MyEnum {
> VALUE1,
> VALUE2
> }
>
> now I would like to use one entry of this map as a model for TextField:
>
> new RequiredTextField<>("componentId", new
> PropertyModel<>(modelObject.getConfig(), "VALUE1")))
>
> but when saved to DB Hibernate says the key is String and Enum is expected:
> java.lang.ClassCastException: java.lang.String cannot be cast to
> java.lang.Enum
>
> Using ChainingModel or
> Model.of(modelObject.getConfig().get(MyEnum.VALUE1))) causes the value not
> to be bind at all.
>
> I know I can use "side" property and fill this map manually in onSubmit
> just before saving to DB but would like to avoid that.
>
> Thanks
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org
Re: Model map with enum as a key
Posted by Bas Gooren <ba...@iswd.nl>.
Hi!
In your first code example you are binding the property model to your
config map, using a string key for lookup (“VALUE1”).
Since generics in java is mostly syntactic sugar, I think the PropertyModel
reads & writes from your config map using a String key.
This is why hibernate complains: the map contains a String key instead of
an Enum.
We use a specialized MapKeyModel in our apps for this:
---
import java.util.Map;
import org.apache.wicket.model.IModel;
public class MapKeyModel<K, V> implements IModel<V>
{
private final IModel<Map<K, V>> mapModel;
private final K key;
public static <K, V> MapKeyModel<K, V> of( IModel<Map<K, V>> mapModel,
K key )
{
return new MapKeyModel<K, V>( mapModel, key );
}
public MapKeyModel( IModel<Map<K, V>> mapModel, K key )
{
this.mapModel = mapModel;
this.key = key;
}
@Override
public V getObject()
{
Map<K, V> map = mapModel.getObject();
return map.get( key );
}
@Override
public void setObject( V object )
{
Map<K, V> map = mapModel.getObject();
map.put( key, object );
}
@Override
public void detach()
{
mapModel.detach();
}
}
---
Met vriendelijke groet,
Kind regards,
Bas Gooren
Op 4 februari 2019 bij 13:51:28, Zbynek Vavros (zbynekvavros@gmail.com)
schreef:
I got this class that has map with enums as keys:
public class MasterClass {
private Map<MyEnum, String> config = Maps.newHashMap();
}
enum is classic:
public enum MyEnum {
VALUE1,
VALUE2
}
now I would like to use one entry of this map as a model for TextField:
new RequiredTextField<>("componentId", new
PropertyModel<>(modelObject.getConfig(), "VALUE1")))
but when saved to DB Hibernate says the key is String and Enum is expected:
java.lang.ClassCastException: java.lang.String cannot be cast to
java.lang.Enum
Using ChainingModel or
Model.of(modelObject.getConfig().get(MyEnum.VALUE1))) causes the value not
to be bind at all.
I know I can use "side" property and fill this map manually in onSubmit
just before saving to DB but would like to avoid that.
Thanks