不同包中具有相同名称的两个实体[重复]

时间:2022-09-11 12:40:37

This question already has an answer here:

这个问题在这里已有答案:

I have two entities with the same name (but in differents packages) in my project, that cause an exception :

我的项目中有两个具有相同名称(但在不同包中)的实体,这会导致异常:

Use of the same entity name twice: MyEntity 

duplicate import: MyEntity refers to both packageOne.MyEntity and packageTwo.MyEntity (try using auto-import="false")

I tried to set the property auto-import to false in my persistence.xml, but it doesn't change anything :

我尝试在persistence.xml中将属性auto-import设置为false,但它不会更改任何内容:

<property name="hibernate.auto-import"  value="false"/>

Is there another solution than add a name in the Entity annotation

是否有另一种解决方案,而不是在实体注释中添加名称

@Entity(name="MyEntity_One")
@Entity(name="MyEntity_Two")

?

2 个解决方案

#1


Am sure you are not getting a compile time error, its deployment error that you must be encountering. the problem is when you package then into an ear/war before deploying, both the class come to the server. Whilst any JPA application the container does not know which class to invoke, thus it throws an exception

我确定你没有得到编译时错误,它必须遇到它的部署错误。问题是当你在部署之前将其打包到耳朵/战争中时,这两个类都会进入服务器。虽然任何JPA应用程序容器都不知道要调用哪个类,但它会引发异常

Is it not possible for you to extend one class and carry your operations or you can create an instance of one class in another and add a one-to-one mapping between them.

您是否无法扩展一个类并进行操作,或者您可以在另一个类中创建一个类的实例,并在它们之间添加一对一映射。

#2


This implies you have two tables with the same name in your database, which is highly unlikely.

这意味着您的数据库中有两个具有相同名称的表,这种可能性极小。

You'll need to use the Entity.name value, or if you're using JPA you can also use the javax.persistence.Table annotation.

您需要使用Entity.name值,或者如果您使用JPA,还可以使用javax.persistence.Table注释。

#1


Am sure you are not getting a compile time error, its deployment error that you must be encountering. the problem is when you package then into an ear/war before deploying, both the class come to the server. Whilst any JPA application the container does not know which class to invoke, thus it throws an exception

我确定你没有得到编译时错误,它必须遇到它的部署错误。问题是当你在部署之前将其打包到耳朵/战争中时,这两个类都会进入服务器。虽然任何JPA应用程序容器都不知道要调用哪个类,但它会引发异常

Is it not possible for you to extend one class and carry your operations or you can create an instance of one class in another and add a one-to-one mapping between them.

您是否无法扩展一个类并进行操作,或者您可以在另一个类中创建一个类的实例,并在它们之间添加一对一映射。

#2


This implies you have two tables with the same name in your database, which is highly unlikely.

这意味着您的数据库中有两个具有相同名称的表,这种可能性极小。

You'll need to use the Entity.name value, or if you're using JPA you can also use the javax.persistence.Table annotation.

您需要使用Entity.name值,或者如果您使用JPA,还可以使用javax.persistence.Table注释。