A implicit @Id for JPA for pure POJO ?
Hi,
I have some POJO's classes that I want to persist. Most of them need to be
saved as an @Entity since their instances need to be shared by some objects
and not simply duplicated.
But for most of them I don't want to add them a field specially for the
primary key (@Id) because those object won't be queried by the users via
their primary key but via their reference in other objects, and because I
want my POJO to stay POJO.
In other words, here is what I have now :
@Entity
public class GameEntity implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public long id;
// Some instances of Color are shared between the game entities.
public Color color;
}
@Entity
public class Color implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id; // I don't need/want that field my Java class !!!! :-(
public float r;
public float g;
public float b;
}
And here is what I would have, an implicit @id field that is not defined in
my POJO class:
@Entity
@ImplicitId(type="long", name="id") // ... something like that
public class Color implements Serializable {
public float r;
public float g;
public float b;
}
.... where the field "id" is in the DB but not in the class of my entity, and
where the entity manager keep track himself of the key of my color instances
with a kind of Map<PojoObject, PojoKey>. In this way, the primary key is not
invasive in my POJO code.
Is it possible ? How ?
Regards,
Vincent