Unable to use JPA Attribute converters in karaf

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Unable to use JPA Attribute converters in karaf

Erdbeerheld1990
Hello,

we are using karaf version 4.2.7 with blueprint. Now the first time we want
to implement an  Attribute Converter
<https://www.baeldung.com/jpa-attribute-converters>   from JPA in order to
convert the string content of a column to an Object.

@Converter(autoApply = true)
public class DynamicViewConfigurationConverterJson implements
AttributeConverter<DynamicViewConfiguration, String> {
 
  private final static ObjectMapper objectMapper = new ObjectMapper();

  @Override
  public String convertToDatabaseColumn(DynamicViewConfiguration
dynamicViewConfiguration) {
    try {
      return objectMapper.writeValueAsString(dynamicViewConfiguration);
    } catch (JsonProcessingException ex) {
      return null;
    }
  }

  @Override
  public DynamicViewConfiguration convertToEntityAttribute(String dbData) {
    try {
      return objectMapper.readValue(dbData, DynamicViewConfiguration.class);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

}

And this is my entity:

@Entity
@Table(name = "CAIS_DYNAMIC_VIEW")
public class DynamicView {
  private Long id;
  private String viewName;
  private String detailViewRoute;
  private String translationKey;
  private String viewCategory;
  private DynamicViewConfiguration viewConfiguration;

  public DynamicView() {
  }

  @Id
  @GenericGenerator(name = "id_generator", strategy =
"com.cedros.cais.persistence.generator.IdGenerator")
  @GeneratedValue(generator = "id_generator")
  public Long getId() {
    return id;
  }

  public void setId(Long id) {
    this.id = id;
  }

  @Column(name = "VIEW_NAME", nullable = false)
  public String getViewName() {
    return viewName;
  }

  public void setViewName(String viewName) {
    this.viewName = viewName;
  }

  @Column(name = "DETAIL_VIEW_ROUTE")
  public String getDetailViewRoute() {
    return detailViewRoute;
  }

  public void setDetailViewRoute(String detailViewRoute) {
    this.detailViewRoute = detailViewRoute;
  }

  @Column(name = "TRANSLATION_KEY", nullable = false)
  public String getTranslationKey() {
    return translationKey;
  }

  public void setTranslationKey(String translationKey) {
    this.translationKey = translationKey;
  }

  @Column(name = "VIEW_CATEGORY")
  public String getViewCategory() {
    return viewCategory;
  }

  public void setViewCategory(String viewCategory) {
    this.viewCategory = viewCategory;
  }

  @Column(name = "VIEW_CONFIGURATION", nullable = false)
  @Convert( converter = DynamicViewConfigurationConverterJson.class)
  public DynamicViewConfiguration getViewConfiguration() {
    return viewConfiguration;
  }

  public void setViewConfiguration(DynamicViewConfiguration
viewConfiguration) {
    this.viewConfiguration = viewConfiguration;
  }
}


My Unit tests are working fine from spring context. But when i keep the
@Convert annotation and run it in karaf, my DAOs are not starting. They are
waiting for the persistence-Context and fail after some time - Declaritive
Services:

Status: GracePeriod
Blueprint
28.02.20 15:30
Missing dependencies:
(&(osgi.unit.name=caisPersistenceUnit)(objectClass=javax.persistence.EntityManager))
Declarative Services


When i remove the @Convert annotation it is not complaining, but then of
course my attribute converter has not any effect. I would be very pleased
for your help.

Best regards,
Andreas



--
Sent from: http://karaf.922171.n3.nabble.com/Karaf-User-f930749.html
Reply | Threaded
Open this post in threaded view
|

Re: Unable to use JPA Attribute converters in karaf

jbonofre
Hi,

Can you share the features you have installed about jpa ?

Regards
JB

> Le 28 févr. 2020 à 15:38, Erdbeerheld1990 <[hidden email]> a écrit :
>
> Hello,
>
> we are using karaf version 4.2.7 with blueprint. Now the first time we want
> to implement an  Attribute Converter
> <https://www.baeldung.com/jpa-attribute-converters>   from JPA in order to
> convert the string content of a column to an Object.
>
> @Converter(autoApply = true)
> public class DynamicViewConfigurationConverterJson implements
> AttributeConverter<DynamicViewConfiguration, String> {
>
>  private final static ObjectMapper objectMapper = new ObjectMapper();
>
>  @Override
>  public String convertToDatabaseColumn(DynamicViewConfiguration
> dynamicViewConfiguration) {
>    try {
>      return objectMapper.writeValueAsString(dynamicViewConfiguration);
>    } catch (JsonProcessingException ex) {
>      return null;
>    }
>  }
>
>  @Override
>  public DynamicViewConfiguration convertToEntityAttribute(String dbData) {
>    try {
>      return objectMapper.readValue(dbData, DynamicViewConfiguration.class);
>    } catch (IOException e) {
>      throw new RuntimeException(e);
>    }
>  }
>
> }
>
> And this is my entity:
>
> @Entity
> @Table(name = "CAIS_DYNAMIC_VIEW")
> public class DynamicView {
>  private Long id;
>  private String viewName;
>  private String detailViewRoute;
>  private String translationKey;
>  private String viewCategory;
>  private DynamicViewConfiguration viewConfiguration;
>
>  public DynamicView() {
>  }
>
>  @Id
>  @GenericGenerator(name = "id_generator", strategy =
> "com.cedros.cais.persistence.generator.IdGenerator")
>  @GeneratedValue(generator = "id_generator")
>  public Long getId() {
>    return id;
>  }
>
>  public void setId(Long id) {
>    this.id = id;
>  }
>
>  @Column(name = "VIEW_NAME", nullable = false)
>  public String getViewName() {
>    return viewName;
>  }
>
>  public void setViewName(String viewName) {
>    this.viewName = viewName;
>  }
>
>  @Column(name = "DETAIL_VIEW_ROUTE")
>  public String getDetailViewRoute() {
>    return detailViewRoute;
>  }
>
>  public void setDetailViewRoute(String detailViewRoute) {
>    this.detailViewRoute = detailViewRoute;
>  }
>
>  @Column(name = "TRANSLATION_KEY", nullable = false)
>  public String getTranslationKey() {
>    return translationKey;
>  }
>
>  public void setTranslationKey(String translationKey) {
>    this.translationKey = translationKey;
>  }
>
>  @Column(name = "VIEW_CATEGORY")
>  public String getViewCategory() {
>    return viewCategory;
>  }
>
>  public void setViewCategory(String viewCategory) {
>    this.viewCategory = viewCategory;
>  }
>
>  @Column(name = "VIEW_CONFIGURATION", nullable = false)
>  @Convert( converter = DynamicViewConfigurationConverterJson.class)
>  public DynamicViewConfiguration getViewConfiguration() {
>    return viewConfiguration;
>  }
>
>  public void setViewConfiguration(DynamicViewConfiguration
> viewConfiguration) {
>    this.viewConfiguration = viewConfiguration;
>  }
> }
>
>
> My Unit tests are working fine from spring context. But when i keep the
> @Convert annotation and run it in karaf, my DAOs are not starting. They are
> waiting for the persistence-Context and fail after some time - Declaritive
> Services:
>
> Status: GracePeriod
> Blueprint
> 28.02.20 15:30
> Missing dependencies:
> (&(osgi.unit.name=caisPersistenceUnit)(objectClass=javax.persistence.EntityManager))
> Declarative Services
>
>
> When i remove the @Convert annotation it is not complaining, but then of
> course my attribute converter has not any effect. I would be very pleased
> for your help.
>
> Best regards,
> Andreas
>
>
>
> --
> Sent from: http://karaf.922171.n3.nabble.com/Karaf-User-f930749.html

Reply | Threaded
Open this post in threaded view
|

Re: Unable to use JPA Attribute converters in karaf

Erdbeerheld1990
This post was updated on .
Hi,

yes of course. Seems to be only one feature.

karaf@root()> feature:list | grep jpa
camel-jpa                                       | 2.18.2           |        
| Uninstalled | camel-2.18.2                      |
openjpa3                                        | 3.0.0            |        
| Uninstalled | enterprise-4.2.7                  | OpenJPA engine support
deltaspike-jpa                                  | 1.3.0            |        
| Uninstalled | org.ops4j.pax.cdi-1.0.0.RC1       | Apache Deltaspike jpa
support
openjpa                                         | 3.0.0            |        
| Uninstalled | openjpa-3.0.0                     | Apache OpenJPA 3
persistence engine support
deltaspike-jpa                                  | 1.8.1            |        
| Uninstalled | org.ops4j.pax.cdi-1.1.1           | Apache Deltaspike jpa
support
jpa                                             | 2.7.2            |        
| Started     | aries-jpa-2.7.2                   | OSGi Persistence
Container


karaf@root()> feature:list | grep hibernate
cais-hibernate-cfg                              | 1.27.0.SNAPSHOT  |          | Started     | cais-core-persistence             |
hibernate                                       | 5.4.2.Final      |          | Started     | enterprise-4.2.7                  | Hibernate JPA engine support
hibernate-envers                                | 5.4.2.Final      |          | Uninstalled | enterprise-4.2.7                  | Feature for easily adding Envers support to hiber
hibernate-validator                             | 6.0.17.Final     |          | Uninstalled | hibernate-validator-osgi-features |
hibernate-validator-jsoup                       | 6.0.17.Final     |          | Uninstalled | hibernate-validator-osgi-features |
hibernate-validator-joda-time                   | 6.0.17.Final     |          | Uninstalled | hibernate-validator-osgi-features |
hibernate-validator-javax-money                 | 6.0.17.Final     |          | Uninstalled | hibernate-validator-osgi-features |
hibernate-validator-groovy                      | 6.0.17.Final     |          | Uninstalled | hibernate-validator-osgi-features |
hibernate-validator-paranamer                   | 6.0.17.Final     |          | Uninstalled | hibernate-validator-osgi-features |


Best regards
Andreas



--
Sent from: http://karaf.922171.n3.nabble.com/Karaf-User-f930749.html
Reply | Threaded
Open this post in threaded view
|

Re: Unable to use JPA Attribute converters in karaf

Erdbeerheld1990
Hello,

i fixed the issue by using field annotation on the entity instead on the
getter methods. Then i was able to correctly use my converter with the
@Convert annotation. I don't know why, but it worked.

Best regards,
Andy



--
Sent from: http://karaf.922171.n3.nabble.com/Karaf-User-f930749.html