How you fix the following Hibernate error:
What does \"Use of the same entity name twice\".
This exception occures when you have more then one @Entity with the same class's name or explicit name. To fix the issue you have to set different explicit names for each entity.
Example of error case:
package A;
@Entity
class Cell{
...
}
package B;
@Entity
class Cell{
...
}
Solution example: package A;
@Entity(name="a.Cell")
class Cell{
...
}
package B;
@Entity(name="b.Cell")
class Cell{
...
}
So, to use them in HQL you have to write
...createQuery("from a.Cell")...
Another common mistake, is that you recently moved one of your persistence classes (from one package to another), but your IDE failed to clean up your .class files properly.
Or some .class files are still hanging around in your Application Server.
One of the most common mistakes you could make to produce this error is to attempt to persist two different Java classes in identically-named tables. Hibernate likes there being exactly one kind of thing in each table (with some exceptions for subclasses and the like), and so if you were to create a class called maybe StudentRecord and a class called MusicRecords, and if you then told Hibernate to persist both of those classes into a table called "records", you could produce that kind of exception. With that particular wording, I suspect you're using annotations (in which case it's even easier to accidentally name two tables, described in two different Java classes, the same thing).
Hope this helps! (although perhaps not, as I have noticed just now that you asked this question 7 months ago. I do hope you're not still stuck, sir!)
I've come across this error a few different times. The causes were as follows:
I have this error (duplicate import) recently: two entities with the same name 'MyEntity' but from different packages/modules: com.test1.MyEntity com.test2.MyEntity
I didn't use them but they were loaded by hibernate to jboss. I wasn't allowed to change entities so I had to do some workaround.
<property name="hibernate.auto-import" value="false"/>
to persistance.xml. It prevented of throwing duplicate exception while deploying to jboss. But exception was thrown when my query was called.entityManager.createQuery("Select a.Name, b.name from AEntity a,
BEntity b where a.ID = b.parentID")
It's ugly but it's a workaround.
I think it means you have declared the same entity in more than one configuration file.
Without more information, I would try commenting out chunks of your config file so that you don't see the error, and then slowly adding sections back in until you encounter the error?
If its only a few config files, then why not post them here? When posting, if you add 4 spaces to the front of your XML, then it will be:
<xml>nicely formatted</xml>
Hope this helps.