英文:
Spring boot and JPA override mapped entity name/id
问题
以下是翻译后的内容:
我有一些混合了嵌入属性的实体。我已经能够通过使用 @AttributeOverrides
和 @AttributeOverride
来覆盖嵌入列名,但似乎对于嵌套实体无效。
请参考以下代码:
@Entity
@Table(name = "branch")
public class Branch {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String phoneNumber;
private String email;
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "streeNumber", column = @Column(name = "branch_street_number")),
@AttributeOverride(name = "streetName", column = @Column(name = "branch_street_name")),
@AttributeOverride(name = "lga", column = @Column(name = "branch_lga_id")),
@AttributeOverride(name = "city", column = @Column(name = "branch_city"))
})
private Location location;
}
Location 实体:
@Embeddable
public class Location {
private String streetNumber;
private String streetName;
@ManyToOne
@JoinColumn(referencedColumnName = "id", nullable = false)
private LocalGovernmentArea lga;
private String city;
}
然而,lga
列仍然会生成为 lga_id
而不是 branch_lga_id
。
是否有解决这个问题的方法呢?
谢谢。
英文:
I have a couple of entities mixed with Embedded attributes. I've been able to override embedded column names using @AttributeOverrides
and @AttributeOverride
but it doesn't seem to work for nested entities.
see below:
@Entity
@Table(name = "branch")
public class Branch {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String phoneNumber;
private String email;
@Embedded
@AttributeOverrides({
@AttributeOverride( name = "streeNumber", column = @Column(name = "branch_street_number")),
@AttributeOverride( name = "streetName", column = @Column(name = "branch_street_name")),
@AttributeOverride( name = "lga", column = @Column(name = "branch_lga_id")),
@AttributeOverride( name = "city", column = @Column(name = "branch_city"))
})
private Location location;
Location entity:
@Embeddable
public class Location {
private String streetNumber;
private String streetName;
@ManyToOne
@JoinColumn(referencedColumnName = "id", nullable = false)
private LocalGovernmentArea lga;
private String city;
the lga column still generates as lga_id
instead of branch_lga_id
Is there any solution to this?
Thank you.
答案1
得分: 0
找到一个解决办法。我可以使用 @AssociationOverrides
来重写实体名称。
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "streetNumber", column = @Column(name = "branch_street_number")),
@AttributeOverride(name = "streetName", column = @Column(name = "branch_street_name")),
@AttributeOverride(name = "lga", column = @Column(name = "branch_lga_id")),
@AttributeOverride(name = "city", column = @Column(name = "branch_city"))
})
@AssociationOverrides({
@AssociationOverride(
name = "lga", joinColumns = @JoinColumn(name = "branch_lga_id"))
})
private Location location;
这就可以达到效果了!
英文:
Found a solution. I can override the entity name using @AssociationOverrides
@Embedded
@AttributeOverrides({
@AttributeOverride( name = "streetNumber", column = @Column(name = "branch_street_number")),
@AttributeOverride( name = "streetName", column = @Column(name = "branch_street_name")),
@AttributeOverride( name = "lga", column = @Column(name = "branch_lga_id")),
@AttributeOverride( name = "city", column = @Column(name = "branch_city"))
})
@AssociationOverrides({
@AssociationOverride(
name = "lga", joinColumns = @JoinColumn(name = "branch_lga_id"))
})
private Location location;
That does the trick!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论