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

ぐ巨炮叔叔 提交于 2019-11-29 13:28:31

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

Please include your persistence.xml.

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.

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.

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

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 !

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

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

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