-->
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.  [ 10 posts ] 
Author Message
 Post subject: hbm2java with bound properties
PostPosted: Sun Dec 21, 2008 4:13 pm 
Newbie

Joined: Sun Dec 21, 2008 4:01 pm
Posts: 4
Hello everyone,

I have recently customised the templates for generating POJOs with hbm2java to also generate bound properties and add PropertyChangeSupport instance to my POJOs. What worries me is that I really could not find much information about people doing stuff like this so my question to the hibernate community is: Are you designing your domain objects with bound properties?

Kind Regards,
Erik

_________________
Be the first to read my blog!
http://blogs.willcodejavaforfood.com


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 22, 2008 4:55 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
I don't since I find most of the time the model is more strict than I really would like it to be for an ui where propertychange listeners are the most useful - but there have been others that did it for the older hibernate tools but the change did not make it over since it was not easily moved from velocity to freemarker.

If you have done it how about contributed your fix ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 22, 2008 1:59 pm 
Newbie

Joined: Sun Dec 21, 2008 4:01 pm
Posts: 4
Certainly,

It might need some more work though. Let me try and outline what I have done first and I'll have a chance to do some enhancements if necessary.

I have a class level meta attribute "property-change-support" which will add the field propertyChangeSupport of the type java.beans.PropertyChangeSupport. It will also add the following methods:

Code:
public void firePropertyChange(String property, Object oldValue, Object newValue);

public void addPropertyChangeListener(String property, java.beans.PropertyChangeListener listener);

public void removePropertyChangeListener(String property, java.beans.PropertyChangeListener listener);


Any property with the following meta attribute "bound-property" will have a constant generated and its set method modified to something along the line of:

Code:
public static final String CREDIT_PROPERTY = "credit";

public void setCredit(double credit) {
        double old = this.credit;
        this.credit = credit;
        firePropertyChange("credit", Double.valueOf(old), Double.valueOf(this.credit));
    }


I have no support for indexed properties and at the moment you have to specify which properties you want to be bound.

_________________
Be the first to read my blog!
http://blogs.willcodejavaforfood.com


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 22, 2008 5:18 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
in the old days meta could be bound and constrained to indicate the type of property change listener the user wanted.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 22, 2008 6:44 pm 
Newbie

Joined: Sun Dec 21, 2008 4:01 pm
Posts: 4
max wrote:
in the old days meta could be bound and constrained to indicate the type of property change listener the user wanted.


You mean, bound, vetoable, indexed etc?

_________________
Be the first to read my blog!
http://blogs.willcodejavaforfood.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 24, 2008 6:27 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
yes - thats what i recall from reading the old hibernate 2 docs.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 24, 2008 8:36 am 
Newbie

Joined: Sun Dec 21, 2008 4:01 pm
Posts: 4
Do you think it would make more sense to have all properties bound instead of doing them individually?

_________________
Be the first to read my blog!
http://blogs.willcodejavaforfood.com


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 25, 2008 5:11 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
meta attributes is inherited by default so if you add <meta attribute="bound"/> at the class level every property will pick that up making them all bound.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Re: hbm2java with bound properties
PostPosted: Thu Sep 08, 2011 6:29 am 
Newbie

Joined: Sat Sep 03, 2011 10:16 am
Posts: 10
If this feature used to exist in the old version, why has it been removed?

It's a really useful feature to have, for anyone doing DataBinding on the persistent objects.


Top
 Profile  
 
 Post subject: Re: hbm2java with bound properties
PostPosted: Thu Sep 08, 2011 10:21 am 
Newbie

Joined: Sat Sep 03, 2011 10:16 am
Posts: 10
willcodejavaforfood decided not to publish any source code, so I will post it here for posterity.

To use it simply add <meta attribute="property-change-support">true</meta> under your <class>.
And to add fields that fire the event, simply add <meta attribute="bound">FIELD_PROPERTY</meta> under the needed <property>.

FIELD_PROPERTY should be the unique name given to this property, and it will be exposed as a static string.

Pojo.ftl
Code:
${pojo.getPackageDeclaration()}
// Generated ${date} by Hibernate Tools ${version}

<#assign classbody>
<#include "PojoTypeDeclaration.ftl"/> {

<#if !pojo.isInterface()>
<#include "PojoFields.ftl"/>

<#include "PojoConstructors.ftl"/>
   
<#include "PojoPropertyAccessors.ftl"/>

<#include "PojoToString.ftl"/>

<#include "PojoEqualsHashcode.ftl"/>

<#else>
<#include "PojoInterfacePropertyAccessors.ftl"/>

