A implicit @Id for JPA for pure POJO ?

V

Vincent Cantin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top