@PersistenceContext in a SFSB created by a factory
newlukai Aug 7, 2007 6:14 AMHi there,
is it possible to have a PersistenceContext in a bean (don't really know which type it is, sorry, I'm a noob somedays :D) which is created by a factory?
The thing I want to do is to imlpement the possibility to switch the implementation of an interface. This works. In one of these implementations I want to obtain the PersistenceContext and that doesn't work.
But let me show you some code:
@Name("upDownloadFactory") @Scope(ScopeType.APPLICATION) @Startup public class UpDownloadFactory implements Serializable { private String className; @Out(required=false) private UpDownload upDownload; @Factory(value="upDownload", scope=ScopeType.SESSION, autoCreate=true) public UpDownload getUpDownload() { if(upDownload == null) { try { if(className == null || className.equals("")) { className = "com.idsscheer.ares.sessions.UpDownloadDatabase"; } upDownload = (UpDownload) Class.forName(className).newInstance(); ... and so on ... } return upDownload; } public void setClassName(String className) { this.className = className; } }
This is the factory I wrote. As you can see the implementation has to be set in components.xml and sets the className property. All those parameters I set in the @Factory annotation shall setup the "upDownload" as SFSB.
Here is one implementation of the managed bean:
@Scope(ScopeType.SESSION) @Stateful public class UpDownloadDatabase implements UpDownload, Serializable { @PersistenceContext(unitName = "aresDatabase") private transient EntityManager em; public boolean upload(UpDownloadFileinfo fileInfo) { ... do the upload with em ... } public List<UpDownloadFileinfo> getAttachedFiles(long testactionID) { ... show a list of attached files ... } @Remove @Destroy public void destroy() { } }
In this implementation the EntityManager is needed but on invokation of this bean the console tells me that
Caused by: java.lang.IllegalArgumentException: @PersistenceContext may only be used on session bean or message driven bean components: upDownload at org.jboss.seam.Component.initMembers(Component.java:663) at org.jboss.seam.Component.<init>(Component.java:253) at org.jboss.seam.Component.<init>(Component.java:203) at org.jboss.seam.init.Initialization.addComponent(Initialization.java:851) ... 93 more
Has anybody an idea how to implement such a factory?
Thanks in advance
Newlukai