Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Can someone show me how to get past a BeanCreationException?

I am getting a BeanCreationException after adding two variables to Owner.java as follows:

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

//I added the following two variable declarations
@Transient
private Set<Pet> cats = new HashSet<Pet>();

@Transient
private Set<Pet> dogs = new HashSet<Pet>();

I also added getter and setter methods for cats and dogs, along with a method for populating cats and dogs as subsets of pets as follows:

public void parsePets() {
    for (Pet pet : getPetsInternal()) {
    if (pet.getType().getName().equals("cat")) {cats.add(pet);} 
    else if (pet.getType().getName().equals("dog")) {dogs.add(pet);}
  }
}

protected Set<Pet> getPetsInternal() {
    if (this.pets == null) {this.pets = new HashSet<Pet>();}
    return this.pets;
}

The application is failing to initialize when I do Run As...Run on Server within eclipse, and is giving the following error message:

org.springframework.beans.factory.BeanCreationException:  
Error creating bean with name  
'org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor#0'  
defined in class path resource [spring/business-config.xml]: Initialization of bean failed;  
nested exception is org.springframework.beans.factory.BeanCreationException:  
Error creating bean with name 'entityManagerFactory' defined in class path resource  
[spring/business-config.xml]: Invocation of init method failed;  
nested exception is javax.persistence.PersistenceException:  
[PersistenceUnit: petclinic] Unable to build EntityManagerFactory  

...  

Caused by: org.hibernate.MappingException:  
Could not determine type for:  
java.util.Set, at table: owners, for columns:  
[org.hibernate.mapping.Column(cats)]

Here is the code for business-config.xml.

I can eliminate the error message and get the application to run by commenting out the changes, but then I am left with the problem that the three lists (pets, cats, dogs) are identical, when I need cats and dogs to each be different subsets of pets. Here is the code which eliminates the error message but creates three identical lists which should not be identical:

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

//I added next two variables
//    @Transient
@OneToMany(cascade = CascadeType.ALL, mappedBy = "owner", fetch=FetchType.EAGER)
private Set<Pet> cats;// = new HashSet<Pet>();

//    @Transient  
@OneToMany(cascade = CascadeType.ALL, mappedBy = "owner", fetch=FetchType.EAGER)  
private Set<Pet> dogs;// = new HashSet<Pet>();   

Per axiopisty's request, I cannot really comment out everything except the addition of cats and dogs because pets, cats and dogs are called from OwnerController.java as follows:

@RequestMapping(value = "/owners", method = RequestMethod.GET)
public String processFindForm(@RequestParam("ownerID") String ownerId, Owner owner, BindingResult result, Map<String, Object> model) {
    Collection<Owner> results = this.clinicService.findOwnerByLastName("");
    model.put("selections", results);
    int ownrId = Integer.parseInt(ownerId);
    Owner sel_owner = this.clinicService.findOwnerById(ownrId);//jim added this
    sel_owner.parsePets();
    model.put("sel_owner",sel_owner);
    return "owners/ownersList";
}  

EDIT:

As per Sotirios' request, here is my entity class, Owner.java:

@Entity
@Table(name = "owners")
public class Owner extends Person {
    @Column(name = "address")
    @NotEmpty
    private String address;

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

    @Column(name = "telephone")
    @NotEmpty
    @Digits(fraction = 0, integer = 10)
    private String telephone;

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

    //I added next two variables
    @Transient
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner", fetch=FetchType.EAGER)
    private Set<Pet> cats = new HashSet<Pet>();

    @Transient
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "owner", fetch=FetchType.EAGER)
    private Set<Pet> dogs = new HashSet<Pet>();
    //end of 2 variables I added

    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;}

    // Call this from OwnerController before returning data to page.
    public void parsePets() {
        for (Pet pet : getPetsInternal()) {
            if (pet.getType().getName().equals("cat")) {
                cats.add(pet);
                System.out.println(pet.getType().getName());
                System.out.println("cats.size() is: "+cats.size());
                System.out.println("added a cat to cats");
            } 
            else if (pet.getType().getName().equals("dog")) {
                  dogs.add(pet);
                System.out.println(pet.getType().getName());
                System.out.println("dogs.size() is: "+dogs.size());
                System.out.println("added a dog to dogs");
            }
            // add as many as you want
    System.out.println("----------------------------------------------");
        }
    }

    public Set<Pet> getCats() {
        System.out.println("about to return cats");
        for (Pet cat : cats) {System.out.println("counting a "+cat.getType()+" in cats.");}
        System.out.println("cats.size() is: "+cats.size());
        return cats;
    }

    public Set<Pet> getDogs() {
        System.out.println("about to return dogs");
        for (Pet dog : dogs) {System.out.println("counting a "+dog.getType()+" in dogs.");}
        System.out.println("dogs.size() is: "+dogs.size());
        return dogs;
    }

    //end section I added

    protected Set<Pet> getPetsInternal() {
        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));
        return Collections.unmodifiableList(sortedPets);
    }

    public void addPet(Pet pet) {
        getPetsInternal().add(pet);
        pet.setOwner(this);
    }

    public Pet getPet(String name) {return getPet(name, false);}

    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();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
167 views
Welcome To Ask or Share your Answers For Others

1 Answer

Why do the seperate lists have to be instance variables?! Why not simply create a getCats method (and the others) and simply filter the pets collection? Trying to map everything makes, imho, things overly complex.

@Entity
@Table(name = "owners")
public class Owner extends Person {

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

    public Set<Pet> getCats() {
        Set<Pet> cats = new HashSet<Pet>();
        for (Pet pet : getPetsInternal()) {
            if (pet.getType().getName().equals("cat")) {
                cats.add(pet);
            }
        }
        return cats;
    }
}

Drawback is that the collection is recreated every time when needed. You can ease this with something like Google Guava and create a filter list.

@Entity
@Table(name = "owners")
public class Owner extends Person {

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

    public Set<Pet> getCats() {
        return Sets.filter(getPetsInternal(), new Predicate<Pet>() {
            public boolean apply(Pet pet) { 
                return pet.getType().getName().equals("cat")
            }
        });         
    }
}

Which you could also do inside your parsePets method and annotate it with @PostLoad so that that method would be invoked after the owner whas retrieved from the database.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...