Powered By Blogger

Tuesday, December 3, 2013

Hibernate Composite Key Mapping (JPA)

   It is sometimes necessary to have composite keys in a database table. In such a scenario, it would be necessary to get hibernate to cater this requirement. This is an illustration of how it could be done.
   Consider an entity, BankFacilityAgent which has percentage and isKeyFacilitator as attributes and another attribute, key of type BankFacilityAgentKey. The class is annotated with @Table and @Entity. The class is bound to a database table, BANK_FAC_AGENT. Getters of attributes percentage and isKeyFacilitator are annotated with @javax.persistence.Column and @Basic. Note that getter of 'key' attribute is annotated with @Id.

===============================================================

package com.shyarmal.persistance.domain;

import javax.persistence.Basic;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Table(name = "BANK_FAC_AGENT", catalog = "")
@Entity
public class BankFacilityAgent {

    private BankFacilityAgentKey key;
    private float percentage;
    private String isKeyFacilitator;

    @Id
    public BankFacilityAgentKey getKey() {
        return key;
    }

    public void setKey(BankFacilityAgentKey key) {
        this.key = key;
    }

    @javax.persistence.Column(name = "PERCENTAGE")
    @Basic
    public float getPercentage() {
        return percentage;
    }

    public void setPercentage(float percentage) {
        this.percentage = percentage;
    }

    @javax.persistence.Column(name = "KEY_FACILITATOR")
    @Basic
    public String getKeyFacilitator() {
        return isKeyFacilitator;
    }

    public void setKeyFacilitator(String keyFacilitator) {
        isKeyFacilitator = keyFacilitator;
    }
}


===============================================================


   BankFacilityAgentKey represents the composite key, which should implement Serializable and is annotated with @Embeddable. This class holds attributes that comprise the composite key (bankId, facilityAgentId). The getters of the attributes are annotated the same way as in the class above.

===============================================================

package com.shyarmal.persistance.domain;

import javax.persistence.Basic;
import javax.persistence.Embeddable;
import java.io.Serializable;

@Embeddable
public class BankFacilityAgentKey implements Serializable {

    private int bankId;
    private int facilityAgentId;

    public BankFacilityAgentKey(int bankId, int facilityAgentId) {
        this.bankId = bankId;
        this.facilityAgentId = facilityAgentId;
    }

    public BankFacilityAgentKey() {

    }

    @javax.persistence.Column(name = "BANK_ID")
    @Basic
    public int getBankId() {
        return bankId;
    }

    public void setBankId(int bankId) {
        this.bankId = bankId;
    }

    @javax.persistence.Column(name = "FACILITY_AGENT_ID")
    @Basic
    public int getFacilityAgentId() {
        return facilityAgentId;
    }

    public void setFacilityAgentId(int facilityAgentId) {
        this.facilityAgentId = facilityAgentId;
    }
}

===============================================================

   The database table mapped is BANK_FAC_AGENT with columns PERCENTAGE, KEY_FACILITATOR, BANK_ID and FACILITY_AGENT_ID where BANK_ID and FACILITY_AGENT_ID are the composite key.


thanks,
Shyarmal.

Friday, November 29, 2013

Enable/ Disable Link Using jQuery


Consider a link by the id, 'link1'.
<a href="abc.htm" id="link1">

It can be disabled using jquery .
$("#link1").css({'pointer-events':'none', 'cursor':'default'});

Disabled link can be enabled by removing the css properties. 
$("#link1").css({'pointer-events':'', 'cursor':''});

thanks,
Shyarmal

Thursday, November 28, 2013

jQuery Popup Dialog



    This example shows how to prompt a pop-up with jQeury when an icon is clicked. Following is the HTML code required for this exercise. An image link is created with the id, 'hint' (, which does not link to any page). Then a div element is defined with the 'dialog' as the id. This is the dialog that would be displayed when the image icon is clicked. The dialog will have a title, 'Bank Code' and a text, 'Abbreviation of the bank name.'. Its dimensions are defined in the style attribute (height and width). Since the div should not be displayed on the html page, it has been made invisible by setting the 'display' property to 'none' in style attribute.

================================================================
<a href="#" id="hint"><img src="images/information-icon.gif" align="top"> </a>


<div id="dialog" title="Bank Code" style="display:none; height: 75mm; width: 300mm;">
    <p>Abbreviation of the bank name.</p>
</div>

================================================================

Binding of click event of the link (referred to by 'hint') should be done in jQuery ready function. Click event of the link is captured and the display property of the div, 'dialog' is set to 'block'. Then prompting of the dialog is done by calling '$( "#dialog" ).dialog();'.


================================================================
$(function() {
     $("#hint").click(function () {
        $( "#dialog").css("display", "block");
        $( "#dialog" ).dialog();
    });
});

================================================================


screen shot of the prompt:


thanks, 
Shyarmal