有人可以告诉我如何摆脱BeanCreationException吗?
在向Owner.java中添加两个变量之后,我得到了BeanCreationException,如下所示:
@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>();
我还添加了用于猫和狗的getter和setter方法,以及将猫和狗作为宠物子集填充的方法,如下所示:
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;
}
当我在Eclipse中在服务器上运行方式为…运行时,应用程序无法初始化,并显示以下错误消息:
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)]
这是business-config.xml的代码。
我可以消除错误消息,并通过注释掉所做的更改来使应用程序运行,但是当我需要猫和狗各不相同时,我剩下的问题是三个列表(宠物,猫,狗)是相同的宠物的子集。下面的代码消除了错误消息,但创建了三个不相同的相同列表:
@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>();
根据公理会的要求,除了添加猫和狗之外,我无法真正注释所有内容,因为宠物,猫和狗是从OwnerController.java中调用的,如下所示:
@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";
}
根据Sotirios的请求,这是我的实体类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();
}
}
为什么单独的列表必须是实例变量?!为什么不简单地创建一个getCats
方法(和其他方法)并简单地过滤pets
集合呢?试图映射所有内容,恕我直言,事情变得过于复杂。
@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;
}
}
缺点是每次需要时都会重新创建集合。您可以使用Google Guava之类的工具来简化此操作,并创建一个过滤器列表。
@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")
}
});
}
}
您还可以在parsePets
方法内部进行操作并使用它进行注释,@PostLoad
以便在所有者从数据库中检索到该方法之后将调用该方法。
问题内容: 在我的PHP应用程序中,我想计算两个时间变量的总和。我正在寻找类似这个例子的东西。 问题答案: 最好的方法是最有可能使用strtotime将其转换为时间戳,然后将它们加在一起: 如果我还记得的话,确实支持这种格式。 否则,您将需要自己过滤掉它。
问题内容: 假设我有很多字符串变量(例如100): 我想将这些String变量添加到ArrayList中,我现在正在做的是 我很好奇,有没有办法使用循环?例如。 问题答案: 使用数组: 这个想法非常流行,以至于有内置的方法可以做到这一点。例如: 会将您的数组元素添加到现有列表中。如果只需要一个仅包含数组元素的列表,则可以一行执行:
问题内容: 我有两个数字。例如: 为什么数字不支持算术运算?无论如何,我将如何在Java中将这两个数字相加?(当然,我是从某个地方获取它们的,我不知道它们是Integer还是float等)。 问题答案: 你不知道数字是整数还是浮点数…使用该类时,编译器也不知道你的数字是整数,浮点数还是其他东西。结果,像+和-这样的基本数学运算符不起作用;计算机将不知道如何处理这些值。 开始编辑 根据讨论,我认为一
问题内容: 是否可以在重定向中添加GET变量?(无需修改我的urls.py) 如果我做 我懂了 我没有抱怨,而是只是想知道… 问题答案: 是否可以在重定向中添加GET变量?(无需修改我的urls.py) 不修改,我不知道有什么办法。 我没有使用HttpResponseRedirect(’/ my_long_url /%s /?q = something’,x)的抱怨,但是只是想知道… 你可能需要编
@PostMapping(“/api/v1.0/{username}/forgot”)是一个公共的void forgotPassword(@PathVariable String username,@RequestBody String enteredPassword,@request body strig reenterPassword){userServiceImpl.forgotPasswo
到目前为止,我有以下代码: 当我运行这个脚本并向它传递一个参数时,我得到一个错误:“./adddir:line 3:[[1:command not found./adddir:line 3:[1:command not found” 当我使用2个参数而不是“1”运行它时,它会显示为“2” 有事吗?