0

This is a simplified, and hopefully clearer version of a question I asked

EDIT: I'm working in NetBeans, persistence provider is Hibernate, here is an image of the actual Database tables (in response to @xiumeteo):

enter image description here

Basically, my problem is that I auto-generate ID values when I persist an entity, and even though the entities are correctly saved into the database, with a correct ID > 0, the entityManager always seems to think that the now persisted entity has an ID of 0. Which leads to subsequent violations of foreign key constraints, but here I’ll just focus on the Id being (or rather, posing as) 0, because that should be closely related to the cause of the problem, and it's in fact a mystery in and of itself.

So I have a simple entity class Person:

    @Entity
    public class Person {

       @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY) 
        @Column(nullable = false, unique = true)
        private Long id;  // I’ve also tried types Integer and int, doesn’t make a difference.

        @Column(nullable = false)
        private String name;

        @Column(nullable = false)
        private String email;
        public Person() {
        }

        public Long getId() {
            return id;
        }

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

    // setters and getters for the other fields omitted here.
    }

Then I have a session bean, containing a method create(p) that persists a person-object p:

  @Stateless
    @Default
    public class PersonRepositoryImpl implements PersonRepository {

        @PersistenceContext
        private EntityManager entityManager;

    @Override
    @Transactional  // Deleting this annotation makes no difference, only added 
                       it because I  thought it might solve the problem...
    public Person create(Person p) {
        entityManager.persist(p);
        entityManager.flush();   // Again, only added this statement because I thought it  
                                    might solve the issue, which it did not.
        return p;
        }
    }

Now in and of itself, the create(p) method does what it’s supposed to do: it persists the person p into the PERSON-table, with a correctly generated ID.

However, the problem becomes obvious when I try to get the value of that ID after the person-object has been persisted; it’s still given as being 0. So I call the create(p) method from a servlet, then immediately get the persisted object’s ID and have it print out to the console, like this (note: personRepo is an injected instance of the session bean whose definition is found above):

Person p = new Person();
p.setName("Carl"); p.setEmail(carl@carl.nl); 
p = personRepo.create(p); // Everything going fine here => entity p gets stored 
                             in the PERSON table with a correctly generated ID>0.    
System.out.println("The newly persisted entity now has the following ID: " + p.getId()); 

And that last println-statement ALWAYS prints 0 as the value of p.getId(), whereas I want it to print the ID-value of the table-row corresponding to the entity.

In answers to similar questions, I’ve read that a call to flush() should help (which is why I added it to the above create-method), but clearly in my case it does not. As seen above, even after returning from the create() method (which does the actual persisting) to the calling servlet, the Id-field is still given as having a value of 0. Even though, as I said, it IS at that point stored in the database-table PERSON with a correct ID>0.

So how do I get the getId() function to return the REAL id? (And by extension, hopefully, get the entityManager / container to 'see' the real id-value as well, so that the entity can engage in many-to-many relationships without violating FK-constraints.)

Naros
  • 19,928
  • 3
  • 41
  • 71
Holland
  • 395
  • 9
  • 22

1 Answers1

-1

Try using session.save(p) instead of entityManager.persist(p). save() will returns an identifier, and if an INSERT has to be executed to get the identifier, this INSERT happens immediately.

OR

Try refresh your entity after flush, to re-read that entity state.

entityManager.persist(p);
entityManager.flush();
entityManager.refresh(p);
Angga
  • 2,305
  • 1
  • 17
  • 21