3 java jpa eclipselink java-ee jpa-2.1
在JavaEE环境中,我使用EclipseLink的JPA 2.1实现,
我有一些实体包含enums.所以我为这些枚举创建了转换器.
汽车实体:
@Entity
public class Car implements Serializable {
private static final long serialVersionUID = 6L;
@Id
private String id;
@Convert (converter = CarColorConverter.class)
private CarColor color;
public enum CarColor {
Black,
Gray,
White,
Red
};
public Car () {
id = GenerateUUID.id ();
}
....
}
Run Code Online (Sandbox Code Playgroud)
CarColor转换器:
@Converter (converterClass = CarColorConverter.class, name = "CarColorConverter")
public class CarColorConverter implements AttributeConverter<CarColor, String> {
private static final String BLACK = "Black";
private static final String GRAY = "Gray";
private static final String WHITE = "White";
private static final String RED = "Red";
@Override
public String convertToDatabaseColumn (CarColor entityData) {
switch (entityData) {
case Black:
return BLACK;
case Gray:
return GRAY;
case White:
return WHITE;
case Red:
return RED;
default:
throw new IllegalArgumentException ("Unknown : " + entityData);
}
}
@Override
public CarColor convertToEntityAttribute (String dbData) {
switch (dbData) {
case BLACK:
return CarColor.Black;
case GRAY:
return CarColor.Gray;
case WHITE:
return CarColor.White;
case RED:
return CarColor.Red;
default:
throw new IllegalArgumentException ("Unknown : " + dbData);
}
}
}
Run Code Online (Sandbox Code Playgroud)
persistence.xml中
<?xml version="1.0" encoding="UTF-8"?>
<persistence
version="2.1"
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">
<persistence-unit name="xyz-restful-api" transaction-type="RESOURCE_LOCAL">
<!-- Converters -->
<!--<class>com.xyz.model.converters.CarColorConverter</class>-->
<!-- Entities / Model -->
<class>com.xtz.model.Car</class>
<properties>
...
</properties>
</persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)
当我在persistence.xml文件上注释转换器的声明,并尝试在DB中保留我的实体时,我收到此错误:" 请确保转换器类名称正确并且存在持久性单元定义. ".并且没有编译时异常,只有一个非常明确的警告:
类"com.xyz.model.converters.CarTypeConverter"已注释,但未在persistence.xml文件中列出
但是,当我取消注释 persistence.xml文件上的转换器声明,并尝试在DB中保留我的实体时,我收到此错误:" 请确保转换器类名称正确并且存在持久性单元定义. ".和编译时间异常:
类"com.xyz.model.converters.CarColorConverter"列在persistence.xml文件中,但未注释
我是以错误的方式宣布转换器吗?
谢谢.
tho*_*ork 10
尝试一下,确保包含正确的包.
保持不变
import javax.persistence.Convert;
@Entity
public class Car implements Serializable {
[...]
@Convert(converter = CarColorConverter.class)
private CarColor color;
[...]
}
Run Code Online (Sandbox Code Playgroud)
您只需要空注释
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
@Converter
public class CarColorConverter implements AttributeConverter<CarColor, String> {
[...]
}
Run Code Online (Sandbox Code Playgroud)
你也可以
要么
只要您需要手动声明实体(例如,当它在库中抵制时),您还需要声明所有其他实体/转换器类.
<?xml version="1.0" encoding="UTF-8"?>
<persistence
version="2.1"
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">
<persistence-unit name="xyz-restful-api" transaction-type="RESOURCE_LOCAL">
<!-- Converters -->
<class>com.xyz.model.converters.CarColorConverter</class>
<!-- Entities / Model -->
<class>com.xtz.model.Car</class>
[...]
</persistence-unit>
</persistence>
Run Code Online (Sandbox Code Playgroud)
小智 4
我的猜测是你有混合javax.persistence和org.eclipse.persistence.annotations包装。
使用包类,您可以在转换器类上javax.persistence使用空注释,并在指定转换器类的实体类上使用注释。ConverterConvert
| 归档时间: |
|
| 查看次数: |
10623 次 |
| 最近记录: |