英文:
JPA - How to save entity with value from non PK column in DB
问题
let's say i have a simple entity call Account and User
**Account**
@Id
@Column(name="ACCOUNT_ID")
private Long accountId;
@Column(name="ACCOUNT_NO")
private String accountNo;
**User**
@Id
@Column(name="USER_ID")
private Long userId;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ACCOUNT_NO")
private Account account;
and I have a save action
Account acc = accountRepo.findByAccountId(1);
acc.setAccountNo("TestAccNo");
User user = new User();
user.setAccount(acc);
userRepo.save(user);
User entity can be saved successfully with the following db result:
User
--------------------------
ID | ACCOUNT_NO
--------------------------
1 | 1
I understand that the mapping should always use the PK, which is ACCOUNT_ID in the User entity. However, for some reason, the User table only has ACCOUNT_NO. Therefore, I want the ACCOUNT_NO to store "TestAccNo" instead of "1" (Account's PK).
I modified the User entity as follows. It's able to capture the ACCOUNT_NO. I kept the @ManyToOne mapping and added another column mapping.
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ACCOUNT_NO", insertable = false, updatable = false)
private Account account;
@Column(name="ACCOUNT_NO")
private String accountNo;
---------------------------------------
user.setAccountNo(acc.getAccountNo());
userRepo.save(user);
This seems like not a proper way to achieve that. Does anyone have a better solution?
Thank you.
英文:
let's say i have a simple entity call Account and User
Account
@Id
@Column(name="ACCOUNT_ID")
private Long accountId;
@Column(name="ACCOUNT_NO")
private String accountNo;
User
@Id
@Column(name="USER_ID")
private Long userId;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ACCOUNT_NO")
private Account account;
and I have a save action
Account acc = accountRepo.findByAccountId(1);
acc.setAccountNo("TestAccNo");
User user = new User();
user.setAccount(acc);
userRepo.save(user);
User entity can be save successfully with the following db result:
User
--------------------------
ID | ACCOUNT_NO
--------------------------
1 | 1
I understand that the mapping should always using the PK, which is ACCOUNT_ID in User entity, but for some reason, the User table only have ACCOUNT_NO, so I want the ACCOUNT_NO to store "TestAccNo" instead of "1" (Account'S PK).
I modified User entity to following and it's able to capture the ACCOUNT_NO, I keep the @ManyToOne mapping and create another column mapping.
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ACCOUNT_NO", insertable = false, updatable = false)
private Account account;
@Column(name="ACCOUNT_NO")
private String accountNo;
---------------------------------------
user.setAccountNo(acc.getAccountNo());
userRepo.save(user);
This seem like not a proper way to do that, is anyone have better solution ?
Thank you.
答案1
得分: 0
你可以在@JoinColumn
中使用referencedColumnName
来指定要存储在连接列中的列的值。
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ACCOUNT_NO", referencedColumnName="ACCOUNT_NO")
private Account account;
英文:
You can use referencedColumnName
in @JoinColumn
to specify which column's value you want to store in the joined column
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="ACCOUNT_NO", referencedColumnName="ACCOUNT_NO")
private Account account;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论