EmbeddedId with Generated field value

#1

I know the following 2 ways to declare a composite Primary key:

********* Solution 1 *********

@Entity @IdClass(CompositePK.class) public class A {
    @Id private long uid;
    @Id private String zone;
}

public class CompositePK {
    private long uid;
    private String zone;
}

********* Solution 2 *********

@Entity public class A {

@EmbeddedId private CompositePK pk;
}

@Embeddable public class CompositePK {
    private long uid;
    private String zone;
}

In both solutions I expected I could get a generated value for the field uid by simply adding the documented annotation @GeneratedValue like the following:

Solution 1) class A { @Id @GeneratedValue private long uid; ... }

Solution 2) class CompositePK { @GeneratedValue private long uid; ... }

Actually I have several Entity classes (A, B, C, D, etc.) using the same CompositePK, so I'd prefer Solution 2 to have a more compact coding. But unfortunately the only working one is Solution 1.

Is this an issue or simply a not contemplated future?

#2

Automatic generated IDs is currently supported only for entity objects (and not for embeddable classes).

Consider using:

@IdClass(ZoneId.class)
@Entity public static class A {
    @Id @GeneratedValue public long id;
    @Id public String zone;
}

public static class ZoneId {
    public long id;
    public String zone;
}
ObjectDB Support
#3

Okay, I'm going to fill all my classes with redundant fields and their relative getters and setters. sad

But at this point has ZoneId class (or CompositePK class) to be declared as @Embeddable static class with annotated fields? I believe declaring it like the following should be the same:

public class ZoneId {
    public long id;
    public String zone;
}

Do you advise differently?

Thank you

 

#4

You are right. Annotations are not required in the ID class and the example above was fixed accordingly.

Maybe you can inherit the common IDs from a common super entity class / mapped superclass.

ObjectDB Support
#5

It's a good suggestion. But this way how objectDB would count each object instances? Would it be 1 or 2 units for each derived object? I wouldn't exceed the 1 million limit ...

Thank you

#6

Every object is count once even if it is an instance of a class with a deep inheritance.

ObjectDB Support
#7

Perfect. Then I can proceed this way. laugh

Many thanks and regards.

 

 

Reply