gpt4 book ai didi

java - @GenericGenerator(name = "generator", strategy = "foreign") 到标准 JPA 2

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:31:48 26 4
gpt4 key购买 nike

我想知道是否有标准的方法来转换类似的东西

@GenericGenerator(name = "generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "user"))
@Id
@GeneratedValue(generator = "generator")
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}

没有 hibernate 的标准 JPA 2 模型

编辑:让我们看看两个实体(我只保留注释的有趣部分)

@Entity
@Table(name = "author", catalog = "test")
public class Author implements java.io.Serializable {

@GenericGenerator(name = "generator", strategy = "foreign", parameters = @Parameter(name = "property", value = "user"))
@Id
@GeneratedValue(generator = "generator")
@Column(name = "id", unique = true, nullable = false)
public Integer getId() {
return this.id;
}

public void setId(Integer id) {
this.id = id;
}

@OneToOne(fetch = FetchType.LAZY)
@JsonBackReference
@PrimaryKeyJoinColumn
public User getUser() {
return this.user;
}

@Entity
@Table(name = "user", catalog = "test")
public class User implements java.io.Serializable {
@OneToOne(fetch = FetchType.LAZY, mappedBy = "user")
public Author getAuthor() {
return this.author;
}

最佳答案

也许您希望这些实体具有完全相同的 ID。如果是这样,只需添加 Author 类

 @OneToOne @MapsId
public User getUser() {
return this.user;
}

@MapsId 注释可以解决问题。你可以在那里查看 http://docs.oracle.com/javaee/6/api/javax/persistence/OneToOne.html (示例 2)

关于java - @GenericGenerator(name = "generator", strategy = "foreign") 到标准 JPA 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13038724/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com