JPA @MappedSuperclass在Eclipse中的单独JAR中 [英] JPA @MappedSuperclass in separate JAR in Eclipse

查看:299
本文介绍了JPA @MappedSuperclass在Eclipse中的单独JAR中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个@MappedSuperclass AbstractEntity,我用于所有@Entity类。只要超类与我的实体在同一个Eclipse项目中,它就可以完美地工作。但是因为我在几个项目中重用了这个超类,所以我只想把它分解成自己的JAR文件。当我这样做时(当然我将JAR文件添加到构建路径中),Eclipse在我的每个@Entity类上都会出错:

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:

实体没有定义主键属性。

Eclipse突出显示@Entity注释作为错误的来源。当然,所有类都继承自此AbstractEntity。两个项目中的包名称相同。 JAR项目具有所有必要的构建路径 - 包含AbstractEntity的JAR文件项目中没有错误。

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.

当我将其部署到我的应用服务器(JBoss 7.1)时,它工作正常。这让我觉得这只是一个错误地识别错误的Eclipse问题。

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.

抽象实体:

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;
}
}

简单实体类示例:

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

我看到其他一些帖子说问题可能是超类中的注释是在getter上而不是在字段上 - 如果您在实体中没有其他JPA注释,这只是一个问题,我这样做。有什么想法吗?

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?

推荐答案

看起来这与这里描述的错误有关:

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

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

最终解决了什么(更多的解决方法)是添加 AbstractEntity 到包含我的实体的项目中的 persistence.xml 中的类列表。即使 AbstractEntity 位于单独的 JAR 中,并且本身不是具体实体,我想这给了Eclipse足够的信息停止抱怨。这似乎没有对应用程序本身产生任何不利影响。

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.

这篇关于JPA @MappedSuperclass在Eclipse中的单独JAR中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