-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: How to do derived properties correctly?
PostPosted: Sun Jan 09, 2005 10:22 pm 
Beginner
Beginner

Joined: Fri Jan 07, 2005 11:07 am
Posts: 30
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();
}
}


Top
 Profile  
 
 Post subject: How to do derived properties correctly?
PostPosted: Mon Jan 10, 2005 10:46 am 
Beginner
Beginner

Joined: Fri Jan 07, 2005 11:07 am
Posts: 30
Do I have to create a trigger on the MS SQL table? If yes, then
what is the point specifying the formula in the mapping file?
Can Hibernate ask the MS SQL server to create a trigger automatically
based on the mapping file?

Hibernate is new to me. Can somebody help me out here?

Thanks!

Pete


Top
 Profile  
 
 Post subject: How to do derived properties correctly?
PostPosted: Mon Jan 10, 2005 3:13 pm 
Beginner
Beginner

Joined: Fri Jan 07, 2005 11:07 am
Posts: 30
Is my question too simple and stupid or too hard to answer?

What is the correct way to do derived properties on the database
side and java code side?

Somebody helps, please...

Thanks!


Top
 Profile  
 
 Post subject: * How to do derived properties correctly? *
PostPosted: Mon Jan 10, 2005 11:30 pm 
Beginner
Beginner

Joined: Fri Jan 07, 2005 11:07 am
Posts: 30
Somebody helps, please...

Thanks!


Top
 Profile  
 
 Post subject: How to do derived properties correctly? ? ?
PostPosted: Tue Jan 11, 2005 10:27 pm 
Beginner
Beginner

Joined: Fri Jan 07, 2005 11:07 am
Posts: 30
can somebody shed some light?

thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 12, 2005 7:26 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Actually, hibernate did not read the DB while saving. Consider using refresh()

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.