JPA中按实体生成的序列

时间:2017-11-07 11:48:44

标签: java mysql jpa

我正在尝试使用JPA在MySQL中创建一个序列生成器字段,“prod_generator_id”必须以“1”开头,对于任何实体。 例如:

image

这是我的java对象:

//this one is working fine, the id is unique...
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "pro_id")
private Long id;

//here is the problem, must start in "1" by entity
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="seq")
@SequenceGenerator(name="seq", sequenceName="pro_id_tenancy", initialValue=1, allocationSize=1)
@Column(name = "pro_id_tenancy")
private Long idTenancyProjeto;

我正在使用spring boot,但是当我保存对象时,idTenancyProjeto将为null。

我发现的最简单的解决方案:只计算该公司的产品数量,并在我的product.save()中添加+ 1,仅此而已。谢谢大家。

2 个答案:

答案 0 :(得分:1)

据我了解,您希望该公司的所有产品编号从1到n。

映射此内容的可能性取决于您使用的JPA提供程序。

要在Hibernate中实现这一点(从4.3开始),您可以将索引集合(通过@OneToMany)与@ListIndexBase结合使用(请参阅https://docs.jboss.org/hibernate/orm/4.3/javadocs/org/hibernate/annotations/ListIndexBase.html)。

示例代码:

@Entity
class Corporation {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "corp_id")
    private Long id;

    @OneToMany(mappedBy="corporation")
    @OrderColumn(name="prod_generator_id")
    @ListIndexBase(1)
    private List<Product> products;

    public List<Product> getProducts() { 
         return products;
    }

    public void setProducts(List<Product> products) {
       this.products = products;
    }
}

@Entity
class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "prod_id")
    private Long id;

    @ManyToOne
    @JoinColumn(
        name = "corp_id", 
        referencedColumnName = "corp_id"
    )
    private Corporation corporation;

    public Corporation getCorporation() {
        return corporation;
    }

    public void setCorporation(Corporation corporation) {
        this.corporation = corporation;
    }
}

另一种香草JPA方法可能是@PrePersist@PreUpdate的使用:

@Entity
class Corporation {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "corp_id")
    private Long id;

    @OneToMany(mappedBy="corporation", cascade = CascadeType.ALL)
    @OrderBy("prodGeneratorId")
    private List<Product> products;

    public Long getId() {
        return id;
    }

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

    public List<Product> getProducts() {
         return products;
    }

    public void setProducts(List<Product> products) {
       this.products = products;
    }
}

@Entity
class Product {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "prod_id")
    private Long id;

    @Column(name = "prod_generator_id")
    private Integer prodGeneratorId;

    public Long getId() {
        return id;
    }

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

    @ManyToOne
    @JoinColumn(
        name = "corp_id",
        referencedColumnName = "corp_id"
    )
    private Corporation corporation;

    public Corporation getCorporation() {
        return corporation;
    }

    public void setCorporation(Corporation corporation) {
        this.corporation = corporation;
    }

    public Integer getProdGeneratorId() {
        return prodGeneratorId;
    }

    private void setProdGeneratorId(Integer prodGeneratorId) {
        this.prodGeneratorId = prodGeneratorId;
    }

    @PrePersist
    @PreUpdate
    public void updateProdGeneratorId() {
        setProdGeneratorId(getCorporation().getProducts().indexOf(this) + 1);
    }
}

答案 1 :(得分:0)

解决方案:

作为最简单的解决方案,我创建了另一个表格,并且在保存之前我获得了值e add + 1。