Class "model.Address" is listed in the persistence.xml file but not mapped
I have created a JPA
project. In that Eclipse displays the following error on the entity class.
Class "model.Address" is listed in the
persistence.xml
file but not mapped
How am I supposed to map the entity class in persistance.xml
?
Here is the model.Address
entity:
package model;
import java.io.Serializable;
import javax.persistence.*;
@Entity
public class Address implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
private long id;
private String city;
private String country;
private String province;
private String postalCode;
private String street;
// Getters/setters omitted for brevity.
}
Here is the persistence.xml
:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence
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/p开发者_如何转开发ersistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0"
>
<provider>org.eclipse.persistance.example.jpa.20.employee.annotations</provider>
<persistence-unit name="employee" transaction-type="RESOURCE_LOCAL">
<class>model.Employee</class>
<class>model.Address</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@localhost:1521:orcl" />
<property name="javax.persistence.jdbc.user" value="scott" />
<property name="javax.persistence.jdbc.password" value="tiger" />
</properties>
</persistence-unit>
</persistence>
This is an Eclipse quirk. I recently had exactly this problem when I created a new JPA project with the JPA library configuration disabled, but didn't manually configure the JPA libraries before I created the entities by the Eclipse New JPA Entity wizard. After creating the entities I configured the JPA libraries in project's Build Path (just by adding target Java EE server runtime in Libraries), but the validation error still remains. I could solve it in at least one of following ways:
- Rightclick
persistence.xml
file, JPA Tools -> Synchronize Class List. - Or, rightclick project, Validate.
- Or, close/reopen project.
This is consistently reproducible. I was using Eclipse Indigo SR1. When I create the entities after configuring the JPA libraries, this validation error doesn't occur.
Right click on jpa project
then
I think you have the wrong JPA provider class. It has to be:
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
(the one you've set doesn't seem to be a class at all, let alone a provider class)
Make sure your class is annotated with @Entity (from javax.persistence.Entity)
I realise that's not the OP's problem, but I got caught by that and Google sent me here.
You need to create a persistence file and a ORM mapping file for JPA, refer the mapping file from persistence file.
<persistence-unit name="persistenceUnit"
transaction-type="RESOURCE_LOCAL">
<provider>org.datanucleus.api.jpa.PersistenceProviderImpl</provider>
<mapping-file>META-INF/hbase-orm.xml</mapping-file>
<class>com.xxx.logcollector.entity.DefaultLogableEntity</class>
<properties>
<property name="datanucleus.jpa.addClassTransformer" value="false" />
<property name="datanucleus.managedRuntime" value="false" />
....
Create ORM mapping file
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
version="1.0">
<entity class="com.xxx.cne.logcollector.entity.DefaultLogableEntity"
name="DefaultLogableEntity">
<table name="RAW_LOG_COLLECTION" />
<attributes>
<id name="clientHostIP">
<column name="ANALYTICS:CLIENT_IP" />
</id>
<basic name="requestDateTime">
<column name="ANALYTICS:REQUEST_DATETIME" />
</basic>
...
Create entity manager in spring
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="persistenceUnit"></property>
If eclipse is used , go to Window>Preference>Validation
and uncheck Suspend all validators
option .Clean the project and problem get solved.
Any of the above didnt work.
For anyone who is searching for the "Class xxxx is mapped, but is not included in any persistence unit" error in Eclipse or RAD:
- Right-click on the project and choose properties.
- Select JPA
- Select the radio button "Discover annotated classes automatically"
- OK and wait for the project to finish building.
These steps worked for me.
It should be written like:
@Entity
**@Table(name="Address",schema="ABCD")** `
...or it could be written like :
@Entity
public class Address {
}
i got similar error and when eclipse artifacts folder ".settings" is missed . After i generate eclipse artifacts using "maven-eclipse-plugin" , the error is gone
精彩评论