Hi
I have the following class
@Entity
public class Foobar {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
private Organization organization;
private Long orderNr;
}
What I'm trying to do is to create an incrementing order number for this object. The problem is that the orderNr should be unique for each organization. Example, let's say we have two organizations (let's call them org1 and org2). First we create an instance of Foobar and set the organization to org1. Upon save, the orderNr's value should become '1'. Then we'll create another instance of Foobar and once again set the organization to org1. The orderNr should now get the value '2' (for this specific instance of Foobar, the first Foobar should still have the value '1'). Then we create yet another instance of Foobar, but this time we set the organization to org2, now the orderNr should get the value '1', since this was the first object for org2.
What is the easiest way to accomplish this? Is there anything in JPA or Hibernate that allows me to do something like this easily (by using annotations) or do I have to manually keep track of the counter values?
==== Made some edits because the question was misunderstood.