@Converter
, autoApply = true
也不会应用。将@Convert
添加到字段本身时可以使用。
以下是 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}
}
使用 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
}
我正在使用JPA 2.1
(Hibernate 5.2.6.FINAL
)
编辑:
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>
答案 0 :(得分:3)
由于我已将注释添加到类@Converter
,因此在<class>com.example.hibernate.model.HeightConverter</class>
persistence.xml
就足够了
答案 1 :(得分:2)
我认为你必须在实体映射中提及转换器才能自动应用到工作中。
<?xml version="1.0"?>
<entity-mappings>
<converter class="com.example.hibernate.model.HeightConverter" auto-apply="true"/>
</entity-mappings>
答案 2 :(得分:1)
您需要确保@Converter注释的类是扫描的软件包的一部分。这为我们解决了这个问题。
public LocalContainerEntityManagerFactoryBean entityManager(DataSource dataSource) {
LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
em.setPackagesToScan("your.converter.package","your.entities.package");
...
在我们的案例中,出于某种原因,必须以编程方式进行em配置,但是也可以通过其他方式实现。
答案 3 :(得分:0)
当exclude-unlisted-classes为false时,您需要设置为类或在orm.xml中
persistence.xml
<class>com.example.hibernate.model.HeightConverter</class>
或
orm.xml
<?xml version="1.0"?>
<entity-mappings>
<converter class="com.example.hibernate.model.HeightConverter" auto-apply="true"/>
</entity-mappings>
persistence.xml
<mapping-file>META-INF/orm.xml</mapping-file>
答案 4 :(得分:0)
在我的情况下,@Converter(autoApply = true)被忽略了,因为枚举字段中有@Enumerated(EnumType.STRING)。