4

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

@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;
}

到没有休眠的标准 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;
    }
4

2 回答 2

2

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

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

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

于 2013-12-13T20:50:41.983 回答
1

我只使用 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;
//...
}

希望这可以帮助!

于 2012-10-23T20:35:14.663 回答