Oracle Trigger and Hibernate Conflict Capitain Write: Our database definition mapping uses
SEQ_SOMENAME Param>
generator>
id>
But doing this, I found that Hibernate will get sequence, and a database has a Triger to get sequence, which actually sequence will add 2! ! Moreover, the primary key ID of the object returned by Create is also wrong, because it is also added by Triger 1, the record inside the database is full, 2, 4, 6, 8 create returns 1, 3, 5, 7 I would like this How to solve the problem, because Triger can't be deleted, can Hibernate use Triger to generate the value? I have viewed Hibernate documents and source code, and I found that when Generator was Sequence, it would finally call this method in DiaLect's public string getsequencenextValstring (String SequenceName) {
Return "SELECT" SequenceName ".NextVal from DUAL";
}
Triger is then going to take this time. Robbin Write: I have the simplest solution: see hibernate2.0.3 source code net.sf.hibernate.dialect.racle9Diaalect Chapter 78: Public String getsequencenextValstring (String sequenceename) {
Return "SELECT" SequenceName ".NextVal from DUAL";
}
As long as we change it into the following, it will be: public string getsequencenextvalstring (String sequenceename) {
Return "SELECT" SequenAme ".Currval 1 from DUAL";
}
That is to say when Hibernate goes sequence value, do not let sequence plus 1, only to take the current value and then add 1 return, when inserted, let Trigger to give sequence plus 1, so you can do this, write a new Diagect: package Net.sf.hibernate.dialect;
Public class oracleMyDiaLect Extends ORALEDIALECT {
Public String getsequencenextValstring (String sequenceename) {
Return "SELECT" SequenAme ".Currval 1 from DUAL";
}
}
After compiling, put it in ClassPath, configure hibernateHibernate.DiaLect.sf.hibernate.Dialect.OrcleMyDiaLect, solve your problem. Capitain Write: Thank you, I am very unfamiliar with Oracle. I don't know, if I only add a record in multiple connections to the database, there will be no problem, in other words, I don't know this GetSequencenexTvalstring () Is there a problem with Triger occurs? I don't know if the Oracle driver can guarantee this an atomic operation? Robbin Write: It is not possible to guarantee atomicity, there may be concurrent conflicts. But now you can only have this, saying that this method can only be considered. Since session.save () needs to return to the primary key, when you use TRIGGER to create an ID, Hibernate is unable to know what the primary key currently created by Trigger, whether you use Select SEqu.currval from DUAL;
Or SELECT MAX (ID) from Table_Name;
After inserting the data, you have the possibility of concurrent conflict. The only way to avoid concurrency is: INSERT INTO TABLE_NAME ... RETURNING: ID;
Use Oracle unique to insert data, return to the primary key, let Hibernate know the value of the primary key, but hibernate does not support this SQL statement, if you want Hibernate to support this SQL, you need to make a considerable change to the source code. Will you add it later. I am not discussing with others for the first time. It doesn't have a good solution unless Hibernate is largely modified. But when I came back, I used Oracle for so many years, I never used Trigger to implement the Auto Increment field, I personally think this way is not good. Original Oracle has already liberated Sequence from the field, you have to put it back. You can generate primary keys without Trigger, see two examples: Insert Single Data: Insert Table_Name (ID, Name, ...) Values (sequ.nextval, ....);
Batch inserted data: INSERT TABLE_NAME (ID, Name, ...) Select sequ .nextval, name, ... from table2_name
Richart Write: If you do this, it is best to use the NVL () function (Oracle comes with it), and can only change one of Oracle related classes. If you are using another database, I don't know if there is a similar problem. Robbin Write: Oldma wrote: Oh, can anyone explain? Look at the PL / SQL: Create Trigger Y Before Insert on Bob
For Each Row
When (new.a is null)
Begin
Select X.NextVal Into: New.a from Dual;
END;