Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: HHH015007: Illegal argument on static metamodel field inject
PostPosted: Thu Nov 09, 2017 5:34 am 
Newbie

Joined: Wed Nov 26, 2014 5:56 am
Posts: 12
I'm trying to map an entity field that is a set of Java Class objects to a comma-delimited string of the simple class names in the database. The implementation essentially follows the solution proposed by this answer on Stackoverflow: https://stackoverflow.com/a/34061723.

Field definition in my entity:
Code:
@Convert(converter = ClassSimpleNameConverter.class)
private Set<Class<?>> types;


JPA converter methods - `types` is a Map of (simple class name String, Class) pairs that are allowed:
Code:
/* @see javax.persistence.AttributeConverter#convertToDatabaseColumn(java.lang.Object) */
@Override
public String convertToDatabaseColumn(final Set<Class<? extends T>> actual)
{
   if (actual == null) return null;
   return actual.stream()
      .map(Class::getSimpleName)
      .filter(t -> types.containsKey(t))
      .collect(Collectors.joining(","));
}

/* @see javax.persistence.AttributeConverter#convertToEntityAttribute(java.lang.Object) */
@Override
public Set<Class<? extends T>> convertToEntityAttribute(final String dbData)
{
   if (dbData == null) return null;
   return Arrays.stream(dbData.split(","))
      .map(t -> types.get(t))
      .collect(Collectors.toSet());
}


This works nicely except when creating the JPA meta model for the entity using Hibernate's meta model generator, version 5.2.12 (pretty recent :-). Due to the fact that the unconverted field's type is `Set`, the corresponding constant in the metamodel is typed to `SetAttribute<MyEntity, Class> types`. This, in turn, leads to the following exception when the container deploys the application:
Code:
ERROR [org.hibernate.metamodel.internal.MetadataContext] (ServerService Thread Pool -- 66) HHH015007: Illegal argument on static metamodel field injection : mypackage.MyEntity_#types; expected type :  org.hibernate.metamodel.internal.SingularAttributeImpl; encountered type : javax.persistence.metamodel.SetAttribute

To me, this appears to be a kind of corner case, if not a gap in the design of the JPA specification. What would be the correct behavior here? Should the meta model generator consider the target type of an AttributeConverter (which is String in this case). I tried to annotate the field as @Basic but that didn't have an effect.


Top
 Profile  
 
 Post subject: Re: HHH015007: Illegal argument on static metamodel field inject
PostPosted: Thu Nov 09, 2017 5:51 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1534
Location: Romania
We have added an example for a comma-delimited String Type in our User Guide.

Unlike that StackOverflow question, we are using a Hibernate custom Type which is way superior to JPA AttributeConverter.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: HHH015007: Illegal argument on static metamodel field inject
PostPosted: Thu Nov 09, 2017 6:04 am 
Newbie

Joined: Wed Nov 26, 2014 5:56 am
Posts: 12
vlad wrote:
We have added an example for a comma-delimited String Type in our User Guide.

Unlike that StackOverflow question, we are using a Hibernate custom Type which is way superior to JPA AttributeConverter.


Nice. Still, why do we invest so much time and efforts in a thing called JPA in the first place, if every second answer I get when using something out of the JPA standard is like "hey, forget about JPA and use our (proprietary) stuff". To a large degree, we are using JPA because we want to remain portable. What precisely makes a Hibernate custom type way superior to a JPA AttributeConverter? Second, can we stick to the question, please. What would be the correct behavior for the JPA meta model generator if it sees a field that seems to be a collection field but which is actually not because of an additional AttributeConverter? What about the exception thrown at runtime?


Top
 Profile  
 
 Post subject: Re: HHH015007: Illegal argument on static metamodel field inject
PostPosted: Thu Nov 09, 2017 7:52 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1534
Location: Romania
Quote:
Still, why do we invest so much time and efforts in a thing called JPA in the first place, if every second answer I get when using something out of the JPA standard is like "hey, forget about JPA and use our (proprietary) stuff".


It's the same with SQL Standard, isn't it? I covered this topic in this free chapter of my book, High-Performance Java Persistence.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
 Post subject: Re: HHH015007: Illegal argument on static metamodel field inject
PostPosted: Thu Nov 09, 2017 9:15 am 
Newbie

Joined: Wed Nov 26, 2014 5:56 am
Posts: 12
vlad wrote:
It's the same with SQL Standard, isn't it?

Sure, that's the case for pretty much every standard for which there are different implementations. But that does not justify suggesting its a bad/naive/unrealisitic idea if someone wants to stick with the standard; in particular, if there is no need to use non-standard features. Which brings us back to the question what precisely makes a Hibernate custom type way superior to a JPA AttributeConverter?


Top
 Profile  
 
 Post subject: Re: HHH015007: Illegal argument on static metamodel field inject
PostPosted: Thu Nov 09, 2017 9:48 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1534
Location: Romania
The best way to answer your question is to ask you to try to map a Jackson JsonNode with Attributeconverter or a PostgreSQL ARRAY if you want.

Since the Attributeconverter does not give you access to the underlying JDBC PreparedStatement or ResultSet, you'll see that it's not very easy to achieve that goal.

_________________
If you liked my answer, you are going to love my High-Performance Java Persistence book and my blog as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.