</#if>
<#include "PojoExtraClassCode.ftl"/>

}
</#assign>

${pojo.generateImports()}
<#if pojo.hasMetaAttribute("property-change-support")>
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
</#if>
${classbody}


PojoFields.ftl
Code:
<#-- // Fields -->

<#foreach field in pojo.getAllPropertiesIterator()><#if pojo.getMetaAttribAsBool(field, "gen-property", true)> <#if pojo.hasMetaAttribute(field, "field-description")>    /**
     ${pojo.getFieldJavaDoc(field, 0)}
     */
</#if>    ${pojo.getFieldModifiers(field)} ${pojo.getJavaTypeName(field, jdk5)} ${field.name}<#if pojo.hasFieldInitializor(field, jdk5)> = ${pojo.getFieldInitialization(field, jdk5)}</#if>;
</#if>
</#foreach>

<#if pojo.hasMetaAttribute("property-change-support")>
    // Property Change support
    <#foreach property in pojo.getAllPropertiesIterator()> 
    <#if pojo.hasMetaAttribute(property, "bound")>
    public static final String ${c2j.getMetaAsString(property, "bound")} = "${property.name}";
    </#if> 
    </#foreach>
   
    private PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
   
    public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
        propertyChangeSupport.addPropertyChangeListener(propertyName, listener);
     }

     public void removePropertyChangeListener(PropertyChangeListener listener) {
        propertyChangeSupport.removePropertyChangeListener(listener);
     }   
</#if>


PojoPropertyAccessors.ftl
Code:
<#-- // Property accessors -->
<#foreach property in pojo.getAllPropertiesIterator()>
<#if pojo.getMetaAttribAsBool(property, "gen-property", true)>
<#if pojo.hasFieldJavaDoc(property)>   
    /**       
     * ${pojo.getFieldJavaDoc(property, 4)}
     */
</#if>
    <#include "GetPropertyAnnotation.ftl"/>
    ${pojo.getPropertyGetModifiers(property)} ${pojo.getJavaTypeName(property, jdk5)} ${pojo.getGetterSignature(property)}() {
        return this.${property.name};
    }
   
    ${pojo.getPropertySetModifiers(property)} void set${pojo.getPropertyName(property)}(${pojo.getJavaTypeName(property, jdk5)} ${property.name}) {
     <#if pojo.hasMetaAttribute(property, "bound")>
       <#assign oldVal = "this.${property.name}">
       <#assign newVal = "this.${property.name} = ${property.name}">
       <#if pojo.getJavaTypeName(property, jdk5) == "byte">
         <#assign oldVal = "new Byte(this.${property.name})">
         <#assign newVal = "new Byte(this.${property.name} = ${property.name})">
       <#elseif  pojo.getJavaTypeName(property, jdk5) == "short">
         <#assign oldVal = "new Short(this.${property.name})">
         <#assign newVal = "new Short(this.${property.name} = ${property.name})">
       <#elseif  pojo.getJavaTypeName(property, jdk5) == "int">
         <#assign oldVal = "new Integer(this.${property.name})">
         <#assign newVal = "new Integer(this.${property.name} = ${property.name})">
       <#elseif  pojo.getJavaTypeName(property, jdk5) == "long">
         <#assign oldVal = "new Long(this.${property.name})">
         <#assign newVal = "new Long(this.${property.name} = ${property.name})">
       <#elseif  pojo.getJavaTypeName(property, jdk5) == "float">
         <#assign oldVal = "new Float(this.${property.name})">
         <#assign newVal = "new Float(this.${property.name} = ${property.name})">
       <#elseif  pojo.getJavaTypeName(property, jdk5) == "double">
         <#assign oldVal = "new Double(this.${property.name})">
         <#assign newVal = "new Double(this.${property.name} = ${property.name})">
       <#elseif  pojo.getJavaTypeName(property, jdk5) == "char">
         <#assign oldVal = "new Character(this.${property.name})">
         <#assign newVal = "new Character(this.${property.name} = ${property.name})">
       <#elseif  pojo.getJavaTypeName(property, jdk5) == "boolean">
         <#assign oldVal = "new Boolean(this.${property.name})">
         <#assign newVal = "new Boolean(this.${property.name} = ${property.name})">
       </#if>
        propertyChangeSupport.firePropertyChange(${c2j.getMetaAsString(property, "bound")}, ${oldVal}, ${newVal});
     <#else>
        this.${property.name} = ${property.name};
     </#if>
    }
</#if>
</#foreach>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 10 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.