4 Replies Latest reply on Apr 13, 2007 2:50 AM by pkraemer

    Question @Inheritance

    pkraemer

      Well it's me again with a question for using @Inheritance

      Well I got one super Entity:

      @Entity
      @Table(name="users")
      @SequenceGenerator(name = "user_sequence", sequenceName = "user_id_seq")
      @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
      public class User implements Serializable {
      
       private static final long serialVersionUID = 1487970457796785528L;
       @Id
       @Column(name="u_id",nullable=false)
       @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_sequence")
       private Long id;
      ....
      }
      


      and a child class

      @Entity
      @Table(name="registered_user")
      public class RegisteredUser extends User implements Serializable{
      
       private static final long serialVersionUID = 2027308984532828735L;
       ....
      }
      


      with no specific id.

      Both entities are represented by an own table. The Primary Key from the parent table is also used as primary key in child table (mapped with foreign key)

      When I try to persist a child object I got an Error:


      18:19:46,968 ERROR [JDBCExceptionReporter] ERROR: insert or update on table "registered_user" violates foreign key constraint "fk22ff4768223777df"
      Detail: Key (u_id)=(1600) is not present in table "users".
      18:19:46,968 ERROR [AbstractFlushingEventListener] Could not synchronize database state with session


      How can I tell the manager to persist the attributes in my parent table? Perhaps with cascade strategy? And where should I put it?

      Thx a lot

        • 1. Re: Question @Inheritance

          Have you developed this application with hibernate.hbm2ddl.auto = update?

          If so, you have probably old constraints lying around from the time when you used InheritanceType.JOINED. Either clean them out manually or drop the whole schema and have it recreated.

          Regards

          Felix

          • 2. Re: Question @Inheritance
            pkraemer

            Thx for your help
            Yes I use hibernate.hbm2ddl.auto = update but I did not try to use InheritanceType.JOINED. That was the problem. If I change it, it works fine.

            • 3. Re: Question @Inheritance

              Well, do you WANT to use InheritanceType.JOINED?

              I suspect the problem is that the constraint required for JOINED inheritance is not dropped when you switch to single table.

              Regards

              Felix

              • 4. Re: Question @Inheritance
                pkraemer

                Well, I'm new to EJB so now I'm a little bit confused.

                Is there any problem to use Inheritance.JOINED?

                I think, SINGLETABLE means all attributes in both entity beans are in one database table.

                But there is one table for users (parent entity) and one table for registered users (child entity).
                When I now try to persist a new RegisteredUser the Id should be generated by parent an mapped as key to registered users table.
                In the end I have an entry in users table and in registered users table.