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