I have a scenario where there is a DB column which should be like sequence for each primary key + another column. The column's value should again reset and start from 1 for another.
For example:
col1(PK)------col2(FK)-------my-column
0---------------0-------------------1
1---------------0-------------------2
2---------------0-------------------3
3---------------1-------------------1
4---------------1-------------------2
5---------------1-------------------3
For col1(PK), I can simply annotate with @GeneratedValue(strategy = GenerationType.IDENTITY)
In the same way, I want to generate value for my-column as well.
From the code side, This is what it looks like:
classA{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private String col2;
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "classa", orphanRemoval = true)
private List<ClassB> listB = new ArrayList<>();
//I want to generate/increment the value of myCol each time based on value of col1 & col2
@Column(name = "my-col")
private Integer myCol;
//getter/setters
}
ClassB{
@Id
@Column(name = "col1")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer col1;
@ManyToOne
@JoinColumn(name = "col2")
private ClassA classa;
//getter/setter
}
ClassAorClassB? Your table names is bad practices.