I have an enumeration in scala mapped to strings in JPA. For more comfortable coding, I defined implicit conversions between them. So I now can define value val person.role = "User", – person.role is the enumeration type "User" a String so there’s the conversion. But when I try to compare these two, I always get false, because the def equals (arg0: Any) : Boolean takes Any so there’s not any conversion triggered. I need some explicit conversion, but my plan was to be able to omit that, what do you think is the best practice | neatest solution here?
I have an enumeration in scala mapped to strings in JPA. For more comfortable
Share
The
Value("User")in yourEnumerationis of typeVal. And I believe it’s implementation ofequalsdoes not compare the string name of the value. I think one heavy handed way of doing this is creating your ownEnumerationandValso that it returns true if the name match.But in my code uses, not with JPA, I always convert the string into the
MyEnumeration.Value. This is easy with things like:Note that when using
withName, if the string does not match any name in the enumeration you get an exception.Then always use the enumeration fields in your comparisons:
If JPA only returns a string, and there is no way around it. Then I think the best option is to either convert the value to string and match string to string, or upgrade the string to a Val and compare Val. Mixing these types will not work for comparison, unless you you implement some kind of extension to the
equalsmethod, and that is tricky.