我有一个问题,是否可以在 JPA 转换中使用 java CDI?
我正在做一些测试来研究,但无法将对象注入(inject)我的转换器中:
我正在使用eclipseLink,请查看我的代码,请分析我的代码哪里出错了?我怎样才能以最好的方式做到这一点?
基本上,为了更好地理解,我将有一个代表我登录的用户的 session Bean,此 session Bean 我有用户时区,我想在我的转换器中注入(inject)此时区以将数据写入数据库中的 UTC
我的代码:
JPA 转换器: org.eclipse.persistence.mappings.converters.Converter
package joda;
import inject.qualifier.UserTimeZoneQualifier;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import org.eclipse.persistence.mappings.DatabaseMapping;
import org.eclipse.persistence.mappings.converters.Converter;
import org.eclipse.persistence.sessions.Session;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import enumerator.UserType;
import security.UserSession;
@RequestScoped
public class JodaDateTimeUTCConverter implements Converter {
private static final long serialVersionUID = 1L;
// JUST TEST IT'S WAS INJECT AND REMOVE
private UserSession userSession = new UserSession("America/Mexico_City", UserType.HIGH_HISK);
@Inject
@UserTimeZoneQualifier
String userTimeZone;
//TODO FOR TEST
DateTimeFormatter dtf = DateTimeFormat.forPattern("dd/MM/yyyy HH:mm:ss:SSS - z - ZZZZZZZZZZZZZZZZZZ");
SimpleDateFormat dt = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss:SSS - z - ZZZZZZZZZZZZZZZZZZ");
@Override
public Object convertDataValueToObjectValue(Object dataValue, Session session) {
// TODO REMOVE
DateTimeZone timeZone = DateTimeZone.forID(userSession.getTimeZoneLocale());
System.out.println("BEFORE OF CONVERTION : " + dt.format(dataValue));
System.out.println("AFTER OF CONVERTION : " + dtf.print(new DateTime((Timestamp) dataValue).withZone(timeZone)));
System.out.println("userTimeZone INJECT" + userTimeZone);
return dataValue instanceof Date ? new DateTime((Timestamp) dataValue).withZone(timeZone) : null;
}
@Override
public Object convertObjectValueToDataValue(Object objectValue, Session session) {
System.out.println("GO TO DB(DATAVALUE)");
System.out.println("AFTER OF CONVERTION : " + dtf.print(((DateTime) objectValue).withZone(DateTimeZone.UTC)));
return objectValue instanceof DateTime?((DateTime) objectValue).withZone(DateTimeZone.UTC).toLocalDateTime().toDate() : null;
}
@Override
public void initialize(DatabaseMapping mapping, Session session) {
}
@Override
public boolean isMutable() {
return false;
}
public String getUserTimeZone() {
return userTimeZone;
}
public void setUserTimeZone(String userTimeZone) {
this.userTimeZone = userTimeZone;
}
}
我的@UserTimeZoneQualifier:
package inject.qualifier;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target({FIELD, METHOD, TYPE, PARAMETER})
public @interface UserTimeZoneQualifier {
}
和我的 UserSessionProduce:
package inject;
import inject.qualifier.UserTimeZoneQualifier;
import javax.annotation.PostConstruct;
import javax.enterprise.inject.Produces;
import enumerator.UserType;
import security.UserSession;
public class UserSessionProduce {
private UserSession userSession;
@PostConstruct
public void init(){
this.userSession = new UserSession("America/Mexico_City", UserType.ADMINISTRATOR);
}
@Produces
public UserSession getUserSessionInstance(){
return this.userSession;
}
@Produces
@UserTimeZoneQualifier
public String getUserSessionTimeZone(){
return this.userSession.getTimeZoneLocale();
}
@Produces
public UserType getUserType(){
return this.userSession.getUserType();
}
}
注意:除了注入(inject)转换器之外,所有其他功能都运行良好,我可以弹出 EntityManager 和其他类以及将数据保留在数据库中
最佳答案
不幸的是,你不能。该规范要求 EntityListener
中支持 CDI 注入(inject)实现。它没有应用于转换器。
如果您想访问注入(inject)点,可以使用 CDI.current()
访问 CDI<Object>
实例。使用它就像使用 Instance<Object>
一样- 你可以做类似 .select(qualifier).select(clazz).get()
的事情检索 bean 实例。
如果需要使用限定符,则首先需要一个文字。
public class UserTimeZoneQualifierLiteral extends AnnotationLiteral<UserTimeZoneQualifier> implements UserTimeZoneQualifier {
}
然后实例化
UserTimeZoneQualifier qualifier = new UserTimeZoneQualifier(); // or use a singleton here.
关于java - JPA 转换器 (EclipseLink) 中的 CDI 注入(inject)类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32634574/