0

An application is developed with JSF and JPA.

@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)

Bill is the Parent class and subclassed to OpdBill, PharmacyBill, etc.

Multiple users may try to persist at the same time.

It is easy to generate sequence number for the Bill class, but that is not essential in the functionality. How can we have separate auto-generated sequence numbers for sub classes? (not necessary the ID, just a serial number)

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Buddhika Ariyaratne
  • 2,339
  • 6
  • 51
  • 88
  • If OpdBill is also a Bill, then an OpdBill-instance would have two IDs? Is that what you want? – stg Nov 14 '12 at 22:53
  • OPD is a separate department. They want separate sequence numbers. Lab is a separate department, they want there own numbers. Although the management analyze all the bills together in most of the reports, there is no need to have a separate sequence number for Bill. I use single table inheritance. – Buddhika Ariyaratne Nov 14 '12 at 23:06
  • @BuddhikaAriyaratne Your departments are being dumb. Sequence numbers should be more or less arbitrary, and the only constraint you can guarantee is that they'll be strictly monotonously increasing. For instance, it's impossible to guarantee there won't be any gaps in the ID sequence. Aborted transactions will cause those, and the only way to prevent them is to make database operations synchronised and kill performance. – millimoose Nov 14 '12 at 23:37

2 Answers2

3

As far as i know JPA is not able to create such Sequences for non ID-Columns, but you could do some dirty workaround like this:

Generate a secound entity like

@Entity
public class OpdSequence {
  @Id
  @GeneratedValue
  private Long id;
}

and create a 1-1 to your OpdBill

@Entity 
public class OpdBill extends Bill {
  @OneToOne
  private OpdSequence opdId;
}
stg
  • 2,757
  • 2
  • 28
  • 55
  • Of course this isn't really JPA inheritance anymore. (It accomplishes the same, is a valid workaround, and it's arguably the preferable pattern overall, but it's not the feature itself.) – millimoose Nov 14 '12 at 23:33
1

If OpdBill is a Bill, then both must share the same ID field, and Hibernate must be able to distinguish two bills (whatever their class is) thanks to their IDs. All the subclasses must thus share the same ID generation with the base class, and the IDs of all the instances of Bill and its subclasses must be unique.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Does that mean that with single inheritance, we can not get a unique number (not essentially the ID) for subclasses? – Buddhika Ariyaratne Nov 14 '12 at 23:10
  • @BuddhikaAriyaratne It's not possible. Imagine the following scenario: you have the entities `Dog` and `Cat`, that inherit from `Animal`. Now you try to get an instance of `Animal` by ID. What should the result be? (Obviously it's possible to come up with an unambiguous behaviour – like raising an error – but the question is whether it'd be useful or worth implementing.) – millimoose Nov 14 '12 at 23:30