tags:

views:

481

answers:

2

In JPA, I am using @GeneratedValue:

///////////

@TableGenerator(name = "idGenerator", table = "generator", pkColumnName = "Indecator" , valueColumnName = "value", pkColumnValue = "man")

@Entity

@Table(name="Man")

public class Man implements Serializable {

@Id

@GeneratedValue(strategy = GenerationType.TABLE, generator = "idGenerator")

@Column(name="ID")

private long id;

public void setId(Long i){

  this.id=i;
}

public Long getId(){

  return id;


}

}
///////////

I initially set the ID to some arbitrary value (used as a test condition later on):

///////////

public class Sear {

  public static void main(String[] args) throws Exception {

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("testID");

    EntityManager em = emf.createEntityManager();

    Man man = new Man();

    man.setId(-1L);

    try {

      em.getTransaction().begin();

      em.persist(man);

      em.getTransaction().commit();

    } catch (Exception e) { }

    if(man.getId() == -1)
      ;  
  }
}

///////////

What is the expected value of man.id after executing commit()? Should it be (-1), a newly generated value, or I should expect an exception?

I want to use that check to detect any exceptions while persisting.

A: 

You setting the value of a field that is auto-generated is irrelevant. It will be (should be) set by the JPA implementation according to the strategy specified.

DataNucleus
but i set this value to check if it will be changed or not.i ask is there is any configuration to force the JPA provider to override this value by new generated one ?. or there is a provider that actually do this ?!. or is there is configuration to fix this problem?when i used eclipselink it persisted the -1 in the database without overriding it by generated value,but when i used hibernate it throwed exception.
This is not defined in the JPA spec so expect anything. An implementation can do what it feels like in that case; blame the spec for not specifying behaviour. DataNucleus allows you to set the value and in that case not to generate a value for the field.
DataNucleus
thanks a lotbut please you said "DataNucleus allows you to set the value and in that case not to generate a value for the field."is there any other provider allows me to set the value and then it generate new one when persisting the entity
No idea. I write DataNucleus so why would I possibly recommend a different one ? There is no other with the same flexibility
DataNucleus
but i don't want this flexibility.i want other one to override the value that i will set by generated one. because i set this value to indicate if the entity persisted in the database or not.and if you know why hibernate provider throw "org.hibernate.PersistentObjectException: detached entity passed to persist" when i run the above code while eclipselink works as DataNucleus????
I already told you, DataNucleus supports a user providing a value and in that case using their value and not generating a value. Or you can have it to always generate a value. So DataNucleus does exactly what you want to do. I *suggest* that this answers your question and you mark it as such
DataNucleus
A: 

What is the expected value of man.id after executing commit()? Should it be (-1), a newly generated value, or I should expect an exception?

You are just not supposed to set the id when using GeneratedValue. Behavior on persist will differ from one implementation to another and relying on this behavior is thus a bad idea (non portable).

I want to use that check to detect any exceptions while persisting.

JPA will throw a (subclass of) PersistenceException if a problem occurs. The right way to handle a problem would be to catch this exception (this is a RuntimeExeption by the way).

If you insist with a poor man check, don't assign the id and check if you still have the default value after persist (in your case, it would be 0L).

Pascal Thivent