使用Eclipse的Postgres的基本独立JPA示例
我一直在尝试获得一个非常基本的独立JPA示例,以便使用EclipseIDE与Postgres一起工作 我定义了一个persistence.xml,如下所示使用Eclipse的Postgres的基本独立JPA示例,eclipse,postgresql,jpa,Eclipse,Postgresql,Jpa,我一直在尝试获得一个非常基本的独立JPA示例,以便使用EclipseIDE与Postgres一起工作 我定义了一个persistence.xml,如下所示 <?xml version="1.0" encoding="UTF-8"?> <persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-in
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="sample" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>package.class</class>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.logging.level" value="INFO" />
<property name="eclipselink.jdbc.driver" value="org.postgresql.Driver" />
<property name="eclipselink.jdbc.url"
value="jdbc:postgresql://localhost:5432/sample" />
<property name="eclipselink.jdbc.user" value="scott" />
<property name="eclipselink.jdbc.password" value="tiger" />
</properties>
</persistence-unit>
</persistence>
我在线程“main”javax.persistence.PersistenceException中得到以下异常-异常:没有名为sample的EntityManager的持久性提供程序
似乎没有拾取我的persistence.xml文件。JPA框架在哪里加载persistence.xml文件 这是一个愚蠢的错误。该文件需要名为persistence.xml,而不是persistence.xml 这是一个愚蠢的错误。该文件需要名为persistence.xml,而不是persistence.xml 这是一个愚蠢的错误。该文件需要名为persistence.xml,而不是persistence.xml。我建议您将此作为您自己问题的答案发布。这是一个愚蠢的错误。该文件需要名为persistence.xml,而不是persistence.xml。我建议您将此作为您自己问题的答案发布。更容易看到解决方案
EntityManagerFactory entityManagerFactory
= Persistence.createEntityManagerFactory("sample");
EntityManager em = entityManagerFactory.createEntityManager();
EntityTransaction tx = em.getTransaction();
try {
User user = new User();
user.setEnabled(false);
user.setEmailId("test@test.com");
tx.begin();
em.persist(user);
tx.commit();
} catch (Exception e) {
em.getTransaction().rollback();
} finally {
em.close();
}