JPA实体与自身有关
我有一个名为“帐户”的表。 在此表上,将有与父母有关的父母和子帐户。 我的想法是使用一个实体,一对多且一对一的关系。
实体看起来像这样:
@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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论