Hello,
If there is an empty setter method in a mapped class then on issuing a select (fetch all) hibernate executes an update - on the column, or delete on a table depending on the mapping.
a) Can anyone tell me why exactly this is happening?
(I am not new to hibernate but I am not well versed with it either.)
b) Can hibernate be configured ('fixed') not to do this?
c) Should this be reported as a bug?
Below is the 'Person' class and its mapping.
The 'Address' class and its mapping is not included but for those who would like to reproduce it, the generator mapping for the 'Address' was 'assigned'.
Code:
package hib.example.model.pojo.bean;
import java.util.Set;
public class Person {
private long slNo;
private String name;
private int age;
private Address address;
private Set<Address> addresses;
public long getSlNo() {
return slNo;
}
public void setSlNo(long slNo) {
this.slNo = slNo;
}
public String getName() {
return name;
}
public void setName(String name) {
/*
* comment below code for recreating - update on select - behaviour
*/
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public Set<Address> getAddresses() {
return addresses;
}
public void setAddresses(Set<Address> addresses) {
/*
* comment below code for recreating - delete on select - behaviour
*/
this.addresses = addresses;
}
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="hib.example.model.pojo.bean">
<class name="Person" table="PERSON">
<id name="slNo" column="sl_no">
<generator class="increment"/>
</id>
<!-- <property update="false" name="name" column="name"/>-->
<property name="name" column="name"/>
<property name="age" column="age"/>
<one-to-one name="address" class="hib.example.model.pojo.bean.Address"
cascade="save-update" fetch="join" />
<set name="addresses" table="PersonAddress">
<key column="person_id"/>
<many-to-many column="address_id"
unique="true"
class="hib.example.model.pojo.bean.Address"/>
</set>
</class>
</hibernate-mapping>
In any case I hope this helps some one!
Regards
Ravi