-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Id Generator creating changes on unchanged objects
PostPosted: Thu May 05, 2011 6:30 am 
Newbie

Joined: Tue Feb 01, 2011 11:06 am
Posts: 3
Hello,

now it’s the real problem I'm facing and not some kind of symptom.

I've got the following model with hibernate annotations, a customer with a shippingAddress, both have a version:
Code:
@Entity
@Table(name = "CUST_CUSTOMER")
@Inheritance(strategy = InheritanceType.JOINED)
public class Customer implements DomainEntity, CreationTimestampAware, UpdateTimestampAware {

   
   @Id
   @Column(name = "CUST_ID", nullable = false)
   //   @GenericGenerator(name = "CustomerIdGenerator", strategy = "org.hibernate.id.enhanced.TableGenerator", parameters = {
   //      @Parameter(name = "value_column_name", value = "SEQUENCE_NEXT_HI_VALUE"), //
   //      @Parameter(name = "table_name", value = "HIBERNATE_SEQUENCES"), //
   //      @Parameter(name = "segment_value", value = "CUST__CUSTOMER"), //
   //      @Parameter(name = "increment_size", value = "50"),
   //      @Parameter(name = "optimizer", value = "MyOnlyEvenIdsOptimizer") //
   //   })
   //   @GeneratedValue(generator = "CustomerIdGenerator")
   private Long id = new Long(new Random(123456789L).nextInt());
   
   /**
    * @see #getShippingAddress()
    */
   @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
   @JoinColumn(name = "CUST_SHIPPING_ADDRESS_ID")
   @ForeignKey(name = "FK_CUSTOMER_SHIPPING_ADDRESS")
   @Valid
   private Address shippingAddress;
   
   @Version
   @Column(name = "CUST_VERSION", nullable = true)
   private long version = 0;
   
   @Column(name = "CUST_NAME", nullable = false)
   @Length(max = 60)
   private String name;
   
}

@Entity
@Table(name = "CUST_ADDRESS")
public class Address implements DomainEntity, CreationTimestampAware, UpdateTimestampAware {

   /**
    * @see #getId()
    */
   @Id
   @Column(name = "ADDRESS_ID", nullable = false, insertable = true, updatable = false)
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private Long id;
   //   = new Long(new Random(123456789L).nextInt());
   
   @Version
   @Column(name = "ADDRESS_VERSION", nullable = true)
   private long version = 0;
}



I now create the customer john with an address and perform a saveOrUpdate.
The versions of john and the shipping are set to 0 which is OK.

But after i saveOrUpdate the unchanged john again the version of the address is incremented!
Nothing changed so where is the error?
I figured out that both entities are using different methods of id generation, because if i change the generation of the address
to the method(simple random) of the customer (commented out in address example) it works perfectly well.


Code:
      Customer john = this.createJohnFixture();

      this.txbegin();
         this.hibernateTemplate.saveOrUpdate(john);
      this.txcommit();
      
      System.out.println(john.getVersion() + "--johnversion--"); //version is 0 a expected
      Address ship = john.getShippingAddress();
      System.out.println(ship.getVersion() + "--shipversion--");//version is 0 a expected
      
      this.txbegin();
         this.hibernateTemplate.saveOrUpdate(john);
      this.commit();
      
      System.out.println(john.getVersion() + "--johnversion--");//version is 1 a expected
      System.out.println(ship.getVersion() + "--shipversion--");//version is 1 this is an error cause the address was not changed


Is there any way that i can use different id generator and be consistent with my versions?
Why is the version changed at all?
I don't have a clue and hope for some insight from your side!

I've been looking for clues for the last 4 days and ... someone hand me a gun.

Thanks, and best regards

dummyuser


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.