user3408439
user3408439

Reputation: 55

EcllipseLink: JPA Entities with same name. Error: Entity name must be unique in a persistence unit

I am migrating code from EJB2 to EJB3. I have converted POJOs to JPA Entities. There are few entities have same names such as @Entity(name="myentity1"). I am getting error at deployment "Entity name must be unique in a persistence unit". This is happened because modules have dependencies on other modules which have entities with same name. Is there any way to interrupt it and update name every time dynamically when it's used (SessionCustomizer or any other way)? I can’t change entity name at this point because entity names are used widely. I am using EcllipseLink 2.5

Sample Code

EJB Module1 (Dependency on EJB Module2)

package com.my.module1.package1;

@javax.persistence.Entity(name = "myentity1")
@Table(name = "TABLE1")
public class MyEntity1
    implements Serializable {

    @Column(name = "ID")
    private Long Id;

    public Long getId() {
    return Id;
  }

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

// EJB Module2

package com.my.module2.package2;

@javax.persistence.Entity(name = "myentity1")
@Table(name = "TABLE1")
public class MyEntity1
    implements Serializable {

    @Column(name = "ID")
    private Long Id;

    public Long getId() {
    return Id;
  }

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

Any help would be much appreciated.

Upvotes: 0

Views: 7896

Answers (2)

Koitoer
Koitoer

Reputation: 19543

Yes there is a way. In the persistence.xml, you can define your own Persistence Unit, also a list of classes that you want to have in your persistence Unit, also there is a xml tag

<exclude-unlisted-classes>true</exclude-unlisted-classes>

The above will disable to add any other entity class that are not listed in , other classes wont be added to the persistenceUnit and I think this will solve the collision problem.

Just configure with <class> and the above tag all your persistence units.

Upvotes: 1

Sabuj Hassan
Sabuj Hassan

Reputation: 39405

Keep annotation(@Table(name = "TABLE1")) only above the one you are using for persisting into database through the EntityManager. And remove from others. Your entity names must have to be unique in persistence unit.

Upvotes: 0

Related Questions