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