Spring Boot和JPA覆盖映射实体名称/标识符

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

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!

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

发表评论

匿名网友

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

确定