Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Spring中连接@manytoman关系_Java_Spring_Jpa_Spring Data Jpa_H2 - Fatal编程技术网

Java 如何在Spring中连接@manytoman关系

Java 如何在Spring中连接@manytoman关系,java,spring,jpa,spring-data-jpa,h2,Java,Spring,Jpa,Spring Data Jpa,H2,我正在创建一个应用程序,它使用员工和班次之间的多对多关系。但是,我很难理解如何将员工分配/连接到轮班 @Data @Entity public class Employee { private @Id @GeneratedValue long employeeID; private String Name; @OneToMany(cascade = CascadeType.ALL) private Set<Shift> shifts; private Employee() {

我正在创建一个应用程序,它使用员工和班次之间的多对多关系。但是,我很难理解如何将员工分配/连接到轮班

@Data
@Entity
public class Employee {

private @Id @GeneratedValue long employeeID;
private String Name;

@OneToMany(cascade = CascadeType.ALL)
private Set<Shift> shifts;

private Employee() {
}

public Employee(long employeeID, String Name) {
    this.employeeID = employeeID;
    this.Name = Name;
}

public Employee(long employeeID, String Name, Set<Shift> shifts) {
    this.employeeID = employeeID;
    this.Name = Name;
    this.shifts = shifts;
}

public void setShift(Set<Shift> shifts) {
    this.shifts = (Set<Shift>) shifts;
}
}

公共接口ShiftRepository扩展了crudepository
公共接口EmployeeRepository扩展了Crudepository
添加到员工和班次中的实体会被保存,但是有没有一种方法可以将班次分配给DatabaseLoader类中的员工,因为我一直在寻找解决方案

我知道我还没有包括一种试图将员工和班次联系起来的方法,但我不知道如何解决这个问题

提前谢谢

**编辑:我现在遇到的新问题是,我在尝试在spring中部署时收到以下消息:

无法构建Hibernate SessionFactory:无法确定列:[org.Hibernate.mapping.Column(employee)]的java.util.Set的类型,在表:shift中

  • 转移是一个独立的实体。它不依赖于任何员工。 因此
    Shift
    不得引用
    Employee

  • 另一方面,一名员工依赖于多个班次,因此
    员工
    必须具有
    班次

  • @OneToMany
    私人名单转移;
    
  • 这里不要使用层叠,因为
    Employee
    Shift
    是独立的实体
  • 更新

    要“向员工添加班次”,我们可以使用例如
    员工
    设定者:

    @Entity
    public class Employee {
        //...
        private String name;
        //...
        @OneToMany
        private List<Shift> shifts;
        //...
        public Employee(String name) {
            this.name = name;
        }
        //...
        public setShifts(Shift... shifts) {
            this.shifts = Arrays.asList(shifts);
        }
        //...
    }
    

    似乎您正在使用Hibernate,那么您所要做的就是正确设置要保存的对象,如下所示:

    @Component
    public class DatabaseLoader implements CommandLineRunner {
    
    private final EmployeeRepository repository;
    private final ShiftRepository shiftRepository;
    
    @Autowired
    public DatabaseLoader(EmployeeRepository repository, ShiftRepository shiftRepository) {
        this.repository = repository;
        this.shiftRepository=shiftRepository;
    }
    
    @Override
    public void run(String... strings) throws Exception {
        Shift shift = new Shift("Friday Morning");
        Employee employee = new Employee(0001, "Adam Smith");
    
        employee.setShift(shift)
        this.repository.save(employee);
    }
    

    唯一的区别是我附加了两个对象,然后才尝试保存它们。值得一提的是,使用Cascade.All或Cascade.persistene非常重要,以便hibernate在这两个实体上进行插入

    在JPA中,我总是将我的多对多分解为一对多和多对一,这使您能够更好地控制级联,并且在映射表显式时更容易移动关系。感谢您的建议,这非常有用!但是,我仍然不确定如何在我的DatabaseLoader类中为员工添加轮班…例如:如何将星期五上午添加到Adam Smith“如何将星期五上午添加到Adam Smith?”-例如,使用
    employee
    setter-查看我的更新答案。您好,谢谢您的回复!但是,当我尝试实现您的方法时,我遇到了以下错误:无法构建Hibernate SessionFactory:无法确定以下列的类型:java.util.Set,在table:shift,在columns:[org.Hibernate.mapping.Column(employee)]------------我还更新了上面的类,以便您可以确切地看到我所做的更改
    public interface ShiftRepository extends CrudRepository<Shift, Long> 
    
    public interface EmployeeRepository extends CrudRepository<Employee, Long>
    
    @Entity
    public class Employee {
        //...
        private String name;
        //...
        @OneToMany
        private List<Shift> shifts;
        //...
        public Employee(String name) {
            this.name = name;
        }
        //...
        public setShifts(Shift... shifts) {
            this.shifts = Arrays.asList(shifts);
        }
        //...
    }
    
    Shift monday = shiftRepository.save(new Shift("Monday"));
    Shift friday = shiftRepository.save(new Shift("Friday"));
    Employee adamSmith = new Employee("Adam Smith");
    adamSmith.setShifts(monday, friday);
    employeeRepository.save(adamSmith);
    
    @Component
    public class DatabaseLoader implements CommandLineRunner {
    
    private final EmployeeRepository repository;
    private final ShiftRepository shiftRepository;
    
    @Autowired
    public DatabaseLoader(EmployeeRepository repository, ShiftRepository shiftRepository) {
        this.repository = repository;
        this.shiftRepository=shiftRepository;
    }
    
    @Override
    public void run(String... strings) throws Exception {
        Shift shift = new Shift("Friday Morning");
        Employee employee = new Employee(0001, "Adam Smith");
    
        employee.setShift(shift)
        this.repository.save(employee);
    }