我正在尝试在 Glassfish Server 上运行 Web 应用程序。当我的 DAO 像这样编码时,我的测试页面上没有问题。我可以获取所有客户并在数据表上查看他们。
@Stateful
public class CustomersDao {
static EntityManagerFactory emf;
static EntityManager em;
public List<Customers> getAllCustomers() {
emf = Persistence.createEntityManagerFactory("Persistence");
em = emf.createEntityManager();
TypedQuery<Customers> query = em.createQuery("SELECT c FROM Customers c", Customers.class);
List<Customers> allCustomers = query.getResultList();
System.out.println(allCustomers);
System.out.println(allCustomers.get(0).getCountry());
return allCustomers;
}
}
但是,当我将课程更改为:
@Stateful
public class CustomersDao {
@PersistenceContext(unitName = "Persistence")
EntityManager em;
public List<Customers> getAllCustomers() {
TypedQuery<Customers> query = em.createQuery("SELECT c FROM Customers c", Customers.class);
List<Customers> allCustomers = query.getResultList();
System.out.println(allCustomers);
System.out.println(allCustomers.get(0).getCountry());
return allCustomers;
}
}
我得到:javax.persistence.PersistenceException: [PersistenceUnit: Persistence] Unable to build EntityManagerFactory
|准备应用程序时出现异常:[PersistenceUnit: Persistence] Unable to build EntityManagerFactory org.hibernate.HibernateException: Connection cannot be null when 'hibernate.dialect' not set
这是我的persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="Persistence">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.tugay.maythirty.model.Customers</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/sampleapplication"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="passsssss"/>
</properties>
</persistence-unit>
</persistence>
有什么帮助吗?