hibernate version 2.1.2
db : mysql (oracle)
For this very simple PO example, first under the transaction control with spring when i want to insert record with value(80,800) which the key is duplicate with the existed record (80,XXX), the result throws java.sql.BatchUpdateException,then i got rid of sping control with all hibernate code which is under here, it also throws java.sql.BatchUpdateException, but record(79,790) has been inserted already when the BatchUpdateException occured. Strangely, when i use the composit id (for pk class), transaction can been successfully rollbacked.The reasion seems when "session.save(ma,ma.getApparatusId()) ; ", it can't found that the key is duplicated,so not throws the duplicated key exception,How can i resolve that? 3ks,
Code:
public void testTran() throws HibernateException{
beginTransaction();
try{
ApparatusMaster ma = new ApparatusMaster();
ma.setApparatusId("79");
ma.setApparatusName("790");
session.save(ma,ma.getApparatusId()) ;
ma = new ApparatusMaster();
ma.setApparatusId("80");
ma.setApparatusName("800");
session.save(ma,ma.getApparatusId()) ;
endTransaction(true);
}catch(Exception e){
endTransaction(false);
throw new HibernateException(e.getMessage());
}
}
public void beginTransaction()
throws HibernateException {
session = Persistance.getSession();
transaction = session.beginTransaction();
}
public void endTransaction(boolean commit)
throws HibernateException {
if (commit) {
transaction.commit();
} else {
transaction.rollback();
}
}
Exception:
Code:
Hibernate: insert into apparatus_master (APPARATUS_NAME, APPARATUS_ID) values (?
, ?)
227237 [Thread-3] WARN util.JDBCExceptionReporter - SQL Error: 1062, SQLState:
S1009
227237 [Thread-3] ERROR util.JDBCExceptionReporter - Invalid argument value, m
essage from server: "Duplicate entry '80' for key 1"
227237 [Thread-3] WARN util.JDBCExceptionReporter - SQL Error: 1062, SQLState:
S1009
227237 [Thread-3] ERROR util.JDBCExceptionReporter - Invalid argument value, m
essage from server: "Duplicate entry '80' for key 1"
227237 [Thread-3] ERROR util.JDBCExceptionReporter - Could not execute JDBC bat
ch update
java.sql.BatchUpdateException: Invalid argument value, message from server: "Du
plicate entry '80' for key 1"
at com.mysql.jdbc.PreparedStatement.executeBatch(PreparedStatement.java:
hbm-xml :
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 2.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd" >
<hibernate-mapping>
<class
name="com.test1.ApparatusMaster"
table="apparatus_master"
>
<id
name="apparatusId"
type="java.lang.String"
column="APPARATUS_ID"
>
<generator class="native" />
</id>
<property
name="apparatusName"
type="java.lang.String"
column="APPARATUS_NAME"
not-null="true"
length="20"
/>
</class>
</hibernate-mapping>
PO:
Code:
public class ApparatusMaster implements Serializable {
/** identifier field */
private String apparatusId;
/** persistent field */
private String apparatusName;
/** full constructor */
public ApparatusMaster(String apparatusName) {
this.apparatusName = apparatusName;
}
/** default constructor */
public ApparatusMaster() {
}
public String getApparatusId() {
return this.apparatusId;
}
public void setApparatusId(String apparatusId) {
this.apparatusId = apparatusId;
}
public String getApparatusName() {
return this.apparatusName;
}
public void setApparatusName(String apparatusName) {
this.apparatusName = apparatusName;
}
[/code]