通过hibernate从db获取值后,在POJO类中设置值

时间:2014-01-19 20:23:33

标签: java hibernate java-ee orm

我正在开发一个hibernate应用程序,其中在DAO中我运行以下查询

@SuppressWarnings("unchecked")
    public Collection<Owner> findOwners(String lastName) {
        return sessionFactory.getCurrentSession().createQuery("from Owner owner where owner.lastName like :lastName")
                .setString("lastName", lastName + "%").list();
    }

从数据库中获取所有者并显示在jsp中。

我的POJO课程

@Entity
@Table(name="OWNERS")
public class Owner extends Person implements Serializable{

    @Column(name="ADDRESS")
    private String address;

    @Column(name="CITY")
    private String city;

    @Column(name="TELEPHONE")
    private String telephone;

    @OneToMany(fetch=FetchType.LAZY, mappedBy="owner", cascade=CascadeType.ALL)
    private Set<Pet> pets;


    public String getAddress() {
        return this.address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCity() {
        return this.city;
    }

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

    public String getTelephone() {
        return this.telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    protected void setPetsInternal(Set<Pet> pets) {
        this.pets = pets;
    }

    protected Set<Pet> getPetsInternal() {

    //A null should not be returned; if returned will throw a NullPointerException
        if (this.pets == null) {
            this.pets = new HashSet<Pet>();
        }
        return this.pets;
    }



    public List<Pet> getPets() {
        List<Pet> sortedPets = new ArrayList<Pet>(getPetsInternal());


        PropertyComparator.sort(sortedPets, new MutableSortDefinition("name", true, true));
        //returns an immutable list of sortedPets
        return Collections.unmodifiableList(sortedPets);
    }

    /*public void addPet(Pet pet) {
        getPetsInternal().add(pet);
        pet.setOwner(this);
    }
*/
    /**
     * Return the Pet with the given name, or null if none found for this Owner.
     *
     * @param name to test
     * @return true if pet name is already in use
     */
    public Pet getPet(String name) {
        return getPet(name, false);
    }

    /**
     * Return the Pet with the given name, or null if none found for this Owner.
     *
     * @param name to test
     * @return true if pet name is already in use
     */
    public Pet getPet(String name, boolean ignoreNew) {
        name = name.toLowerCase();
        for (Pet pet : getPetsInternal()) {
            if (!ignoreNew || !pet.isNew()) {
                String compName = pet.getName();
                compName = compName.toLowerCase();
                if (compName.equals(name)) {
                    return pet;
                }
            }
        }
        return null;
    }

    @Override
    public String toString() {
        return new ToStringCreator(this)

        .append("id", this.getId())

        .append("new", this.isNew())

        .append("lastName", this.getLastName())

        .append("firstName", this.getFirstName())

        .append("address", this.address)

        .append("city", this.city)

        .append("telephone", this.telephone)

        .toString();
    }
}

我的问题可能看起来有些古怪,我知道在jsp中,EL调用了get *()方法,但是所谓的Owner类的set *()方法在哪里?如何在POJO中设置值?

通过休眠在内部在POJO中设置从db中获取后的值是什么?

0 个答案:

没有答案