Announcement Announcement Module
No announcement yet.
unsaved values on hibernate annotations and oracle sequences Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • unsaved values on hibernate annotations and oracle sequences

    i have the next code in my bean to the persistence layer:

    class User{
    @Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="gen_USERS")
        @SequenceGenerator(name="gen_USERS", sequenceName = "seq_USERS")
        @Column(name="user_id", updatable=false)
        public Long getId(){
        	return super.getId();
    The problem is that now, when i call the, if there are an error during the insert query, i get the id of the user.
    I don't like to get the id of the user if the user is not saved in the database, with xdoclet there were a simple way, by writing

    Code: .... unsaved-value="null"
    but with annotations i don't find the solution to this issue.
    I'm searching during hours for fix this fail in my application, i have a way, getting the exception that the insert throws and putted a null in the property id of the bean, but is not a good idea, i like that hibernate makes this for me.
    Have somebody found something about it?

  • #2
    another error in the previous post

    if there are a fail during the insert query, the id is a random value that haven't got anything in common with the value that must be due to the sequence, to this issue:

    @Id @GeneratedValue(generator="gen_USERS")
    @GenericGenerator(name="gen_USERS", strategy="sequence", parameters = {
                @Parameter(name = "sequence", value = "seq_USERS"),
                @Parameter(name = "unsaved-value" , value = "null"),
                @Parameter(name = "allocationSize", value = "1")
    i think that the parameter unsaved-value in this case would work for my problem, but this doesn't work.
    In the documentations say that the GenericGenerator params where as you write in a standard jpa xml (probably would be the better, start the application in xml instead of annotations, but this is just initializated, and now it haven't got any sense), but the parameter unsaved-value is not getted for the id property if the object is not persisted in the database.

    I need help to this, i need that hibernate don't save nothing, or save all, not a partial query (select seq_USERS.nextval from dual) and then the other part (insert into *... ).

    I think that before, with the SequenceGenerator, the problem were worst, because the value were a random value, now, the problem continues, but i have the value from the sequence, it doesn't fix nothing, but i think i'm advancing to find the solution.


    • #3
      Problem when i save an object

      When i save an object, in the last configuration, i have one value to the sequence of the id, i have an id number 823 and in the database i have saved the object with the id 824.
      I think that when i called save, the object id will be actualized, but it doesn't work, in the object, i have the an incorrect id after calling save with the method save of the DAO, i show the code:

      public void save(Object o) {
           	log.trace("Persisting object " + o);
     " flush mode : " + getHibernateTemplate().getFlushMode());
               User savedUser = (User)o;
     " the id is " + savedUser.getId());
              // o = get(User.class, savedUser.getId());
      what would be the error?
      why the object in the database have the id 824 and is not actualized the reference to the object with this id, i have the id 823.
      I'm going crazy with this error, if i use another database there were any problems, but with oracle sequences, it's the worst.

      I'm based my application on appfuse with spring.
      I don't know how to integrate this with hibernate annotations and oracle sequences.


      • #4
        After Hibernate throws an exception, the whole session and any objects you tried to persist are in undefined state. The only thing you can do with them is throw them all away.


        • #5
          thanks for your note, but the exception now is not the problem

          the main problem is the sequence generated id with the oracle sequences.
          It doesn't refresh the id of the persistentObject, i save the persistentObject, and the id assigned in the session is X, but in the database is X+1, when i use the persistentObject saved in my controllers, i've got an incorrect value, and every things are wrong, because i've got an invalid id (X) instead the id saved in the database (x+1).

          I try a lot of diferent annotations on the bean, the annotations are in the base class BaseUser... but the entity is User, but i don't think that this is the problem.

          Why the object is not refreshed in the session with the id getted from the database?
          This is now the main problem on my appfuse based application, when i call save on a controller, the object is saved in the database, but the id is not refreshed in the object.

          I try a lot of different ways, calling sionInViewFilter, changing the configuration of the @GeneratedValue and the @SequenceGenerator...

          Thanks for all.

          Thanks for your reply.
          Last edited by cclafuente; Oct 29th, 2007, 07:19 AM.