Hi!
We want to access properties of hibernate loaded entities through reflection and the appropriate hibernate ClassMetaData.
If we access a hibernate mapped set-property with access modifier
private through hibernates ClassMetaData.getPropertyValue(...) then we always get an empty set. If we change the access modifier of the property to
public we get the correct result. The entity we loaded is wrapped by cglibs proxy. Is this intendended behaviour? Do we have a lack of understanding reflection and proxy handling?
Below are details of our configuration:
Hibernate version: 3.2.4 SP1
Mapping documents:
Code:
<class name="Order" table="ORDERS" lazy="true">
<id name="id" column="ID">
<generator class="uuid.hex"/>
</id>
<set name="itemsInternal" inverse="true" cascade="all-delete-orphan" lazy="true">
<key column="ORDER_ID"/>
<one-to-many class="OrderItem"/>
</set>
</class>
<class name="OrderItem" table="ORDERITEM">
<id name="id" column="ID">
<generator class="uuid.hex"/>
</id>
<many-to-one name="Order" column="ORDER_ID" class="Order" cascade="none" />
</class>
Code between sessionFactory.openSession() and session.close():Code:
ClassMetadata metadata = factory.getClassMetadata(Order.class);
Order order = (Order) session.load(Order.class, id);
Object propertyValue = metadata.getPropertyValue(order, "itemsInternal", EntityMode.POJO);
System.out.println("expected=1, acutal=" + ((Collection) propertyValue).size());
Full stack trace of any exception that occurs:no exception occurs
Name and version of the database you are using:SQLExpress 2005, HSQLDB
The generated SQL (show_sql=true):This is the output when getItemsInternal has
private access modifier:
Code:
Hibernate: insert into ORDERS (ID) values (?)
Hibernate: insert into ORDERITEM (ORDER_ID, ID) values (?, ?)
expected=1, acutal=0
This is the output when getItemsInternal has
public access modifier:
Code:
Hibernate: insert into ORDERS (ID) values (?)
Hibernate: insert into ORDERITEM (ORDER_ID, ID) values (?, ?)
Hibernate: select order0_.ID as ID0_0_ from ORDERS order0_ where order0_.ID=?
Hibernate: select itemsinter0_.ORDER_ID as ORDER2_1_, itemsinter0_.ID as ID1_, itemsinter0_.ID as ID1_0_, itemsinter0_.ORDER_ID as ORDER2_1_0_ from ORDERITEM itemsinter0_ where itemsinter0_.ORDER_ID=?
expected=1, acutal=1
Debug level Hibernate log excerpt:NA
Order-class with private access modifier of getItemsInternal:Code:
public class Order {
private String id;
private Set<OrderItem> items;
public Order() {
items = new HashSet<OrderItem>();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public Set<OrderItem> getItems() {
return Collections.unmodifiableSet(getItemsInternal());
}
private Set<OrderItem> getItemsInternal() {
return items;
}
private void setItemsInternal(Set<OrderItem> items) {
this.items = items;
}
public void addItem(OrderItem item) {
if (items == null) {
setItemsInternal(new HashSet<OrderItem>());
}
getItemsInternal().add(item);
}
}
Order-class with public access modifier of getItemsInternal:Code:
public Set<OrderItem> getItemsInternal() {
return items;
}