jpa - Unable to lookup JNDI name -
hi i'm using netbeans+glassfish i'm trying run code: create db without table ( running code want create tables , persist object)
public static void main(string[] args) { smartphoneservice ss = new smartphoneservice(); smartphone smart = new smartphone(0, 0, null, null, null); ss.create(smart); } but got error :
unable 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 smartphone u 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(); } } { em.close(); emf.close(); } return false; }} i checked connection pool ping ( ping succeeded )
thanks ur help
you're mixing javase , javaee environment.
your datasource looks it's configured on glassfish (java ee environment). jndi name java:comp/env/jdbc/mysql available in java ee context.
your smartphoneservice being run in java se context (via public static void main() method. when try lookup of java:comp/env/jdbc/mysql, it's not going there because datasource exists in glassfish (java ee) environment.
you need perform jndi lookups same context resources registered in. suggestion make smartphoneservice code run on glassfish. there lots of ways drive -- ejbs, servlets, etc...
Comments
Post a Comment