JPA AttributeConverter works unexpectedly

36 views
Skip to first unread message

Francois Green

unread,
Aug 27, 2016, 4:26:28 PM8/27/16
to ceylon-users
I've implemented an AttributeConverter which works but seems that it shouldn't.   By adding the convert annotation to the target attribute, without specifying a converter class, the attribute converter just works.

shared abstract class Role(String role) of host | guest {
 
string => role;
}
shared
object host extends Role("HOST") {}
shared
object guest extends Role("GUEST") {}

entity
shared
class Person(firstname, lastname, middle, role) {
 
  id generated
{ strategy = auto; }
  shared
Int? id = null;
  shared variable
String firstname;
  shared variable
String lastname;
  shared variable
String middle;
  convert
//NOTE: Converter not specified
  shared variable
Role role;
}

converter
//NOTE: autoApply not set and should false
shared
class RoleConverter() satisfies AttributeConverter<Role, JString> {
 
  convertToDatabaseColumn
(Role role) => javaString(role.string);
 
  convertToEntityAttribute
(JString string) => if (string == "HOST") then host else guest;
}

At least none of the documentation I've read shows anything similar in Java.  While I'm happy with the behavior, as trying to follow the Java EE 7 documentation only yielded errors, I want know if it's correct and I can rely on it.

I'm using Ceylon 1.2.2 with EclipseLink 2.6.2

Gavin King

unread,
Aug 27, 2016, 4:38:04 PM8/27/16
to ceylon...@googlegroups.com
Sounds like an EclipseLink question to me.
> --
> You received this message because you are subscribed to the Google Groups
> "ceylon-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ceylon-users...@googlegroups.com.
> To post to this group, send email to ceylon...@googlegroups.com.
> Visit this group at https://groups.google.com/group/ceylon-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/ceylon-users/cfbfc4dd-10eb-4511-b98f-aa7b6db21153%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
Gavin King
ga...@ceylon-lang.org
http://profiles.google.com/gavin.king
http://ceylon-lang.org
http://hibernate.org
http://seamframework.org
Message has been deleted
Message has been deleted

Francois Green

unread,
Aug 27, 2016, 9:51:33 PM8/27/16
to ceylon-users
You're right!  I recreated the project in Java and got the same behavior.  In fact, the basic string conversion works with this:

converter

shared
class RoleConverter() satisfies AttributeConverter<Role, JString> {

 
  convertToDatabaseColumn
(Role role) => nothing;
 
  convertToEntityAttribute
(JString string) => nothing;
}

I can't find any documentation on this feature, but it seems to be a part of JPA 2.1.  Thanks for putting me on the right track.
Reply all
Reply to author
Forward
0 new messages