java @GenericGenerator(name = "generator", strategy = "foreign") 到标准 JPA 2
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13038724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
@GenericGenerator(name = "generator", strategy = "foreign") to standard JPA 2
提问by guillaume
I would like to know if there is a standard way to convert something like that
我想知道是否有标准的方法来转换类似的东西
@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;
}
to standard JPA 2 model without hibernate
到没有休眠的标准 JPA 2 模型
EDit : lets see both entities (i've only keep the annoted interessting portion)
编辑:让我们看到两个实体(我只保留了注释的有趣部分)
@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;
}
AND
和
@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;
}
回答by lakreqta
Perhaps you want those entities to have the exact same Id. If so just add in Author Class
也许您希望这些实体具有完全相同的 ID。如果是这样,只需添加作者类
@OneToOne @MapsId
public User getUser() {
return this.user;
}
The @MapsId
annotation will do the trick. You can check there http://docs.oracle.com/javaee/6/api/javax/persistence/OneToOne.html(example 2)
该@MapsId
注释会做的伎俩。您可以在那里查看http://docs.oracle.com/javaee/6/api/javax/persistence/OneToOne.html(示例 2)
回答by fostiguy
I only use JPA model so I'm not sure if this is linked to a serial (sequence). If so, then it should look like this:
我只使用 JPA 模型,所以我不确定这是否与序列(序列)相关联。如果是这样,那么它应该是这样的:
@Entity
@Table(name = "project", schema = "a_schema" )
@SequenceGenerator(name = "project_id_seq_generator", sequenceName = "a_schema.project_id_sequence")
public class MyObject {
@Id @GeneratedValue(strategy = GenerationType.AUTO, generator = "project_id_seq_generator")
@Column(name = "project_id")
private Long projectID;
//...
}
Hope this helps!
希望这可以帮助!