Eclipse上JPA报错:Target Entity is not a entity 和the "mapped by" attribute has an invalid mapping type

项目选中JPA2.1后,源文件上会报错,但并不影响运行,只不过几个红叉放在那儿,对于有强迫症的我来说,实在不能忍受。

始终也找不到它报错的根本原因,也无法从根本上解决这个问题,没办法,我只能去屏蔽这个错误。

先说第一个Target Entity is not a entity

这个我在论坛上提出个,但最终也是没有解决:http://bbs.csdn.net/topics/391932124

把我自己最终找到问题的回复引用于此:

经过我不懈且神经质的努力,终于解决了,可能理解的不太对,所以下面的说法未必正确,但我解决的方法确实是这样,希望高手指正我的错误,因为我确实对很多东西都理解不深,仅仅拿来用而已。
原来的项目导入到eclipse3.2中的时候,查看项目properties-->project Facets,这时默认不不选中JPA的,而在4.5.2中的时候,JPA是选中的,如图。

当选中JPA项目时,就会用JPA的规则进行验证,所以就会报错,即使是在3.2中,只要选中了JPA,也会报错。因为我的文件里用的是泛型,在实际运行时,泛型对应的关系可以转化为实际对应的实体,但是在此之前,它确实不是一个entity,因为它是一个继承于abstract类的泛型,不可能实体化。所以JPA的校验一定会报错,要关掉这个错误,可以选择在项目properties-->JPA(注意,project Facets中选中JPA,才会显示此项)-->Error/Warnnings中,选中Enable project spacific settings,以单独对这个项目编辑报错项,然后展开Attribute,将下面的Target entity is not an entity由Error改成其它(我改成了Info),然后就不再会显示这个错误了,如图。

当然这样也有一个不好之处,就是其它情况下的这种错误,也不报错了。那我程序中的这个错误究竟是我写法有问题,还是JPA验证没有考虑到这个情况,就不得而知了。
选中这个JPA有什么用呢?看一下下面这篇文章,可能会有所启发,可惜我不是这样用的。而且我也不知道,我这种包含泛型的写法,能不能适用这种方法,等有时间再测试一下吧。
http://my.oschina.net/hcliu/blog/401262

第二个问题:the "mapped by" attribute has an invalid mapping type for this relationship.

这个真是无解,我连上面的原因都无法分析,因为根本就找不到错误的理由,我的代码简化如下:

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "mg_sys_department")
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.INTEGER, length = 4)
@DiscriminatorValue(value = "1")
public class Department{
	private Department parent;
	private List children = new ArrayList();

	@ManyToOne
	@JoinColumn(name = "parent_id")
	public Department getParent() {
		return parent;
	}

	public void setParent(Department parent) {
		this.parent = parent;
	}

	@OneToMany(mappedBy = "parent")
	@OrderBy("id")
	public List getChildren() {
		return children;
	}

	public void setChildren(List children) {
		this.children = children;
	}
}

这有什么错?不都是这么用的吗?可它就是报错,这次我连原因都懒得找了,反正运行也没问题,直接去JPA的错误选项里屏蔽。

位置:项目--》属性--》JPA--》Error/Warnnings--》Attribute --》Attribute has invalid mapping for this relationship将Error换成你喜欢的,至少,不再看到那恼人的红叉了。

具体什么原因报的错,希望有一天,我能找到答案。

你可能感兴趣的:(JAVA,ECLIPSE,JPA)