@GenericGenerator(name =“generator”,strategy =“foreign”)到标准JPA 2

时间:2012-10-23 20:28:20

标签: java hibernate jpa-2.0

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

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

2 个答案:

答案 0 :(得分: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;
//...
}

希望这有帮助!

答案 1 :(得分:1)

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

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

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