您好我试图将JPA与eclipse链接一起使用,但它给了我以下错误: 我使用maven,context.xml文件在web模块中,persistaence.xml在access / db模块中。
Failed to connect to MyModalTestApp: Exception [EclipseLink-30005] (Eclipse
Persistence Services - 2.6.4.v20160829-44060b6):
org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for
persistence archives with ClassLoader: ParallelWebappClassLoader
context: modaltestapp-api
delegate: false
----------> Parent Classloader:
java.net.URLClassLoader@3fee733d
Internal Exception: javax.persistence.PersistenceException: Exception
[EclipseLink-28018] (Eclipse Persistence Services - 2.6.4.v20160829-
44060b6): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [MyModalTestApp]
failed.
Internal Exception: Exception [EclipseLink-7161] (Eclipse Persistence
Services - 2.6.4.v20160829-44060b6):
org.eclipse.persistence.exceptions.ValidationException
Exception Description: Entity class [class
com.modaltestapp.entity.RegistrationEntity] has no primary key specified.
It should define either an @Id, @EmbeddedId or an @IdClass. If you have
defined PK using any of these annotations then make sure that you do not
have mixed access-type (both fields and properties annotated) in your
entity class hierarchy.
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="MyModalTestApp" transaction-type="RESOURCE_LOCAL">
<non-jta-data-source>java:comp/env/jdbc/MODALTESTAPP_DATASOURCE</non-jta-data-source>
<class>com.modaltestapp.entity.RegistrationEntity</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<property name="eclipselink.ddl-generation" value="none"/>
<property name="eclipselink.weaving" value="static" />
<property name="eclipselink.target-database" value="Auto"/>
<property name="eclipselink.target-server" value="None"/>
<property name="eclipselink.jdbc.batch-writing" value="JDBC"/>
<property name="eclipselink.logging.level" value="FINEST"/>
<property name="eclipselink.logging.timestamp" value="true"/>
<property name="eclipselink.logging.session" value="false"/>
<property name="eclipselink.logging.thread" value="true"/>
<property name="eclipselink.logging.exceptions" value="true"/>
<!-- 0 is a valid ID
<property name="eclipselink.id-validation" value="NULL"/> -->
<!-- We need the following, else EclipseLink cannot properly translate column names in Native SQL queries. -->
<property name="eclipselink.jdbc.uppercase-columns" value="true"/>
</properties>
</persistence-unit>
</persistence>
pojo是:
package com.modaltestapp.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "REGISTRATION")
public class RegistrationEntity implements Serializable{
private static final long serialVersionUID = 1L;
@Column(name = "FIRSTNAME")
private String firstName;
@Column(name = "LASTNAME")
private String lastName;
@Column(name = "LASTNAME")
private String email;
@Column(name = "MESSAGE")
private String message;
@Column(name = "EDUCATION")
private String education;
@Column(name = "HOBBIES")
private String hobbies;
@Column(name = "GENDER")
private String gender;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getEducation() {
return education;
}
public void setEducation(String education) {
this.education = education;
}
public String getHobbies() {
return hobbies;
}
public void setHobbies(String hobbies) {
this.hobbies = hobbies;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
context.xml是:
<Context>
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<!-- Uncomment this to disable session persistence across Tomcat restarts -->
<!--
<Manager pathname="" />
-->
<!-- Uncomment this to enable Comet connection tacking (provides events
on session expiration as well as webapp lifecycle) -->
<!--
<Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
-->
<Resource name="jdbc/MODALTESTAPP_DATASOURCE" auth="Container" type="javax.sql.DataSource"
maxTotal="8" maxIdle="2"
username="root" password="unnati@123" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/MODALTESTAPP" validationQuery="select 1"/>
</Context>
我的实现代码是:
try {
EntityManager em = Persistence.createEntityManagerFactory("MyModalTestApp").createEntityManager();
System.out.println("in database portion1");
/* Persist entity */
em.getTransaction().begin();
System.out.println("in database portion2");
em.persist(REG);
System.out.println("in database portion3");
em.getTransaction().commit();
System.out.println("yes transcaction done");
} catch (Exception e) {
System.out.println("Failed to connect to MyModalTestApp: " + e);
// e.printStackTrace();
}