Angular 6 + Spring Boot Rest无法保存@OneToMany关系的外键
创始人
2024-10-16 00:31:00
0

这个问题是由于Angular 6和Spring Boot Rest在处理@OneToMany关系时,无法保存外键引用导致的。下面是一个可能的解决方法:

  1. 首先,确保在Angular 6中正确配置了与后端服务器的通信。可以使用HttpClient模块发送HTTP请求。

  2. 在Angular 6的服务类中,确保正确地将数据发送到后端服务器。可以使用POST请求将数据发送到Spring Boot Rest API。

  3. 在Spring Boot Rest控制器中,确保正确地处理接收到的数据。可以使用@RequestBody注解将JSON数据转换为Java对象。

  4. 在Spring Boot的实体类中,确保正确地定义@OneToMany关系。使用@JoinColumn注解指定外键列。

  5. 在Spring Boot的数据访问层中,确保正确地保存@OneToMany关系的外键引用。可以使用JpaRepository的save方法来保存实体对象。

下面是一个示例代码,演示了上述解决方法的实现:

Angular 6 服务类:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable()
export class DataService {
  constructor(private http: HttpClient) {}

  saveData(data: any) {
    return this.http.post('http://localhost:8080/api/saveData', data);
  }
}

Spring Boot Rest 控制器:

@RestController
@RequestMapping("/api")
public class DataController {
  @Autowired
  private DataRepository dataRepository;

  @PostMapping("/saveData")
  public void saveData(@RequestBody Data data) {
    dataRepository.save(data);
  }
}

Spring Boot 实体类:

@Entity
public class Data {
  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
  @JoinColumn(name = "data_id")
  private List childData;

  // getters and setters
}

Spring Boot 数据访问层:

@Repository
public interface DataRepository extends JpaRepository {
}

请注意,上述示例代码仅用于演示目的,并不是完整的解决方案。您可能需要根据您的具体要求和数据模型进行适当的修改。

相关内容

热门资讯

hhpoker有透视的吗!wp... hhpoker有透视的吗!wpk辅助插件(透视)技巧-切实教你存在有挂运hhpoker有透视的吗辅助...
透视揭幕!epoker免费透视... 透视揭幕!epoker免费透视脚本(透视)wepoker祈福有用吗,教程指引(真的有挂)-哔哩哔哩1...
wepoker辅助插件功能!w... wepoker辅助插件功能!wepoker辅助器激活码(透视)教程-确实专业是真的挂1、任何wepo...
透视详细!wepoker私人局... 透视详细!wepoker私人局俱乐部怎么进(透视)wpk软件是正规的吗,教程学习(有挂分析)-哔哩哔...
we poker辅助器!hhp... we poker辅助器!hhpoker可以控制牌吗(透视)器-确实曝光存在有挂1.hhpoker可以...
透视解密!wepoker线上大... 透视解密!wepoker线上大神(透视)aapoker透视脚本入口,教程窍门(确实有挂)-哔哩哔哩;...
pokemmo修改器手机版!f... pokemmo修改器手机版!fishpoker透视(透视)器-竟然必备是有挂1、pokemmo修改器...
透视详细!wepoker有没有... 透视详细!wepoker有没有挂(透视)wpk有那种辅助吗,教程积累(有挂透明挂)-哔哩哔哩wpk有...
拱趴大菠萝开挂方法!竞技联盟透... 拱趴大菠萝开挂方法!竞技联盟透视(透视)插件-确实总结真的有挂暗藏猫腻,小编详细说明拱趴大菠萝开挂方...
透视开挂!hhpoker德州挂... 透视开挂!hhpoker德州挂真的有吗(透视)约局吧开挂神器是真的吗,教程模板(有挂工具)-哔哩哔哩...