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

时光毁灭记忆、已成空白 提交于 2020-01-20 05:32:44

问题


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.


回答1:


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

Please include your persistence.xml.




回答2:


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.




回答3:


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.




回答4:


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




回答5:


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 !




回答6:


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() {
   } 
   ...
}



回答7:


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



来源:https://stackoverflow.com/questions/8353752/jpa-error-uses-a-non-entity-class-ch-printsoft-mailhouse-usermgr-entity-departm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!