0

I'm trying to make a sequencial generator field in MySQL with JPA, the "prod_generator_id" must start in "1" for any entity. Example:

image

Here is my java object:

//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;

I'm using spring boot, but when I save the object, the idTenancyProjeto is going null.

The most simple solution I've found: Just count the number of products of that company, and add+1 in my product.save(), nothing more, is working. Thanks for all.

2 Answers2

1

As far as I understand, you would like to have all products of a company numbered from 1 to n within that company.

The possibilities to map this are dependent on the JPA provider you use.

To achieve this in Hibernate (starting from 4.3 upwards), you could use an indexed collection (via @OneToMany) in conjunction with @ListIndexBase (see https://docs.jboss.org/hibernate/orm/4.3/javadocs/org/hibernate/annotations/ListIndexBase.html).

Example code:

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

Another approach which is vanilla JPA could be the employment of @PrePersist and @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);
    }
}
Lars Gendner
  • 1,816
  • 2
  • 14
  • 24
  • 3
    Perhaps you should mention to the OP what is this `@ListIndexBase`, since it is NOT part of JPA, and so the application would be non-portable. Also the OP doesn't say which JPA provider is used, so you must have some prescient knowledge ... –  Nov 07 '17 at 13:45
0

Solution:

As the most simple solution, I made another table that make a total, and before save I get the value e add+1.