保存实体时出错,表示属性引用未知实体

问题描述

当我尝试通过添加子书对象来保存用户时,我已经创建了具有多对一关系的多个实体,但我得到一个错误,即预订的属性计划正在引用一个未知的实体。错误是“ com.demo.flightmanagement.entity.Booking.schedule上的@OnetoOne或@ManyToOne引用了未知实体:com.demo.flightmanagement.entity.Schedule”

这是机场实体

$ID3413241
  X var1 var2
1 1   22   43
2 2   43   45

$ID3413242
  X var1 var2
1 1   42   43
2 2   43   45

$ID3413243
  X var1 var2
1 1   90   97
2 2   98   96

$ID3413244
  X var1 var2
1 1   43   85
2 2   55   43

这是计划实体

@Entity
public class Airport {
    
    @Id
    private int id;
    
    @OnetoMany(cascade = CascadeType.ALL,mappedBy = "airport")
    private List<Schedule> schedules = new ArrayList<Schedule>();

}

这是用户实体

@Entity
public class Schedule {
    
    @Id
    private int id;
    
    @OnetoMany(mappedBy = "schedule")
    private List<Booking> bookings = new ArrayList<Booking>();
    
    @ManyToOne(targetEntity = Airport.class)
    @JoinColumn(name="airport_id")
    private Airport airport;
}

这是预订实体

@Entity
public class User {
    @Id
    private int id;
    private String userName;
    private String password;
    
    @OnetoMany(cascade = CascadeType.ALL,mappedBy = "user")
    private List<Booking> bookings = new ArrayList<Booking>();
}

这是主要的通话类

@Entity
public class Booking {
    
    @Id
    @GeneratedValue
    private int id;
    
    @ManyToOne
    @JoinColumn(name = "user_id")
    private User user;
    
    @ManyToOne
    @JoinColumn(name = "schedule_id")
    private Schedule schedule;
}

这是要保存的用户存储库

@SpringBootApplication
public class JwtExamplesApplication {

    @Autowired
    private UserRepo repo;
    
    @postconstruct
    public void initUsers() {
        User u =  new User(1,"abc","def");
        Schedule s = new Schedule(22,null);
        Booking b = new Booking (u,s);
        List<Booking> l = new ArrayList<Booking>();
        l.add(b);
        u.setBookings(l);
        repo.save(u);
    }
    
    public static void main(String[] args) {
        SpringApplication.run(JwtExamplesApplication.class,args);
    }

 

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)