Wednesday, 6 June 2012

ZK MVVM CRUD - Part 2


Previous Part 1




In the part 1, we have listed all the records from the DB.
I would like to do some changes in the Part 1 without adding any other stuff.  Open PracticeDAO. Java and let us have a close look on the following line;
Query q1 = session.createQuery("from Practice ");
Here, we did not specify any columns, so it will fill up the values for all the columns matching with our java bean. But in the list, we need only to display four columns such as Practice Name, city, State and Zip Code.
So how we can retrieve only selected columns in the create query ?  You can see this example to achieve the result.
So as per that example, first let us constructors in our practice.java as shown here.
package domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "practice")
public class Practice {

    private Integer practiceID;
    private String practiceName;
    private String city;
    private String ZipCode;
    private String state;
    private Integer isActive; 


    // Our default Constructor
    public Practice() {

    }

    // constructor with fields needed for our listing
    public Practice(Integer pID, String prname, String pCity, String pState, String pZipCode, Integer pIsActive ) {
        
        this.practiceID = pID;
        this.practiceName = prname;
        this.city = pCity;
        this.state = pState;
        this.ZipCode = pZipCode;
        this.isActive = pIsActive;
    }

    @Id
    @GeneratedValue
    @Column(name = "PRACTICE_ID")
    public Integer getPracticeID() {
        return practiceID;
    }

    public void setPracticeID(Integer practiceID) {
        this.practiceID = practiceID;
    }

    @Column(name = "PRACTICE_NAME")
    public String getPracticeName() {
        return practiceName;
    }

    public void setPracticeName(String practiceName) {
        this.practiceName = practiceName;
    }

    @Column(name = "CITY")
    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Column(name = "POSTAL_CODE")
    public String getZipCode() {
        return ZipCode;
    }

    public void setZipCode(String zipCode) {
        ZipCode = zipCode;
    }

    @Column(name = "STATE_PROVINCE_GEO")
    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    @Column(name = "IS_ACTIVE")
    public Integer getIsActive() 
    {
        if (isActive== null)
            return 1;
        else
            return isActive;
    }

    public void setIsActive(Integer isActive) {
        this.isActive = isActive;
    }

}



Now let us change our DAO as follows

Query q1 = session.createQuery("Select new Practice(practiceID, practiceName,city,state,zipCode,isActive) from Practice ");


Next Part 3


No comments:

Post a Comment