Im working with an existing circuit that Id would rather not change. A schema has a one-to-one relationship between the Person and VitalStats tables, where Person has a primary key, and VitalStats uses the same field as its primary key and its foreign key for Person, which means its value is the value of the corresponding person PK.
These records are created by external processes, and my JPA code never needs to update VitalStats. For my Id object model, such as my Person class, to contain a VitalStats element, BUT:
When i try
@Entity public class Person{ private long id; @Id public long getId(){ return id; } private VitalStats vs; @OneToOne(mappedBy = "person") public VitalStats getVs() { return vs; } } @Entity public class VitalStats{ private Person person; @OneToOne public Person getPerson() { return person; } }
I have a problem with VitalStats not having @Id, which does not work for @Entity. \
If i try
@Id @OneToOne public Person getPerson() { return person; }
which solves the @Id problem but requires that Person be Serializable. OK, back to that.
I could make VitalStats @Embeddable and associate it with Person using the @ElementCollection element, but then it will need to be obtained as a collection, although I know that this is only one element. An opportunity, but also a little annoying and a bit confusing.
So, what prevents me from simply saying that the Person implements Serializable? Nothing, really, except that I like everything in my code to be there for some reason, and I don't see any logic in it, which makes my code less readable.
In the meantime, I just replaced the Person field in VitalStats with a long personId and made VitalStatss @Id, so @OneToOne now works.
All these solutions, which seem to me like simple problems, are a bit awkward, so I wonder if I am missing something or someone can at least explain to me why Person should be Serializable.
TIA