tags:

views:

43

answers:

2

hi; I am using seam with tomcate and icefaces the problem is when I inject entity manager in component bean it works well but if put it in generic DAO it returns null my code is like that:

this the bean

@Scope(ScopeType.PAGE)
@Name("TestBean")
public class TestBean {
 public void test(ActionEvent actionEvent) {
  Roles entity = new Roles();
  entity.setName("cons");
  RolesDao dao = new RolesDao();
  dao.emPrisit(entity);

 }
}

DAO

public class RolesDao {
 @In
 EntityManager em;

 public void emPrisit(Roles entity) {
  em.persist(entity);
 }

}

Component.xml

<persistence:entity-manager-factory name="bookingDatabase"/>

<persistence:managed-persistence-context name="em"
                           auto-create="true" 
                entity-manager-factory="#{bookingDatabase}"/>   

persistence.xml

<persistence-unit name="bookingDatabase"
  transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.ejb.HibernatePersistence </provider>
  <jta-data-source>java:comp/env/AP</jta-data-source>
  <properties>
   <property name="transaction.flush_before_completion" value="true" />
   <property name="transaction.factory_class"
    value="org.hibernate.transaction.JDBCTransactionFactory" />

   <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
   <!--
    <property name="hibernate.transaction.manager_lookup_class"
    value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
   -->

  </properties>
 </persistence-unit>

if I inject entitymanger in bean not dao it runs well the second problem when I add this annotation before entitymanger

@In
 @PersistenceContext(type = PersistenceContextType.EXTENDED)
 EntityManager em;

it give this exception

caused by: java.lang.IllegalArgumentException: @PersistenceContext may only be used on session bean or message driven bean components: TestBean
 at org.jboss.seam.Component.checkPersistenceContextForComponentType(Component.java:901)
 at org.jboss.seam.Component.scanField(Component.java:877)
 at org.jboss.seam.Component.initMembers(Component.java:557)
 at org.jboss.seam.Component.<init>(Component.java:244)
 at org.jboss.seam.Component.<init>(Component.java:205)
 at org.jboss.seam.init.Initialization.addComponent(Initialization.java:1186)
 ... 13 more
+1  A: 

Injection only occurs in Beans, so your DAO should have a @Name("something"), otherwise Seam doesn't knows what to do with your class.

@Name("RolesDao")
public class RolesDao {
   @In
   EntityManager em;

   public void emPrisit(Roles entity) {
       em.persist(entity);
   }
}

Then to use this class you should either do:

@In(value="RolesDao") // value="..." is optional
private RolesDao rolesDao;

or

org.jboss.seam.Component.getInstance(RolesDao.class)

Sorry if the sample code has some errors, didn't had eclipse to try it.

Hope this helps

tiago
thanks that really help me but what about the second issue
aatsy
Sorry, didn't noticed the second part. I've never had that issue but from what I understand from the excpetion, to use the extended context you will need to make your bean a session bean "@Scope(ScopeType.Session)". I've never needed it so I can't say much about it, sorry.
tiago
+1  A: 

You don't need both @In and @PersistenceContext on your EntityManager. It is enough with one of them.

Shervin