Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jpa error uses a non-entity [class ch.printsoft.mailhouse.usermgr.entity.Department] as target entity in the relationship attribute

I try to persist my Department and Mandator classes to hsqhldb but it gives this error.

Exception Description: [class ch.printsoft.mailhouse.usermgr.entity.Mandator] uses a non-entity [class ch.printsoft.mailhouse.usermgr.entity.Department] as target entity in the relationship attribute [field departments].
at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:126)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:115)
at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)

These are the classes that I try to persist to my database. I really don't know what the problem is.

@Entity
public class Mandator {
  @Id
  @GeneratedValue
  private Integer id;
  private String mandatorId;
  @OneToMany(mappedBy = "mandator")
  private List<MandatorUser> mandatorUsers;
  @OneToMany(mappedBy = "mandator")
  private List<SLAFamilyGroup> slaFamilyGroups;
  @OneToMany(mappedBy = "mandator")
  private List<Group> groups;
  @OneToMany(mappedBy = "mandator")
  private List<Department> departments;
  @OneToMany(mappedBy = "mandator")
  private List<CostUnit> costUnits;



@Entity
  public class Department {
  @Id
  @GeneratedValue
  private Integer id;
  private String name;
  private String responsiblePerson;
  private String location;

  @ManyToOne(optional = false)
  private Mandator mandator;
  @ManyToMany
  private List<DocumentUser> documentUsers;

I've really tried every thing but it didn't work.

like image 488
Miguel Iglesias Avatar asked Dec 02 '11 08:12

Miguel Iglesias


4 Answers

I just spent a lot of time debugging a seemingly unexplainable case of this exception, so I'm just leaving my story here.

If you're using a somewhat old implementation of JPA (e.g. EclipseLink 2.5.x) and you're using modern Java 8 features (such as lambda expressions) in your code base - don't ever use them in the JPA entity classes.

EclipseLink 2.5's class metadata parser crashes upon encountering Java 8 features (such as lambda expressions, method references etc.) in the bytecode. The internal crash is a very unfriendly ArrayIndexOutOfBoundsException, but you don't even get to see it, as the weaver code silently ignores metadata parser crashes and just assumes the class has no interesting metadata. This leads it to believe that the class in question is not an entity even though it has all the proper annotations, and the end result is the error message we are talking about.

Bottom line: Don't use Java 8 lambdas in JPA entity classes. EVER.

like image 175
kFYatek Avatar answered Nov 20 '22 11:11

kFYatek


Ensure you have both classes listed in your persistence.xml, and the both classes are on the classpath.

Please include your persistence.xml.

like image 24
James Avatar answered Nov 20 '22 12:11

James


I hope this can help someone.

I had the same problem. The night before, everything was ok. In NetBeans I've checked, using History, all the files involved in the error. Persistence.xml included. Nothing was changed.

I've checked also manually. I've tryed all the solution suggested in this thread even migrate to eclipselink 2.6.4. I've removed the 2 class involved in the persistence.xml, saved it, and added again. The error was still there.

Then, I've removed ALL the classess in the list of the Entity Classes included in the persistence.xml, and then I've included them again.

Now it works. Magically.

But really there were non difference in the history. Even a single character.

like image 45
docHell Avatar answered Nov 20 '22 13:11

docHell


Ensure you have defined both classes are under < persistence-unit > tag of persistence.xml file. As we know At the time of persistence the entity into DB. Our Application finds the entity classes information from persistence.xml file. If entity's mapping is not found in persistence.xml, then application throw this exception while you will perform any operation on that entity like here your are.

So Please make both entities entry in your persistence.xml

Example: 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="mapping_unit">
        <class>ch.printsoft.mailhouse.usermgr.entity.Mandator </class>
        <class>ch.printsoft.mailhouse.usermgr.entity.Department</class>
        ...
        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/practice" />
            <property name="javax.persistence.jdbc.user" value="root" />
            <property name="javax.persistence.jdbc.password" value="root" />
            <property name="eclipselink.logging.level" value="SEVERE" />
            <property name="eclipselink.ddl-generation" value="create-or-extend-tables" />
        </properties>
    </persistence-unit>
    
</persistence>
like image 2
vishal rathod Avatar answered Nov 20 '22 13:11

vishal rathod