users@glassfish.java.net

How to inject Entity Manager to JSF Managed Bean

From: <glassfish_at_javadesktop.org>
Date: Sun, 23 Sep 2007 19:28:49 PDT

Hi All,
This is my first time writing in this forum.
I tried to search about injection Entity Manager to JSF Bean but couldn't find it so I am asking here...

I have JSF JSP Page like this :
<h:commandButton value="#{msg.login}" action="#{loginBean.validate}"></h:commandButton>

and my faces-config.xml :
<managed-bean>
<managed-bean-name>loginBean</managed-bean-name>
<managed-bean-class>com.login.LoginBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>

my managed bean : LoginBean.java

my entity class :
@Entity
@Table(name = "contacts")
public class ContactsEntity implements Serializable {
//getter and setter
...
}

my persistence.xml :
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="ContactsEntity">
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
<class>com.login.ContactsEntity</class>
<properties>
<property name="toplink.jdbc.user" value="root"/>
<property name="toplink.jdbc.password" value=""/>
<property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/Test"/>
<property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
</properties>
</persistence-unit>
</persistence>

and now I want to inject this entity class to my managed bean
i read from somewhere that i have to create one bean class with @Stateless annotation and make injection here,
i did something like this:

i have @Stateless bean class called : loginActionBean.java

@Stateless(name = "LoginPassword")
public class loginActionBean implements LoginPassword{
@PersistenceContext(unitName="ContactsEntity")
private EntityManager em;

public void loginPassword(String name, String password) {
List getName = em.createQuery("Select name from ContactsEntity where name=:name ").setParameter("name", name).getResultList();

and in my JSF managed bean, i put something like this :
public void validate() {
final Context context = new InitialContext();
LoginPassword lp = (LoginPassword)context.lookup("LoginPassword");
lp.loginPassword(name, password.convertedID);

whenever i tried to run this project, it returns error like : javax.name.NameException : name LoginPassword is not bound in the context for this line below :
LoginPassword lp = (LoginPassword)context.lookup("LoginPassword");

I am just wondering what i have done here is correct or i miss something?
If someone here has experience in creating EJB injection to JSF managed bean, could you please share yours with me?
I spend 2 days trying to solve this, could you please help me?

Thank you in advance for anyone who help me..

Regards,
Maria
[Message sent by forum member 'maria_r' (maria_r)]

http://forums.java.net/jive/thread.jspa?messageID=236699