3 Replies Latest reply on Sep 14, 2007 9:35 AM by waynebaylor

    Single Inheritance / ConstraintViolationException  / Postgre

    toni

      Hi,

      using a single table inheritance strategy with postgres 7.3 always causes a ConstraintViolationException when I try to merge the changes.

      I first create a new enity bean, which works. Then I add it to a OneToMany collection of another entity bean and try to merge the change. This is when I get a ConstraintViolationException.

      Here is the setup:

      @Entity
      @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
      
      public class Person implements Serializable
      {
       @Id
       @GeneratedValue(strategy = GenerationType.IDENTITY)
       long id;
      ..
      }
      


      @Entity
      @Name("junior")
      
      public class Junior extends Person
      {
      ...
      }
      


      @Entity
      @Name("school")
      
      public class School implements Serializable
      {
       @Id
       @NotNull
       @Length(min = 5, max = 40)
       String schoolName
      
       @OneToMany(cascade = {CascadeType.ALL})
       List<Juniors> juniors = new ArrayList<Juniors>();
      
       @OneToMany(cascade = {CascadeType.ALL})
       List<Softmore> softmores = new ArrayList<Softmore>();
      
       @OneToMany(cascade = {CascadeType.ALL})
       List<Senior> seniors = new ArrayList<Senior>();
      ...
      



      The following code always throws a ConstraintViolationException

       // works
       entityManager.persist(junior);
       // this causes a contraint violation
       school.getJuniors().add(junior);
       school = entityManager.merge(rewriteConfiguration);
      


      The reason why this happens, is because the columns of the database table, which keeps track of the OneToMany relationships are all defined to be NOT NULL. Here comes the table:

      
      school_schoolname character varying(40)
      NOT NULL
      
      juniors_id bigint
      NOT NULL
      
      softmores_id bigint
      NOT NULL
      
      seniors_id bigint
      NOT NULL
      
      


      However, all those columns SHOULD be nullable, but hibernate does not set them up like this. How can I make sure hibernate does this?