Hi, I get stuck with derived properities. I find there are few materials available and I have not found any examples.
I am using Hibernate 2.1.7 and Microsoft SQL Server.
I used net.sf.hibernate.tool.hbm2java.Hbm2JavaTaskto generate code
and net.sf.hibernate.tool.hbm2ddl.SchemaExportTask to generate
schema. I wrote the testing code.
I never see the derived properity has a correct value. In the
example below, I hope to see 9 assigned to the derived property *total*,
but it is always 0.0 before and after save or commit. Why?
What is the correct way to do a derived property?
What went wrong?
Thanks a lot!
Pete
The following are 3 complete files:
hibernate mapping file, generated java class file, and the testing code.
----------------------------------------------------------------
Here is the complete mapping file:
----------------------------------------------------------------
<?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="DerivedProperty" table="DERIVED_TABLE">
<meta attribute="implement-equals">true</meta>
<id name="id" type="long" column="ID">
<meta attribute="scope-set">private</meta>
<generator class="native"/>
</id>
<property name="quantity" type="integer" not-null="true">
<meta attribute="use-in-tostring">true</meta>
<column name="QUANTITY"/>
</property>
<property name="total"
formula="QUANTITY * 3" type="double" not-null="true">
<meta attribute="use-in-tostring">true</meta>
</property>
</class>
</hibernate-mapping>
----------------------------------------------------------------
Here is the generated DerivedProperty.java:
----------------------------------------------------------------
import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
/** @author Hibernate CodeGenerator */
public class DerivedProperty implements Serializable {
/** identifier field */
private Long id;
/** persistent field */
private int quantity;
/** persistent field */
private double total;
/** full constructor */
public DerivedProperty(int quantity, double total) {
this.quantity = quantity;
this.total = total;
}
/** default constructor */
public DerivedProperty() {
}
public Long getId() {
return this.id;
}
private void setId(Long id) {
this.id = id;
}
public int getQuantity() {
return this.quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getTotal() {
return this.total;
}
public void setTotal(double total) {
this.total = total;
}
public String toString() {
return new ToStringBuilder(this)
.append("id", getId())
.append("quantity", getQuantity())
.append("total", getTotal())
.toString();
}
public boolean equals(Object other) {
if ( (this == other ) ) return true;
if ( !(other instanceof DerivedProperty) ) return false;
DerivedProperty castOther = (DerivedProperty) other;
return new EqualsBuilder()
.append(this.getId(), castOther.getId())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(getId())
.toHashCode();
}
}
-----------------------------------------------------
Here is the testing code:
----------------------------------------------------------------
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;
/**
*
*/
public class TestPDerivedProperty
{
public static void main(String args[]) throws Exception {
Configuration config = new Configuration();
config.addClass(DerivedProperty.class);
SessionFactory sessionFactory = config.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = null;
try {
tx = session.beginTransaction();
DerivedProperty obj = new DerivedProperty();
obj.setQuantity(3);
System.out.println("before save\n"+ obj);
session.save(obj);
System.out.println("after save\n"+ obj);
tx.commit();
System.out.println("after commit \n"+ obj);
} catch (Exception e) {
if (tx != null) {
tx.rollback();
}
throw e;
} finally {
session.close();
}
sessionFactory.close();
}
}
|