Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@Converter(autoApply = true) not working

@Converter doesn't get applied even when autoApply = true is added. Works when @Convert is added to field itself.

Here's a code for Converter

package com.example.hibernate.model;

@Converter(autoApply = true)
public class HeightConverter implements AttributeConverter<Height, Integer> {
    public Integer convertToDatabaseColumn(Height height) {//convert}
    public Height convertToEntityAttribute(Integer dbData) {//convert}
}

Class where Height is used

package com.example.hibernate.model;

@Entity
@Table(name = "student")
public class Student implements Serializable {
    @Id
    @GeneratedValue(generator = "MY_S")
    private int id;

    // works if @Convert is applied
    // @Convert( converter = HeightConverter.class, disableConversion = false )

    @Column(name = "height_in_cm")
    private Height height;

    //getter setter

}

I'm using JPA 2.1 (Hibernate 5.2.6.FINAL)

EDIT:

persistence.xml

<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
             http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
             version="2.1">

    <persistence-unit name="persistence" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

        <mapping-file>META-INF/orm.xml</mapping-file>
        <class>com.example.hibernate.model.Student</class>
        <exclude-unlisted-classes>false</exclude-unlisted-classes>

        <properties>
            <property name="packagesToScan" value="com.example.hibernate.model" />
            <property name="hibernate.archive.autodetection" value="class" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/test_db1?useSSL=false" />
            <property name="hibernate.connection.username" value="root" />
            <property name="hibernate.connection.password" value="password" />
            <property name="hibernate.show_sql" value="true"/>
            <property name="hibernate.format_sql" value="true"/>
            <property name="hibernate.flushMode" value="FLUSH_AUTO" />
            <property name="hibernate.hbm2ddl.auto" value="create-drop" />
        </properties>

    </persistence-unit>
</persistence>
like image 624
A0__oN Avatar asked Feb 21 '17 17:02

A0__oN


4 Answers

You need to make sure the @Converter annotated class is part of the scanned packages. That fixed the issue for us.

 public LocalContainerEntityManagerFactoryBean entityManager(DataSource dataSource) {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setPackagesToScan("your.converter.package","your.entities.package");
    ...

In our case em configuration has to be made programmatically for reasons, but it can be achieved in other ways too.

like image 51
Ruben Daddario Avatar answered Oct 17 '22 12:10

Ruben Daddario


On my case the @Converter(autoApply = true) was ignored because there was @Enumerated(EnumType.STRING) on the enum field.

like image 42
Donnie Avatar answered Oct 17 '22 12:10

Donnie


Since I already added annotation to Class @Converter it was enough to add <class>com.example.hibernate.model.HeightConverter</class> in persistence.xml

like image 3
A0__oN Avatar answered Oct 17 '22 12:10

A0__oN


I think you have to mention the Converter in the entity-mappings for auto-apply to work.

<?xml version="1.0"?>
<entity-mappings>
    <converter class="com.example.hibernate.model.HeightConverter" auto-apply="true"/>
</entity-mappings>
like image 2
tom Avatar answered Oct 17 '22 11:10

tom