【发布时间】:2018-06-22 20:14:04
【问题描述】:
意识到已经有很多关于这个的问题,我看不出我的具体案例的问题。我的应用程序中有另一个实例(工作正常),据我所知,我正在镜像配置。事实上,当我使用mvn: spring-boot:run 运行应用程序时,一切正常,所有数据都按预期找到。但是,当我尝试为应用程序运行测试时,任何使用
@RunWith(SpringRunner.class)
@DataJpaTest
public class TestClass {
@Autowired
private TestEntityManager em;
...
}
产生这个错误:
java.lang.IllegalStateException: 无法加载 ApplicationContext 引起:org.springframework.beans.factory.BeanCreationException: 创建类中定义的名称为“entityManagerFactory”的bean时出错 路径资源 [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: 调用 init 方法失败;嵌套异常是 org.hibernate.AnnotationException:使用@OneToMany 或@ManyToMany 针对未映射的类: com.utilities.domain.manufacturing.Machine.operators[com.humanresources.domain.MachineOperator] 引起:org.hibernate.AnnotationException:使用@OneToMany 或 @ManyToMany 针对未映射的类: com.utilities.domain.manufacturing.Machine.operators[com.humanresources.domain.MachineOperator]
诚然,我对配置不是很了解,但我不明白为什么一组类可以工作,但事实并非如此。以下是类(仅包含相关部分):
员工
@Entity
@Table(name="humanresources.employees")
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private int employeeID;
...
private List<MachineOperator> machines = new ArrayList<>();
public Employee() {}
@Id
@Column(name="pk_employeeid")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonView(View.SimpleEmployeeView.class)
public int getEmployeeID() {
return employeeID;
}
public void setEmployeeID(int employeeID) {
this.employeeID = employeeID;
}
...
@OneToMany(mappedBy="employee",cascade=CascadeType.ALL,orphanRemoval=true)
@JsonView(View.EmployeeView.class)
public List<MachineOperator> getMachines() {
return machines;
}
public void setMachines(List<MachineOperator> machines) {
this.machines = machines;
}
public void addMachine(Machine machine) {
MachineOperator machineOperator = new MachineOperator(this, machine);
this.machines.add(machineOperator);
machine.getOperators().add(machineOperator);
}
public void removeCompany(Machine machine) {
for (Iterator<MachineOperator> iterator = machines.iterator(); iterator.hasNext(); ) {
MachineOperator machineOperator = iterator.next();
if (machineOperator.getEmployee().equals(this) &&
machineOperator.getMachine().equals(machine)) {
iterator.remove();
machineOperator.getMachine().getOperators().remove(machineOperator);
machineOperator.setEmployee(null);
machineOperator.setMachine(null);
}
}
}
}
机器
@Entity
@Table(name="utilities.mnfg_machines")
public class Machine implements Serializable {
private static final long serialVersionUID = 1L;
private int machineID;
...
private List<MachineOperator> operators = new ArrayList<>();
public Machine() {}
@Id
@Column(name="pk_machineid")
@GeneratedValue(strategy = GenerationType.IDENTITY)
@JsonView({View.MachineView.class,View.DefaultMachineView.class})
public int getMachineID() {
return machineID;
}
public void setMachineID(int machineID) {
this.machineID = machineID;
}
...
@OneToMany(mappedBy="machine",orphanRemoval=true)
@JsonView({View.MachineView.class,View.DefaultMachineView.class})
public List<MachineOperator> getOperators() {
return operators;
}
public void setOperators(List<MachineOperator> operators) {
this.operators = operators;
}
}
机器操作员
@Entity
@Table(name="humanresources.employee_machineoperators")
@IdClass(MachineOperatorID.class)
public class MachineOperator implements Serializable {
private static final long serialVersionUID = 1L;
private Employee employee;
private Machine machine;
private SkillLevel skillLevel;
public MachineOperator() {}
public MachineOperator(Employee employee, Machine machine) {
this.employee = employee;
this.machine = machine;
}
public MachineOperator(Employee employee, Machine machine, SkillLevel skillLevel) {
this.employee = employee;
this.machine = machine;
this.skillLevel = skillLevel;
}
@Id
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="fk_employeeid")
@JsonView(View.SimpleEmployeeView.class)
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
@Id
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="fk_machineid")
public Machine getMachine() {
return machine;
}
public void setMachine(Machine machine) {
this.machine = machine;
}
@ManyToOne
@JoinColumn(name="fk_skilllevelid")
public SkillLevel getSkillLevel() {
return skillLevel;
}
public void setSkillLevel(SkillLevel skillLevel) {
this.skillLevel = skillLevel;
}
@Override
public int hashCode() {
return Objects.hash(machine, employee);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) {
return false;
}
MachineOperator other = (MachineOperator) obj;
return Objects.equals(machine, other.getMachine()) && Objects.equals(employee, other.getEmployee());
}
}
机器操作员ID
public class MachineOperatorID implements Serializable {
private static final long serialVersionUID = 1L;
private Employee employee;
private Machine machine;
public MachineOperatorID() {}
public MachineOperatorID(Employee employee, Machine machine) {
this.employee = employee;
this.machine = machine;
}
public Employee getEmployee() {
return employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public Machine getMachine() {
return machine;
}
public void setMachine(Machine machine) {
this.machine = machine;
}
@Override
public int hashCode() {
int hash = 7;
hash = 83 * hash + Objects.hashCode(this.machine);
hash = 83 * hash + Objects.hashCode(this.employee);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final MachineOperatorID other = (MachineOperatorID) obj;
if (!Objects.equals(this.machine, other.machine)) {
return false;
}
if (!Objects.equals(this.employee, other.employee)) {
return false;
}
return true;
}
}
任何人都知道出了什么问题,或者有没有更好的方法来获得相同的结果? (我希望能够查看一个员工并查看他们可以操作的机器,或者查看一台机器并查看所有可以操作它的员工。)我使用的是 Spring Boot 2.0.3。谢谢!
【问题讨论】:
-
您的应用是否配置为扫描
com.humanresources.domaindocs.spring.io/spring-boot/docs/current/api/org/springframework/… docs.spring.io/spring-boot/docs/current/reference/htmlsingle/… 中的实体 -
你能在启动应用时提供控制台吗
-
@JeanMarois 是的,它本质上被配置为扫描
com。 (我实际上有com.companyname,但删除了帖子的公司名称。) -
@Keaz 当我启动应用程序时,我得到了 Spring ASCII 艺术。当我构建应用程序时,唯一相关的输出就是我上面包含的内容。
-
您能告诉我们如何为测试配置实体扫描吗?你使用的是同一个主类吗?
标签: hibernate spring-boot spring-data-jpa