A implicit @Id for JPA for pure POJO ?

Discussion in 'Java' started by Vincent Cantin, Dec 24, 2006.

  1. 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
    Vincent Cantin, Dec 24, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hilz
    Replies:
    9
    Views:
    3,148
    P.Hill
    Jul 27, 2004
  2. Scott Balmos
    Replies:
    1
    Views:
    647
    Steve W. Jackson
    Aug 31, 2005
  3. mdjj
    Replies:
    0
    Views:
    735
  4. Todd Aspeotis
    Replies:
    3
    Views:
    458
    Kanenas
    May 30, 2005
  5. Replies:
    4
    Views:
    782
    Ben C
    Mar 29, 2008
Loading...

Share This Page