hibernate继承子类不使用序列的问题

时间:2014-05-07 12:54:34

标签: hibernate

嗨我的超级类有主键,在我的Sub类实体中我想定义/使用在Oracle数据库中定义的Sequencer,但我总是将ID(主键)的值变为0.似乎序列器没有被执行。请帮忙。

@MappedSuperclass
public abstract class GenericEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "ID")
    protected long id;

    @Version
    protected Integer version = Integer.valueOf(0);

    public GenericEntity() {
    }

    public abstract long getId() ;

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

}

子类:

@Entity
@Table(name = "pf_address")
@SequenceGenerator(sequenceName="SEQ_PF_ADDRESS", name = "seq_address", allocationSize=1)
public class Address extends GenericEntity {
    private static final long serialVersionUID = 5627302335705194341L;
    private String country = "";


    @GeneratedValue(generator= "seq_address", strategy = GenerationType.SEQUENCE)
    @AttributeOverride(name = "id", column = @Column(name = "ID"))
    public long getId() {
    return id;
    }

        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }


}

2 个答案:

答案 0 :(得分:0)

我认为你错过了这个

@Table(name = "pf_address")
@SequenceGenerator(sequenceName="SEQ_PF_ADDRESS", name = "seq_address", allocationSize=1)
@GeneratedValue(generator = "seq_address", strategy = GenerationType.SEQUENCE)

答案 1 :(得分:0)

在超级课程中,我们只需要使用序列。

@MappedSuperclass
public abstract class GenericEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "ID")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "vivas_sequence_generator")
    protected long            id;



    @Version
    protected Integer         version          = Integer.valueOf(0);

    public GenericEntity() {}

    public long getId(  ) {
    return id;  
    }

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

    public Integer getVersion() {
    return version;
    }

    public void setVersion(Integer version) {
    this.version = version;
    }
}

子实体类:

@Entity
@Table(name = "pf_address")
@GenericGenerator(name = "vivas_sequence_generator", strategy = "native", parameters = {
        @Parameter(name = "sequence", value = "SEQ_PF_ADDRESS"),
        @Parameter(name = "optimizer", value = "none") })
public class Address extends GenericEntity {
    private static final long serialVersionUID = 5627302335705194341L;
    private String            country          = "";



    public String getCountry() {
    return country;
    }

    public void setCountry(String country) {
    this.country = country;
    }

}

其中SEQ_PF_ADDRESS是Oracle数据库中的实际序列。类似地,对于其他子类实体,仅更改将是不同的序列名称。

例如

@Entity
@Table(name = "pf_stakeholder")
@GenericGenerator(name = "vivas_sequence_generator", strategy = "native", parameters = {
        @Parameter(name = "sequence", value = "SEQ_PF_STAKEHOLDER"),
        @Parameter(name = "optimizer", value = "none") })
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Stakeholder extends GenericEntity {

}