JPA @MappedSuperclass in separate JAR in Eclipse

喜你入骨 提交于 2019-12-10 09:28:51

问题


I've got a @MappedSuperclass AbstractEntity that I use for all of my @Entity classes. It works perfectly as long as the superclass is in the same Eclipse project as my entities. But since I reuse this superclass among several projects, I just want to break it out into its own JAR file. When I do that (and of course I add the JAR file to the build path), Eclipse gives an error on every one of my @Entity classes:

The entity has no primary key attribute defined.

Eclipse highlights the @Entity annotation as the source of the error. Of course all of the classes do inherit from this AbstractEntity. The package name is the same in both projects. The JAR project has all necessary build paths - there are no errors in the JAR file project containing the AbstractEntity.

When I deploy this to my app server (JBoss 7.1), it works fine. This makes me think it is just an Eclipse issue where it is falsely identifying an error.

The Abstract Entity:

package com.xyc.abc;

import java.io.Serializable;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;

@MappedSuperclass
public abstract class AbstractEntity implements Serializable {

private static final long serialVersionUID = 1L;

private Long id;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
public Long getId() {
    return id;
}

public void setId(Long id) {
    this.id = id;
}
}

Simple Entity Class Example:

 @Entity
 public class TestEntity extends AbstractEntity {
 ...
 }

I've seen some other posts saying that the problem might be that the annotations in the superclass are on getters instead of on the fields - this is only a problem if you don't have other JPA annotations inside your entities, which I do. Any ideas?


回答1:


It looks like this is related to the bug described here:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=361042

What ended up fixing it (more of a workaround really) is adding the AbstractEntity to the list of classes in persistence.xml in the project containing my entities. Even though the AbstractEntity is located in a separate JAR and is not a concrete entity itself, I guess this gives Eclipse enough information to stop complaining. This does not seem to have had any adverse effect on the application itself.



来源:https://stackoverflow.com/questions/12045710/jpa-mappedsuperclass-in-separate-jar-in-eclipse

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