微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

当我尝试通过添加子书对象来保存用户时,我已经创建了具有多对一关系的多个实体,但我得到一个错误,即预订的属性计划正在引用一个未知的实体。错误是“ 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 举报,一经查实,本站将立刻删除。