【发布时间】:2015-09-17 15:41:12
【问题描述】:
我有一个问题,可以在 JPA 转换中使用 java CDI 吗?
我正在做一些测试来研究,但我无法在我的转换器中注入对象:
我正在使用eclipseLink,请看我的代码,请分析我的代码哪里出错了?我怎样才能以最好的方式做到这一点?
基本上为了更好地理解,我将有一个代表我的用户登录的会话 Bean,这个会话 Bean 我有用户时区,我想在我的转换器中注入这个时区以将数据写入数据库中的 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();
}
}
注意:除了注入到转换器之外,所有其他功能都运行良好,我可以弹出 EntityManager 和其他类以及将数据持久化到数据库中
【问题讨论】:
标签: java jpa eclipselink converter cdi