2017-02-21 4 views
1

@Converter wird nicht angewendet, auch wenn autoApply = true hinzugefügt wird. Funktioniert, wenn @Convert dem Feld selbst hinzugefügt wird.@Converter (autoApply = true) funktioniert nicht

Hier ist ein Code für 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} 
} 

Klasse, wo Height

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 verwendet wird JPA 2.1 (Hibernate 5.2.6.FINAL)

EDIT bin mit:

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> 

Antwort

2

Ich glaube, Sie haben den Konverter in den Entity-Zuordnungen zu erwähnen Auto-Anwendung zu arbeiten.

<?xml version="1.0"?> 
<entity-mappings> 
    <converter class="com.example.hibernate.model.HeightConverter" auto-apply="true"/> 
</entity-mappings> 
+0

Wie vermeiden wir xml? Können wir Konverter automatisch scannen? –

+0

Sollte ohne die xml arbeiten ... aber ich würde es trotzdem versuchen – tom

+0

Hinzugefügt Sie snippets @ orm.xml und es funktioniert .. Aber ich versuche dies mit Annotation zu erreichen. Fehle ich etwas? –

2

Da ich bereits hinzugefügt Anmerkung zu Klasse @Converter es genug war, um hinzuzufügen <class>com.example.hibernate.model.HeightConverter</class> in persistence.xml

Verwandte Themen