我对3个实体的关系存在问题。我试图有一个继承heriarchy但在子类中我引用了我的Parent类对象的List作为属性。所以我希望我的子类实体成为一个映射器表,其中显示了它与它的父节点之间的关联。在这样做时,我遇到了一堆关系问题 我已经创建了一个模拟功能的示例项目。
Automobile.java
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.OneToOne;
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
public abstract class Automobile implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long Id;
@OneToOne
private Manufacturer manufacturer;
@OneToOne
private AutomobileType autotype;
@OneToOne
private LoanerCar loanerCarID;
private String vin;
//Getters and Setters, Arg Constructors, No-Arg Constructor, toString
}
LoanerCar.java
package com.example.demo.domains;
import java.sql.Timestamp;
import java.util.List;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
@Entity
public class LoanerCar extends Automobile{
private static final long serialVersionUID = 1L;
private String name;
private Timestamp dateLoaned;
private Timestamp dateReceived;
@OneToMany
private List<Automobile> alist;
//Getters and Setters, Arg Constructors, No-Arg Constructor, toString
}
TotaledCar.java
import java.util.List;
import javax.persistence.Entity;
@Entity
public class TotaledCar extends Automobile {
private static final long serialVersionUID = 1L;
private List<LoanerCar> eligibleLoaners;
// Getters and Setters,Arg构造函数,No-Arg构造函数,toString }
AutomobileBaseRepository.java
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.NoRepositoryBean;
import com.example.demo.domains.Automobile;
import com.example.demo.domains.Manufacturer;
@NoRepositoryBean
public interface AutomobileBaseRepository<T extends Automobile> extends JpaRepository<T, Long> {
List<T> findByLoanerCarID(Long id);
List<T> findByVinAndManufacturerAndLoanerCarID(String vin, Manufacturer manfacturer, Long id);
}
Automobile,TotaledCar,LoanerCar存储库扩展了AutomobileBaseRespoistory,并且没有方法。
LoaanerController.java
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.domains.LoanerCar;
import com.example.demo.domains.Manufacturer;
import com.example.demo.domains.TotaledCar;
import com.example.demo.repositories.LoanerCarRepository;
import com.example.demo.repositories.ManufacturerRepository;
import com.example.demo.repositories.TotaledCarRepository;
@RestController
public class LoanerController {
@Autowired
LoanerCarRepository loanerRepo;
@Autowired
TotaledCarRepository totaledCarRepo;
@Autowired
ManufacturerRepository manRepo;
@RequestMapping(value="/loaner/all", method = RequestMethod.GET)
public List<LoanerCar> loaners(){
List<LoanerCar> list = loanerRepo.findAll();
return list;
}
@RequestMapping(value="/loaner/{id}", method=RequestMethod.GET)
public LoanerCar getLoaner(@PathVariable Long id){
LoanerCar loanerCar= loanerRepo.findOne(id);
return loanerCar;
}
@RequestMapping(value="/loaner", method=RequestMethod.GET)
public String response(@RequestParam("vin") String vin, @RequestParam("manufacturer") Long manfuacturerId, @RequestParam("loanerId") Long loanerId){
Manufacturer manObj = manRepo.findById(manfuacturerId);
List<LoanerCar> loanerList = loanerRepo.findByLoanerCarID(loanerId);
List<TotaledCar> tlist = totaledCarRepo.findByVinAndManufacturerAndLoanerCarID(vin, manObj, loanerId);
for (TotaledCar junk : tlist){
loanerList.addAll(junk.getEligibleLoaners());
}
String strlist = loanerList.toString();
return strlist;
}
}
因此,这个映射的流程应该是从我添加汽车时开始,默认情况下它的loanerId为null。当我为loanerId赋值时,它被映射到LoanerCar表,其中有一个来自父类/表的AutomobileId,而loanerId驻留在LoanerCar类/表中。因此,在LoanerCar表中,loanerId绑定到符合LoanerCars条件的汽车列表。所以汽车有一个loanerId,那个loanerId可以很多汽车。我的控制器方法只会给我一个总计和租车的列表。我知道这听起来令人困惑,但我知道这就是我想要实现的目标。
堆栈跟踪
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:856) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:314) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1162) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1151) [spring-boot-1.5.3.RELEASE.jar:1.5.3.RELEASE]
at com.example.demo.SpringJpaInheritanceRelationshipsApplication.main(SpringJpaInheritanceRelationshipsApplication.java:10) [classes/:na]
Caused by: javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:954) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:882) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60) ~[spring-orm-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353) ~[spring-orm-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370) ~[spring-orm-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359) ~[spring-orm-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
... 16 common frames omitted
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.List, at table: totaled_car, for columns: [org.hibernate.mapping.Column(eligible_loaners)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:431) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:398) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.mapping.Property.isValid(Property.java:225) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:595) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.mapping.JoinedSubclass.validate(JoinedSubclass.java:44) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.internal.MetadataImpl.validate(MetadataImpl.java:329) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:443) ~[hibernate-core-5.0.12.Final.jar:5.0.12.Final]
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:879) ~[hibernate-entitymanager-5.0.12.Final.jar:5.0.12.Final]
... 22 common frames omitted
答案 0 :(得分:0)