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

Spring boot 保存实体时出错,表示属性引用未知实体,spring-boot,jpa,spring-data-jpa,Spring Boot,Jpa,Spring Data Jpa,当我试图通过添加子book对象来保存用户时,我创建了多个具有多对一关系的实体。我收到一个错误,即预订的属性计划引用了未知实体。错误为“@OneToOne或@ManyToOne on com.demo.flightmanagement.entity.Booking.schedule引用未知实体:com.demo.flightmanagement.entity.schedule” 这是机场实体: @Entity public class Airport { @Id priv

当我试图通过添加子book对象来保存用户时,我创建了多个具有多对一关系的实体。我收到一个错误,即预订的属性计划引用了未知实体。错误为“@OneToOne或@ManyToOne on com.demo.flightmanagement.entity.Booking.schedule引用未知实体:com.demo.flightmanagement.entity.schedule”

这是机场实体

@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;
}
这是主调用类

@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);
    }

 

}
@springboot应用程序
公共类JWTExample应用程序{
@自动连线
私人回购;
@施工后
公共用户(){
用户u=新用户(1,“abc”,“def”);
附表s=新附表(22,空);
预订b=新预订(美国);
列表l=新的ArrayList();
l、 添加(b);
u、 立木(l);
回购保存(u);
}
公共静态void main(字符串[]args){
run(JwtExamplesApplication.class,args);
}
}
这是用于保存的用户存储库

@Repository
public interface UserDao extends JpaRepository<User, BigInteger> {

}
@存储库
公共接口UserDao扩展了JpaRepository{
}

您是否错过了机场实体上的@Entity?不,它在程序中,我忘了把它放在这里。您的实体列名只是根据类别的Id,但您正在加入schedule_Id,甚至在其他实体中。确保列名正确。@JoinColumn为外键命名
@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);
    }

 

}
@Repository
public interface UserDao extends JpaRepository<User, BigInteger> {

}