JPA – 如何使用数据库中非主键列的值保存实体

huangapple go评论60阅读模式
英文:

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;

huangapple
  • 本文由 发表于 2020年9月15日 12:07:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63894915.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定