Seam 2.2与jboss 5非ejb无法注入entityManager
预计到达时间(ETA)解释这应该做什么。 员工必须跟踪两周的活动。
每周 (tqiActivitySheetActionBean) 包含 7 天 (tqiDayActionBean)。
每天包含当天的任务(tqiTasks)
任务一次显示一周(因此员工可以在本周和上周之间切换)。
我所做的是使用枚举来迭代一周 - 创建 tqiActivitySheetActionBeans 的数据表并迭代嵌套数据表中的数据表。
员工需要能够在适当的位置(在数据表中)添加、删除和编辑每天的任务。我将 tqiTasks 包装在 tqiDayActionBean 中以对它们进行分组。
我在 Seam 网站上发布了这个,但我没有得到任何反馈,我已经尝试了我所知道的一切和一些我不知道的东西来让它工作,但到目前为止没有任何乐趣。 这里有使用 EJB 的 Jboss 5 和不使用 EJB 的 Jboss 4 的配置信息。我有一个使用 Jboss 5 和 Seam 2.2.0 的分解 WAR,并且没有 EJB。我无法将实体管理器注入到我的操作 POJO 中。 我尝试从 web.xml 中删除 persistence-unit-ref-name 引用,
<transaction:entity-transaction />
<transaction:ejb-transaction />
从 elements.xml 添加和删除标签(有或没有entity-manager="#{entityManager}")。当我使用其中任何一个时,该应用程序都不会加载。 使用默认值并一次对它们进行硬编码。我仍然无法让它发挥作用。 我目前正在使用此配置:
persistence.xml:
<persistence-unit name="myEmployee" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/myEmployeeDatasource</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
<property name="hibernate.hbm2ddl.auto" value="validate"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.default_schema" value="MY_PROD"/>
<!-- Only relevant if Seam is loading the persistence unit (Java SE bootstrap) -->
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
<!-- this binds emf to jndi under java namespace - but only for jboss prior to 5.0 - I shouldn't need this. -->
<property name="jboss.entity.manager.factory.jndi.name" value="java:/EntityManagerFactories/myEmployee"/>
</properties>
</persistence-unit>
Components.xml:
<persistence:managed-persistence-context name="entityManager"
persistence-unit-jndi-name="java:/EntityManagerFactories/myEmployee"
auto-create="true"/>
这可以正确部署并启动,但entityManager 仍然为空。
我在同一个包中有一堆 EntityHome 类,它们可以毫无问题地使用实体管理器。我看过的所有文档都表明我应该能够注入一个entityManager,我遵循了Seam文档页面、Seam in Action一书和Seam wiki for JBoss 5 (Seam wiki)
我在 ManagedPersistence 类中设置了断点 - 在其中看起来我有一个entityManager,直到我想使用它时,才向类和方法添加了@Transactional 注释。还是什么都没有。我可以使用 EM
(EntityManager)Component.getInstance("entityManager")
但随后我无法对底层实体Bean进行事务访问,最终会出现 hibernate LazyInitializationExceptions。 我已经使用 Seam 快一年了,但还没有让它发挥作用。到目前为止,使用 Component 方法已经有效,但是我们的设置一定存在根本性错误,导致我无法使注入工作。 我将不胜感激任何帮助。
用户类别的 ETA 代码。
实体类是TQITask。 TQIDayActionBean 类应该处理一组 TQITasks 的 CRUD 一天,TQIActivitySheetActionBean 处理 TQIDayActionBeans 一周。
实体bean:
@Entity
@Table(name = "TQI_TASK")
@Name("tqitask")
public class TqiTask implements java.io.Serializable {
static Logger log = Logger.getLogger(TqiTask.class);
private Integer sysid;
private TqiActivitySubtype tqiActivitySubtype;
private TqiActivityType tqiActivityType;
// ... more fields, getters and setters
dayActionBean(一天的任务):
@Name("tQIDayActionBean")
@Transactional(TransactionPropagationType.REQUIRED) //this is latest attempt
@Scope(ScopeType.CONVERSATION)
public class TQIDayActionBean implements Serializable {
static Logger log = Logger.getLogger(TQIDayActionBean.class);
@In(required=true)
EntityManager entityManager;
//this doesn't work either
@DataModel("tqiTasksForOneDay")
@Out(required = false)
private List<TqiTask> tqiTasksForOneDay;
// .... more stuff
// method that should get an entityManager
/**
* tasks are children of tqiActivitySheet, info we need is in tqiActivitySheetV, so use peopleSoftIdSelected and weekSelected.weekSysid
* to get the tqiActivitySheet sysid needed to load the right tasks
* TODO for now lets just get it straight from the db
*/
@SuppressWarnings("unchecked")
@Begin(flushMode=FlushModeType.MANUAL)
public void generateTasksForDayOfWeek() {
log.debug("date to search for: " + this.tqiTaskDate);
this.tqiTasksForOneDay = this.entityManager.createQuery("SELECT t from TqiTask t where t.workDate = ?1 AND t.activitySheetSysid = ?2 order by t.sysid")
.setParameter(1, this.tqiTaskDate).setParameter(2, this.activitySheetSysid).getResultList();
}
getter/setters etc ...
activitySheetActionBean(一周的dayActionBeans):
/**
* handle the activity sheet weekly code generation here
*/
@Name("tqiActivitySheetActionBean")
public class TQIActivitySheetActionBean implements Serializable{
static Logger log = Logger.getLogger(TQIActivitySheetActionBean.class);
// tried it here first
// @In
// EntityManager entityManager;
@Out(value="tqiDayActionBeansForTheWeek", required=false)
List<TQIDayActionBean> tqiDayActionBeansForTheWeek;
// List<DaysOfTheWeek> daysOfTheWeekEnum;
private BigDecimal currentlySelectedWeekActivitySheetSysid;
@PostConstruct
public void init(){
log.debug("calling init");
this.tqiDayActionBeansForTheWeek = new ArrayList<TQIDayActionBean>();
this.updateDataWhenWeekChanges();
}
getters/setters more stuff ....
ETA an explanation of what this is supposed to do.
An employee has to keep track of 2 weeks worth of activities.
Each week(tqiActivitySheetActionBean) contains 7 days (tqiDayActionBean).
Each day contains tasks for that day(tqiTasks)
Tasks are displayed one week at a time(so the employee can switch between this week and last week).
What I did was use an enum to iterate over the week - creates a dataTable of tqiActivitySheetActionBeans and iterate over those in a nested dataTable.
The employee needs to be able to add, delete and edit tasks for each day in place(in the dataTable). I wrapped the tqiTasks in the tqiDayActionBean to group them.
I posted this over at the Seam website, but I'm not getting any bites, I've tried everything I know and some stuff I don't to get this working, but so far no joy.
There's configuration information for Jboss 5 using EJB's, Jboss 4 without. I have an exploded WAR using Jboss 5 and Seam 2.2.0, and no EJB's. I cannot inject an entityManager into my action POJOs.
I tried removing the persistence-unit-ref-name reference from web.xml, added and removed
<transaction:entity-transaction />
<transaction:ejb-transaction />
tags from components.xml (with and without entity-manager="#{entityManager}") . The app won't load when I use either of these.
Used the default values and hardcoded them one at a time. I still can't get this to work.
I'm currently using this configuration:
persistence.xml:
<persistence-unit name="myEmployee" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/myEmployeeDatasource</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
<property name="hibernate.hbm2ddl.auto" value="validate"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.default_schema" value="MY_PROD"/>
<!-- Only relevant if Seam is loading the persistence unit (Java SE bootstrap) -->
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
<!-- this binds emf to jndi under java namespace - but only for jboss prior to 5.0 - I shouldn't need this. -->
<property name="jboss.entity.manager.factory.jndi.name" value="java:/EntityManagerFactories/myEmployee"/>
</properties>
</persistence-unit>
components.xml:
<persistence:managed-persistence-context name="entityManager"
persistence-unit-jndi-name="java:/EntityManagerFactories/myEmployee"
auto-create="true"/>
This deploys and starts correctly, but entityManager is still null.
I have a bunch of EntityHome classes in the same package, these can use an entityManager with no problem. All the docs I've looked at state that I should be able to inject an entityManager, I've followed the configuration info from the Seam docs page, the Seam in Action book and the Seam wiki for JBoss 5 (Seam wiki)
I've set breakpoints in the ManagedPersistence class - where it looks like I have an entityManager right up to the time I want to use it, added @Transactional annotations to the class and method. Still nothing. I can get an EM using
(EntityManager)Component.getInstance("entityManager")
But then I have no transactional access to the underlying entityBean and end up with hibernate LazyInitializationExceptions.
I've been using Seam for almost a year now and I have yet to get this to work. Up til now using the Component method has worked, but There must be something fundamentally wrong with our setup that I cannot get injection to work.
I'd appreciate any help.
ETA code for user classes.
The entity class is TQITask. TQIDayActionBean class is supposed to handle the CRUD for a group of TQITasks for one day, TQIActivitySheetActionBean handles the TQIDayActionBeans for one week.
entity bean:
@Entity
@Table(name = "TQI_TASK")
@Name("tqitask")
public class TqiTask implements java.io.Serializable {
static Logger log = Logger.getLogger(TqiTask.class);
private Integer sysid;
private TqiActivitySubtype tqiActivitySubtype;
private TqiActivityType tqiActivityType;
// ... more fields, getters and setters
dayActionBean(a days tasks):
@Name("tQIDayActionBean")
@Transactional(TransactionPropagationType.REQUIRED) //this is latest attempt
@Scope(ScopeType.CONVERSATION)
public class TQIDayActionBean implements Serializable {
static Logger log = Logger.getLogger(TQIDayActionBean.class);
@In(required=true)
EntityManager entityManager;
//this doesn't work either
@DataModel("tqiTasksForOneDay")
@Out(required = false)
private List<TqiTask> tqiTasksForOneDay;
// .... more stuff
// method that should get an entityManager
/**
* tasks are children of tqiActivitySheet, info we need is in tqiActivitySheetV, so use peopleSoftIdSelected and weekSelected.weekSysid
* to get the tqiActivitySheet sysid needed to load the right tasks
* TODO for now lets just get it straight from the db
*/
@SuppressWarnings("unchecked")
@Begin(flushMode=FlushModeType.MANUAL)
public void generateTasksForDayOfWeek() {
log.debug("date to search for: " + this.tqiTaskDate);
this.tqiTasksForOneDay = this.entityManager.createQuery("SELECT t from TqiTask t where t.workDate = ?1 AND t.activitySheetSysid = ?2 order by t.sysid")
.setParameter(1, this.tqiTaskDate).setParameter(2, this.activitySheetSysid).getResultList();
}
getter/setters etc ...
activitySheetActionBean(a week's dayActionBeans):
/**
* handle the activity sheet weekly code generation here
*/
@Name("tqiActivitySheetActionBean")
public class TQIActivitySheetActionBean implements Serializable{
static Logger log = Logger.getLogger(TQIActivitySheetActionBean.class);
// tried it here first
// @In
// EntityManager entityManager;
@Out(value="tqiDayActionBeansForTheWeek", required=false)
List<TQIDayActionBean> tqiDayActionBeansForTheWeek;
// List<DaysOfTheWeek> daysOfTheWeekEnum;
private BigDecimal currentlySelectedWeekActivitySheetSysid;
@PostConstruct
public void init(){
log.debug("calling init");
this.tqiDayActionBeansForTheWeek = new ArrayList<TQIDayActionBean>();
this.updateDataWhenWeekChanges();
}
getters/setters more stuff ....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
TQIDayActionBean
类的名为tQIDayActionBean
的 bean 无法使用new
实例化,并且绝对无法拥有List实例。
在应用程序中。该 bean 与任何其他 Seam 托管 bean 一样,只能使用
@In
注入或使用Component.getInstance()
实例化。在第二种情况下(即使用Component.getInstance()
),您应该明白,只要您处于上下文边界内,Seam 将始终返回相同的实例(在这种情况下,直到对话结束) 。我建议您重新编写代码,以便分离
TQIDay
标准类(不带@Name
和@Inject
)来对您需要的数据进行建模从TQIActionBean
管理和收集多个实例,您可以在其中注入EntityManager
。The bean named
tQIDayActionBean
of classTQIDayActionBean
cannot be instantiated withnew
, and there is absolute no way to have aList<TQIDayActionBean>
in the application.This bean, like any other Seam managed bean, can only be injected with
@In
or instantiated withComponent.getInstance()
. In the second case (that is withComponent.getInstance()
) you should understand that Seam will always return the same instance as far as you stay in the context boundaries (in this case until the conversation ends).I suggest that you rework your code in order to separate a
TQIDay
standard class (without@Name
and@Inject
) to model the data you need to manage and collect in multiple instances, from anTQIActionBean
where you can have yourEntityManager
injected.