views:

58

answers:

2

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.

A: 

First, why do you say that the counter value should be set "upon save"? That's going to be tough. I'd expect that instead, you'd want the value to be set at the point you attach an organization to a Foobar.

Second, as Paul Keeble noted in a comment, yes, this value appears to be Organization-related, and so belongs in that class.

You're going to need to create a method to associate Foobars with Organizations (as opposed to simply setting a member variable). This method will have to set the counter. So in Foobar:

   public void associateWithOrganization (Organization org) {
      this.organization = org;
      org.addAssociation (this);
   }

and in Organization:

   public void addAssociation (Foobar foo) {
      this.associationCounter ++;
   }
   ...

You'll also need "getNumberOfAssociationsForThisOrganization()" and "removeAssociation(this)" methods, of course.

CPerkins
You guys misunderstood the purpose of the counter, of course, because I named it badly. The counter isn't supposed to tell "how many Foobars there are in Organization", it is rather supposed to be the order number of the Foobar in an organization, "This is the 3rd Foobar in Organization X", hence, it belongs to Foobar and not to Organization
Kim L
A: 

I got it. Despite the comments, orderNr indeed belongs to Foobar rather than to Organization.

However, I feel that this should be implemented in a different way. I don't think that Hibernate directly supports what you want. Easily, at least.

I would add a timestamp to Foobar, which would hold the time when the instance was created. Then, you could find the actual orderNr by running a query like

"SELECT COUNT (*) FROM Foobar WHERE organization = :myOrg AND timestamp < :myTimeStamp"

Additionally, I'd probably have Organization have a List<Foobar> (ie, by mapping it with @OneToMany(mappedBy = "organization"). Of course, perhaps you don't need it.

André Neves