0
嗨,我是使用NetBeans + GlassFish的 我试图运行此代码: 我不作任何表创建DB(通过运行此代码我想创建表和坚持我的对象)无法查找JNDI名称
public static void main(String[] args) {
SmartphoneService ss = new SmartphoneService();
Smartphone smart = new Smartphone(0, 0, null, null, null);
ss.create(smart);
}
,但我得到这个错误:
Unable to lookup JNDI name
我的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>
我的课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;
}}
我检查我的连接池平(平成功)
感谢乌拉圭回合的帮助
我该如何解决这个问题? – FuSsA
用建议的修正更新了答案 –
我更新了我的问题,通过创建一个servlet ..但仍然有错误:/ – FuSsA