当前位置: 首页 > 知识库问答 >
问题:

为什么Spring JPA双向OneToMany和ManyToOne没有更新外键列?

淳于健
2023-03-14

嗨,我正在学习Spring JPA使用OneToMany和ManyToOne双向关系,在一些示例中,我看到OneToMany和ManyToOne关系,当我在两个方面编写时,JPA添加一个新列作为外键列,并插入父表中的键值。但当我尝试我的时候,这一栏总是空白的。下面是我的代码的样子:

下面是我的Account.java模型:

@Entity
@Table(name = "msAccount")
public class Account {

    @Id
    @NotBlank(message = "Not Blank")
    @Size(min = 0, max = 20)
    public String accountId;

    @NotBlank(message = "Not Blank")
    public String accountName;

    @NotBlank(message = "Not Blank")
    @Email(message = "Should be the right email")
    public String accountEmail;

    @NotBlank(message = "Not Blank")
    @Size(min = 5, message = "Minimal 5 char")
    public String accountAddress;

    @NotBlank(message = "Not Blank")
    public String town;

    @NotBlank(message = "Not Blank")
    public String npwp;

    @NotBlank(message = "Not Blank")
    public String phoneNumber;

    public String fax;

    public String remarks;

    @NotNull
    public Date entryTime;

    @NotNull
    public Boolean active;

    @OneToMany(mappedBy="account", cascade = CascadeType.ALL, orphanRemoval = true)
    public List<Dealer> dealer;

//getter setter skipped

}

下面是我的dealer.java模型:

@Entity
@Table(name = "msDealer")
public class Dealer {

    @Id
    @NotBlank(message = "Tidak Boleh Kosong")
    @Size(min = 0, max = 20)
    public String dealerId;

    @NotBlank(message = "Tidak Boleh Kosong")
    public String dealerName;

    @NotBlank(message = "Tidak Boleh Kosong")
    @Email(message = "Masukkan Email yang bener")
    public String dealerEmail;

    @NotBlank(message = "Tidak Boleh Kosong")
    @Size(min = 5, message = "Minimal 5 karakter")
    public String dealerAddress;

    @ManyToOne(fetch = FetchType.LAZY)
    public Account account;

//getter setter skipped

}
@Repository
public interface AccountRepository extends JpaRepository<Account, Long> {

}
@Service
public class AccountService {

    @Autowired
    private AccountRepository accountRepository;

    public Account save(Account account) {
        return accountRepository.save(account);
    }

}

这里是我的控制器:

@RestController
@RequestMapping("/api/account")
public class AccountController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final int ROW_PER_PAGE = 10;

    @Autowired
    private AccountService accountService;

    @PostMapping("/new")
    public ResponseEntity<Account> addAccount(@Valid @RequestBody Account account) {
        try {
            Account newAccount = accountService.save(account);
            return ResponseEntity.created(new URI("/api/account/" + newAccount.getAccountId()))
                    .body(account);
        } catch(Exception ex) {
            logger.error(ex.getMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
    }

}

然后将JSON发布到保存endpoint:

{
  "accountId": "USA001",
  "accountName": "string",
  "accountEmail": "string",
  "accountAddress": "string",
  "town": "string",
  "npwp": "string",
  "phoneNumber": "string",
  "fax": "string",
  "remarks": "string",
  "entryTime": "2020-04-07T15:01:29.404Z",
  "active": true,
  "dealer": [
    {
      "dealerId": "MMO001",
      "dealerName": "string",
      "dealerEmail": "string",
      "dealerAddress": "string"
    }
  ]
}

当我保存它时,我的终端中显示的hibernate将查询插入到2表中,但是当我检查我的数据库表(它是postgresql)时,我发现有一个字段“account_account_id”是空的,我错过了什么?

我希望Hibernate像这样运行sql:

insert into account (account_id, account_name, ...etc)
values ('USA001', 1)

insert into dealer (account_account_id, dealer_name, dealer_id, ...etc)
values ('USA001', 'New dealer 1', 'MMO001')

下面是我经过一番尝试后更新的模型:

我的Account.java我删除cascade=CascadeType.all,OlphanRemovation=true

@Entity
@Table(name = "msAccount")
public class Account {

    @Id
    @NotBlank(message = "Tidak Boleh Kosong")
    @Size(min = 0, max = 20)
    public String accountId;

    @NotBlank(message = "Tidak Boleh Kosong")
    public String accountName;

    @NotBlank(message = "Tidak Boleh Kosong")
    @Email(message = "Masukkan Email yang bener")
    public String accountEmail;

    @NotBlank(message = "Tidak Boleh Kosong")
    @Size(min = 5, message = "Minimal 5 karakter")
    public String accountAddress;

    @NotBlank(message = "Tidak Boleh Kosong")
    public String town;

    @NotBlank(message = "Tidak Boleh Kosong")
    public String npwp;

    @NotBlank(message = "Tidak Boleh Kosong")
    public String phoneNumber;

    public String fax;

    public String remarks;

    @NotNull
    public Date entryTime;

    @NotNull
    public Boolean active;

    @OneToMany(mappedBy="account")
    // @JoinColumn(name = "accountId")
    public List<Dealer> dealer;

//getter setter skipped

}
@Entity
@Table(name = "msDealer")
public class Dealer {

    @Id
    @NotBlank(message = "Tidak Boleh Kosong")
    @Size(min = 0, max = 20)
    public String dealerId;

    @NotBlank(message = "Tidak Boleh Kosong")
    public String dealerName;

    @NotBlank(message = "Tidak Boleh Kosong")
    @Email(message = "Masukkan Email yang bener")
    public String dealerEmail;

    @NotBlank(message = "Tidak Boleh Kosong")
    @Size(min = 5, message = "Minimal 5 karakter")
    public String dealerAddress;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "account_id")
    public Account account;

//getter setter skipped

}
> "Unable to find com.api.b2b.Model.Dealer with id MMO001; nested
> exception is javax.persistence.EntityNotFoundException: Unable to find
> com.api.b2b.Model.Dealer with id MMO001"

