@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>
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.
On my case the @Converter(autoApply = true) was ignored because there was @Enumerated(EnumType.STRING) on the enum field.
Since I already added annotation to Class @Converter it was enough to add <class>com.example.hibernate.model.HeightConverter</class> in persistence.xml
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With