I observed that on using CompositeUserType, when I save an object which contains the property defined by CompositeUserType,
the SQLs generated are an insert statement followed by an update statement with only the fields in CompositeUserType.
My application is very sensitive to the SQLs generated as there are triggers on update statements and this results in a pseudo update in my audit tables . Is this a bug with hibernate or is it the expected behaviour ?
Hibernate version:
3.2.1
Mapping documents:
Code:
<id name="id" type="int" unsaved-value="null" >
<column name="id" not-null="true"/>
<generator class="increment"/>
</id>
<property column="a1" name="a1" type="string"/>
<property column="a2" name="a2" type="string"/>
<property name="cols" type="com.wipro.poc.TestCompositeUserType" index="3,4,5" lazy="true">
<column name="A3"/>
<column name="A4"/>
<column name="A5"/>
</property>
A.class
Code:
public class A {
private int id;
private String[] cols;
public String a1;
public String a2;
}
Code:
public class TestCompositeUserType implements CompositeUserType{
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException {
if (rs.wasNull()) return null;
// if all the cols are null, return a null object
if (rs.getString(names[0]) == null && rs.getString(names[1]) == null && rs.getString(names[2]) == null) return null;
// Set the values reading from resultSet
String cols[] = new String[3];
cols[0] = rs.getString(names[0]);
cols[1] = rs.getString(names[1]);
cols[2] = rs.getString(names[2]);
return cols;
}
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException {
// TODO Auto-generated method stub
if (value== null) {
st.setNull(index, Types.VARCHAR);
st.setNull(index + 1, Types.VARCHAR);
st.setNull(index + 2, Types.VARCHAR);
}else{
String[] cols = (String[]) value;
st.setString(index, cols[0]);
st.setString(index + 1, cols[1]);
st.setString(index + 2, cols[2]);
}
}
...
..
..
DB script
Code:
CREATE TABLE A(
ID INTEGER NOT NULL,
A1 VARCHAR(20),
A2 VARCHAR(20),
A3 VARCHAR(20),
A4 VARCHAR(20),
A5 VARCHAR(20),
PRIMARY KEY(ID)
);
Name and version of the database you are using:
HSQLDB
The generated SQL (show_sql=true):
insert into A (a1, a2, A3, A4, A5, id) values ('1', '2', '', '', '', 3)
update A set A3='6', A4='7', A5='8' where id=3