这里是我的github回购:https://github.com/fly-away/learningspring

共有1个答案

王凌
2023-03-14

子端缺少@joincolumn:

@Entity
@Table(name = "ms_dealer")
public class Dealer {

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "account_account_id")
    public Account account;

    // other fields

}

您已经在父端使用了mappedby,但在子端没有映射。您需要指出dealer是关系所有者-它具有外键。

编辑:如果您将account实体及其子实体持久化(而不是合并),则不应传递子实体的ID。(实际上,在persist上传递任何ID是一种代码味道,而且很可能是性能杀手。)所使用的json应该如下所示:

{
  "accountName": "string",
  "accountEmail": "string",
  "accountAddress": "string",
  "town": "string",
  "npwp": "string",
  "phoneNumber": "string",
  "fax": "string",
  "remarks": "string",
  "entryTime": "2020-04-07T15:01:29.404Z",
  "active": true,
  "dealer": [
    {
      "dealerName": "string",
      "dealerEmail": "string",
      "dealerAddress": "string"
    }
  ]
}
account.getDealer().forEach(d -> d.setAccount(account));

编辑:

作者编辑必须级联到子级:

@OneToMany(mappedBy = "account", cascade = CascadeType.ALL, orphanRemoval = true)
public List<Dealer> dealer;

您还可以在actionlist 上添加@jsonignore以避免序列化到JSON时的堆栈溢出。

 类似资料:
  • 我在将带有@ManyToOne关系的实体(雇员)映射到带有@OneToMany关系的实体(社区)时遇到了问题。 当我将一个实体(社区)分配给一个实体(员工),其中社区的值为空时,它工作正常。 问题出现了,如果雇员已经为社区分配了价值。当我更改该值并保存更改时,该员工为社区获得了新的值,并且这个新社区在集合中获得了该员工。唯一的问题是,老社区仍然有这个员工在收集,但它应该被删除。这只有在数据库重新启

  • 拥有2个实体:订单和产品。1个订单可以有多个产品,多个产品可以属于1个订单(每个产品只属于1个订单)。 尝试了@JsonIgnore。这不会返回子元素或父元素。尝试了@JsonManagedReference和@JsonBackReference-仍然没有成功。 请给我指点一下

  • 问题内容: 在JPA注释参考的示例部分中: 示例1-59 @OneToMany-具有泛型的客户类 示例1-60 @ManyToOne-具有泛型的Order类 在我看来,实体是协会的所有者。但是,在同一文档中对属性的说明中写道: 如果关系是双向的,则将关联的反(非所有权)侧的maptedBy元素设置为拥有该关系的字段或属性的名称,如示例1-60所示。 但是,如果我没有记错,则在示例中看起来,实际上是

  • 我想安装Primeng7.0.0到我的Angular项目,但首先我需要更新我的JHipster到最后一个版本。 当我将此命令写入terminal时,我得到以下错误

  • 我基于以下示例实现了这一点:https://vladmihalcea.com/the-best-way-to-map-a-onetomany-association-with-jpa-and-hibernate/

  • 我是一个JPA新手,正在尝试理解@Jointable annotation用于双向、单管关系、B/W项目和任务实体,其中项目可以有多个任务。 我可以将@Jointable与具有@ManyToOne注释的实体一起使用,但当我将@JoinColumn放置在具有@OneToMany的另一个实体上时,我没有在@ManyToOne注释上指定“MappedBy”属性的选项。 我想知道为什么? 我尝试在这两个实