I have several entities in my project, all of them extending this abstract class:
@MappedSuperclass
public abstract class AbstractClass<PK extends Serializable> implements Serializable {
public static final String GENERATOR = "default";
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = GENERATOR)
private PK id;
// setters and getters
}
All the entities are using the same annotations, so I'm going to show you only one:
@Entity
@Table(name = "TABLE1")
@SequenceGenerator(name = AbstractClass.GENERATOR, sequenceName = "SEQ_NAME_ENTITY1", allocationSize = 1)
@AttributeOverride(name = "id", column = @Column(name = "ID_ENTITY1", nullable = false, precision = 17, scale = 0))
public class Entity1 extends AbstractClass<Long> {
// Strings, Longs, etc of this entity
}
Each entity in my project has a different sequenceName, so one entity has "SEQ_NAME_ENTITY1", another "SEQ_NAME_ENTITY2", and so on.
My EJB containing the entityManager looks like this:
@Stateless
public class PersistenceBean implements IPersistenceBean {
@PersistenceContext(unitName = "MY_UNIT_NAME")
private EntityManager entityManager;
public EntityManager getEntityManager() {
return entityManager;
}
}
I'm inyecting this EJB in every DAO, and using it like this:
@Stateless
public class MyDao implements IMyDao {
@EJB
protected IPersistenceBean persistenceBean;
public Entity1 save(Entity1 entity) {
persistenceBean.getEntityManager().persist(entity);
persistenceBean.getEntityManager().flush();
persistenceBean.getEntityManager().refresh(entity);
return entity;
}
}
Every DAO looks the same for every entity.
The problem I'm facing here is that every time I insert an entity, all of them reuses the same sequence, for example all of them will take the sequence returned by the generator of the entity4, and ignore their own sequences.
I tried setting an inexistent sequence for some entities, and the program will crash whenever it tries to get the next value, however, as soon as I fix them, they will be ignored and the value of another sequence will be taken.
I think the issue here might be related with using the same entityManager everywhere, but I'm not sure why everything else works as intended, and only the sequences are messed up.