persistence icon indicating copy to clipboard operation
persistence copied to clipboard

Auto generate record ID class if no explicit `@IdClass`

Open quaff opened this issue 1 year ago • 3 comments

Given an entity meets:

  1. using composite primary key
  2. no @IdClass annotated

For example:

   @Entity
   public class Employee {
      @Id String empName;
      @Id Date birthDay;
      ...
   }

It would be convenient if a record class is generated as implicit @IdClass

   public record EmployeeId(String empName, Date birthDay) {}

candidate names of generated record class:

  • EmployeeId
  • EmployeePK
  • EmployeeId_
  • EmployeePK_
  • Employee$Id
  • Employee$PK

quaff avatar Nov 07 '24 01:11 quaff

It's an interesting idea.

candidate names of generated record class

I kinda hate those suggestions though :)

My knee-jerk would be an inner class of Book_, something like Book_.Id_ which does not collide with anything currently defined in 5.1.1. But it does have one potential problem in that in Hibernate Processor, IIRC, we have started using the convention X_.Y_ when you have a static inner class annotated @Entity (which is permitted by the spec, but currently not considered in 5.1.1). Right, @cigaly?

gavinking avatar Nov 08 '24 09:11 gavinking

@gavinking Actually, what I've done was generating separate class file for each (static) inner class, so that X.Y will produce X_Y_. [But there should be not big problem to make it as inner class X_.Y_] Why not generate something like Book_.Id [or Book_Id] by simply ommiting underscore at the end, so that distinction will be more clear?

cigaly avatar Nov 08 '24 09:11 cigaly

I'm voting for Book_.Id.

quaff avatar Nov 08 '24 09:11 quaff