0 Replies Latest reply on May 27, 2006 3:20 PM by Serge Redchuk

    Is there any way to define derived entity class for database

    Serge Redchuk Newbie

      Hello, all!

      Sounds easy but not appear so in practice...

      Let's say I have entity class User with @id Long id; and @Column String name;.

      And I have other table with user profile data.
      The columns: user_id, name, value.

      One of profile names is 'NICKNAME'.
      So to store nickname Brutt of user with id 1 I create profile record:
      1, "NICKNAME", "Brutt".

      Now what if I need to show some users with nicknames ordered by nickname?
      I created a view as

      SELECT u.*, p.value as nickname FROM u users, p profiles WHERE p.user_id = u.id AND p.name = 'NICKNAME'


      I defined entity class for the view like this:
      class UserNicknameView extends User {


      And for User I'm using inheritance type TABLE_PER_CLASS.

      It works fine until...

      After I fetch a List of UserNicknameView, the UserNicknameView instances are remembered by Entity Manager with some @Id's (derived from User).

      Then after try to update User with the same @Id that was already remembered I have exception about I want to merge wrong instance.

      Also I can't override @id in the UserNicknameView by @id nor @EmbeddedId.

      Any suggestion?

      I'd like to keep the User <- UserNicknameView derivation.

      Great thanks to any attention.