我正在将应用程序从GlassFish 3.1迁移到JBoss 6.1。
这段代码与GlassFish完美配合,但在JBoss中引发了一个丑陋的错误:
自定义注释:
@Named
@ViewAccessScoped
@Stereotype
@Target( { ElementType.TYPE, ElementType.METHOD, ElementType.FIELD } )
@Retention(RetentionPolicy.RUNTIME)
public @interface Model {
}
豆:
@Model
public class MyBean extends BaseBean {
@Inject
UserService userService
public void save() {
startTransaction();
studioService.persist(studio);
commitTransaction();
}
}
基础豆:
public class BaseBean implements Serializable {
@Resource
protected UserTransaction transaction;
@PersistenceContext(unitName = "fits_PU")
protected EntityManager entityManager;
[...]
protected void startTransaction() {
try {
transaction.begin();
entityManager.joinTransaction();
} catch [a few exceptions]
}
protected void commitTransaction() {
try {
transaction.commit();
} catch [an awful lot of exceptions]
}
}
我得到以下异常:
Caused by: java.lang.IllegalArgumentException: Can not set javax.transaction.UserTransaction field my.company.project.BaseBean.transaction to org.jboss.tm.usertx.client.ServerVMClientUserTransaction
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164) [:1.7.0_11]
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168) [:1.7.0_11]
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81) [:1.7.0_11]
at java.lang.reflect.Field.set(Field.java:680) [:1.7.0_11]
整个下午我一直在谷歌搜索,但是找不到任何提示。有什么想法吗?
答案 0 :(得分:0)
因此。我环顾四周,尝试并错误地看到我错了。
如果我正在使用EJB,我不需要管理任何UserTransaction
,因为事务是由容器管理的。因此,
@Named
服务更改为@Stateless
EJB @EJB
注释而不是@Inject
希望它会帮助别人。