23

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.

Xavi López
  • 27,550
  • 11
  • 97
  • 161
Miguel Iglesias
  • 379
  • 2
  • 3
  • 6

10 Answers10

58

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.

kFYatek
  • 5,503
  • 4
  • 21
  • 14
  • 2
    Tested and run into exact same prob! Thx for the BOTTOM-line which fixed the prob :-/ – LeO Mar 01 '17 at 10:24
  • 3
    But EclipseLink 2.6.4 has fixed this issue and now JPA works great with lambdas!! At least the stuff I've tested ;-) – LeO Mar 03 '17 at 08:30
  • 1
    Thanks kFYatek ! in my case, an Entity was just ignored without any Warn from JPA (in FINE log !!) and then on a @OneToMany pointing on this ignored Entity, JPA throw an Exception "entity A uses a non-entity B (the ignored one) ... how to lost 1 hour in a dark cloud ....... :p a small lambda expression was lost in my Entity B :) After replacing it, all is ok! – Elloco Aug 16 '17 at 15:21
  • Holycow (...Namaste...) I would have not guess that! Oh that little `.foreach` – TungstenX Aug 19 '19 at 08:08
  • @LeO Do you have any suggestion how to install EclipseLink 2.6.4 or greater in Eclipse Dali JPA Tools? I tried but didn't succeed yet, it seems the Dali plugin wasn't updated for quite some time... – Balder Aug 26 '19 at 11:01
51

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

Please include your persistence.xml.

James
  • 17,965
  • 11
  • 91
  • 146
5

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.

docHell
  • 121
  • 3
  • 8
3

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>
1

Remove mappedBy from @OneToMany annotations from Mandator class

Check if all related classes are mapped as @Entity. Department class has @ManyToMany relation to DocumentUser. Provide the listing of DocumentUser class

WeMakeSoftware
  • 9,039
  • 5
  • 34
  • 52
  • 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]. – Miguel Iglesias Dec 02 '11 at 09:06
  • From the Department class remove "optional = false" from @ManyToOne annotation on mandator – WeMakeSoftware Dec 02 '11 at 09:35
  • Could you update the class listings in the question with the modified annotations and provide full class listings? From the exception it seems that the entity manager "thinks" that there is no entity Department. – WeMakeSoftware Dec 02 '11 at 09:53
  • BTW, do you have DocumentUser mapped as an Entity? – WeMakeSoftware Dec 02 '11 at 10:03
0

Make sure that both "class" files are in the same jar. Make sure that you don't have two .class files with the same name !

Marcin Cinik
  • 105
  • 5
0

Ensure you have both classes has the property of PK and the no parameter constructor. I add the following code in my entity class.

@Entity
public class EntityClass{
   @Id
   @GeneratedValue(strategy=GenerationType.AUTO)
   public int id;
   public EntityClass() {
   } 
   ...
}
0

Examine your mappedBy=xxx and make sure xxx is unique in whole project.

WesternGun
  • 11,303
  • 6
  • 88
  • 157
0

1))Proper relationship for the parent and child.

First Class

@Entity
@Table(name="nova_payment_result")
@NamedQuery(name="NovaPaymentResult.findAll", query="SELECT n FROM NovaPaymentResult n")
@JsonIgnoreProperties({"novaOrder"})
public class NovaPaymentResult implements Serializable {
    private static final long serialVersionUID = 1L;
 
    //bi-directional many-to-one association to NovaOrder
    @JsonIgnoreProperties("paymentResults")
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name="orderid")
    private NovaOrder novaOrder;

2nd class

@Entity
@Table(name="nova_order")
@NamedQueries({
@NamedQuery(name="NovaOrder.findAll", query="SELECT n FROM NovaOrder n")
})
@JsonIgnoreProperties({"novaOrders","novaCustomerProfile"})
public class NovaOrder implements Serializable {
    private static final long serialVersionUID = 1L;
@OneToMany(fetch=FetchType.LAZY,cascade=CascadeType.ALL,mappedBy="novaOrder")
    private List<NovaPaymentResult> paymentResults;

Makesure entery in persistence.xml should be there for both classes.

0

What a nightmare. For me it worked when I invalidated the caches of IntelliJ Idea from the File menu

SparkOn
  • 8,806
  • 4
  • 29
  • 34