JPA实体与自身有关

发布于 2025-02-05 07:00:36 字数 1444 浏览 1 评论 0原文

我有一个名为“帐户”的表。 在此表上,将有与父母有关的父母和子帐户。 我的想法是使用一个实体,一对多且一对​​一的关系。

实体看起来像这样:

@Entity
@Getter
@Setter
public class Accounts implements Serializable {

@Id
@Column(name = "ACCOUNT_ID", nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "account_id_generator")
@SequenceGenerator(name="account_id_generator", sequenceName = "ACCOUNT_ID_seq", allocationSize = 1)
Long accountId;

@Column(name = "ACCOUNT")
String accountType;

@Column(name = "FK_SUB_ACCOUNT")
Long fkSubAccount;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "subAccount")
List<Accounts> subAccounts = new ArrayList<>();

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "fk_sub_account", updateable = false, insertable = true, referenceColumn = "account_id")
Account subAccount;

...
Some other attributes here, they are being populated by jpa

}

我的填充方式:

var account = new Account();
account.setAccountType("Parent");

var subAccount = new Account();
List<Account> subAccounts = new ArrayList<>();
subAccount.setAccountType("Sub");
subAccount.setSubAccount(account);
subAccount.getSubAccounts.addAll(List.of(account));
account.setSubAccount(subAccount);
subAccounts.add(subAccount);

account.getSubAccounts().addAll(subAccounts);

repository.save(account);

我能够看到除fksubaccount以外的所有属性 - null。 我做错了什么?

我有一些具有类似设置的桌子,除了它们在不同的实体上,而且它们工作正常。

I have a table called "Accounts".
On this table, there will be parent and sub accounts tied to a parent.
My idea is to use a single entity, with One-To-Many and Many-To-One relationship.

Entity looks like this:

@Entity
@Getter
@Setter
public class Accounts implements Serializable {

@Id
@Column(name = "ACCOUNT_ID", nullable = false)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "account_id_generator")
@SequenceGenerator(name="account_id_generator", sequenceName = "ACCOUNT_ID_seq", allocationSize = 1)
Long accountId;

@Column(name = "ACCOUNT")
String accountType;

@Column(name = "FK_SUB_ACCOUNT")
Long fkSubAccount;

@OneToMany(cascade = CascadeType.ALL, mappedBy = "subAccount")
List<Accounts> subAccounts = new ArrayList<>();

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "fk_sub_account", updateable = false, insertable = true, referenceColumn = "account_id")
Account subAccount;

...
Some other attributes here, they are being populated by jpa

}

How I populate:

var account = new Account();
account.setAccountType("Parent");

var subAccount = new Account();
List<Account> subAccounts = new ArrayList<>();
subAccount.setAccountType("Sub");
subAccount.setSubAccount(account);
subAccount.getSubAccounts.addAll(List.of(account));
account.setSubAccount(subAccount);
subAccounts.add(subAccount);

account.getSubAccounts().addAll(subAccounts);

repository.save(account);

I am able to see all attributes except for the fkSubAccount - which is null.
Anything I am doing wrong?

I had some tables with similar setup except they are on different entities, and they work fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文