0

hi i'm using netbeans+glassfish i'm trying to run this code: i only create DB without any table ( by running this code i want to create tables and persist my object)

public static void main(String[] args) {
    SmartphoneService ss = new SmartphoneService();
    Smartphone smart = new Smartphone(0, 0, null, null, null);
    ss.create(smart);
}

but i got this error :

Unable to lookup JNDI name

my persistence.xml:

 <persistence-unit name="manager1" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:comp/env/jdbc/mysql</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
  <property name="javax.persistence.schema-generation.database.action" value="create"/>
</properties>

my class smartphoneservice:

@Stateless
public class SmartphoneService implements IDao<Smartphone>  {

private static final String JPQL_SELECT_PAR_ID = "SELECT u FROM Smartphone u WHERE u.idSmartphone=:id";

private EntityManagerFactory emf;

private EntityManager em;

public SmartphoneService() {
    emf = Persistence.createEntityManagerFactory("manager1");
    em = emf.createEntityManager();
}
public boolean create( Smartphone smart) {
    try {
        em.getTransaction().begin();
        em.persist(smart);
        em.getTransaction().commit();
        return true;

    } catch (Exception e) {
        if (em.getTransaction() != null) {
            em.getTransaction().rollback();
        }
    } finally {
        em.close();
        emf.close();
    }
    return false;
}}

i checked my connection pool ping ( Ping Succeeded )

thanks for ur help

FuSsA
  • 4,223
  • 7
  • 39
  • 60

1 Answers1

1

You're mixing up a JavaSE and with a JavaEE environment.

Your datasource looks like it's configured on Glassfish (Java EE environment). So the JNDI name java:comp/env/jdbc/mysql will only be available in a Java EE context.

Your SmartphoneService is being run in a Java SE context (via a public static void main() method. When you try to do a lookup of java:comp/env/jdbc/mysql, it's not going to be there because the DataSource only exists in your Glassfish (Java EE) environment.

You will need to perform JNDI lookups from the same context that the resources were registered in. My suggestion would be to make your SmartphoneService code run on Glassfish. There are lots of ways to drive that -- EJBs, Servlets, etc...

Andy Guibert
  • 41,446
  • 8
  • 38
  • 61
  • how can i fix this problem ? – FuSsA Jan 05 '16 at 23:07
  • i updated my question by creating a servlet..but still got the error :/ – FuSsA Jan 06 '16 at 01:03
  • 1
    now the exception says there was an issue injectin the EJB using the jndi name `java:comp/env/com.fussa.test.NewServlet/ss`. I think you should open a separate question for this because you original question (*why can't I lookup a JavaEE resource from JavaSE*) has been answered, and editing your question makes my original answer invalid. – Andy Guibert Jan 06 '16 at 03:52