-
1. Re: Sanity check on FlushMode.MANUAL and persist()
christian.bauer Jul 3, 2008 2:03 PM (in response to damianharvey.damianharvey.gmail.com)Difference between pre- and post-INSERT id generators. There are books about it.
-
2. Re: Sanity check on FlushMode.MANUAL and persist()
damianharvey.damianharvey.gmail.com Jul 3, 2008 3:55 PM (in response to damianharvey.damianharvey.gmail.com)Thanks Christian. I have your book and chapter 11.2.3 was very helpful. I see your point about pre and post-INSERT ID generators.
I am using GenerationType.IDENTITY which is forcing the insert so that Hibernate can get the ID.
As I see it I now have 3 options that are discussed in that chapter:
- Change my GenerationType to something like SEQUENCE or TABLE. However MySQL doesn't support SEQUENCE.
- Follow the JPA approach and perform the insert outside of a transaction (which just seems odd)
- Use Compensation actions to undo what has been inserted to this point.
For my use case option 3 will be the easiest to implement.
Cheers,
Damian.
-
3. Re: Sanity check on FlushMode.MANUAL and persist()
aaragus.sibeon.gmail.com Jul 3, 2008 4:10 PM (in response to damianharvey.damianharvey.gmail.com)Hello there,
The solution we found here was to use the evict(obj) option that comes with the Hibernate Session.
Try it out
Darryl A. Sibeon
-
4. Re: Sanity check on FlushMode.MANUAL and persist()
christian.bauer Jul 3, 2008 4:19 PM (in response to damianharvey.damianharvey.gmail.com)Look at how the wiki software uses a fake-sequence generator on MySQL, which is a relatively new Hibernate feature. You definitely want pre-INSERT identifier generators wherever you are.
-
6. Re: Sanity check on FlushMode.MANUAL and persist()
damianharvey.damianharvey.gmail.com Jul 3, 2008 5:18 PM (in response to damianharvey.damianharvey.gmail.com)Thanks, that works a treat.
Is the best practice here to use the SequenceStyleGenerator with one common record for all Entities (as per the Seam Wiki) or to maybe use the TableGenerator with a row for each entity? Are there advantages/disadvantages of each approach? With over 100 entities I'm leaning toward the TableGenerator.
For anyone finding this thread later I have changed the ID of my test entity from:
@Id @GeneratedValue(strategy = IDENTITY) @Column(name = "ID", unique = true, nullable = false) public Long getId() { return this.id; }
to
@Id @GenericGenerator( name = "personSequenceGenerator", strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator", parameters = { @Parameter(name = "sequence_name", value = "PERSON_SEQUENCE"), @Parameter(name = "initial_value", value = "1000"), @Parameter(name = "increment_size", value = "1") } ) @GeneratedValue(generator = "personSequenceGenerator") @Column(name = "ID", unique = true, nullable = false) public Long getId() { return this.id; }
And created a table called PERSON_SEQUENCE with one field called next_val.
Cheers,
Damian