1 Reply Latest reply on Apr 4, 2012 4:58 PM by Cdelahun-Oracle

    Foreign key not created in database

    luno
      Hi,

      I am trying to create a master detail relationship using two tables with JPA.
      Master table:
      @Entity(name = "Person")
      @Table(name = "PERSON")
      public class Person
      {
        @Id
        @SequenceGenerator(name = "PERSON_GENERATOR", sequenceName = "SEQ_PERSON", allocationSize = 1)
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "PERSON_GENERATOR")
        private long id;
      
        @Column(name = "person_name")
        private String name;
      
        @OneToMany(mappedBy = "person", cascade = CascadeType.ALL)
        private List<Car> cars;
      
        //getter and setter following...
      }
      Detail table:
      @Entity(name = "Car")
      @Table(name = "CAR", uniqueConstraints = @UniqueConstraint(columnNames =
      {"car_name", "person_id"}))
      public class Car
      {
        @Id
        @SequenceGenerator(name = "CAR_GENERATOR", sequenceName = "SEQ_CAR", allocationSize = 1)
        @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "CAR_GENERATOR")
        private long id;
      
        @Column(name = "car_name")
        private String carname;
      
        @ManyToOne
        private Person person;
      
        //getter and setter following...
      }
      The tables are created in the database, even the unique constraint is set to the "CAR" table, but not the foreign key constraint.
      So I am missing a statement like this:
      CONSTRAINT "FK_PERSON_CAR" FOREIGN KEY ("PERSON_ID") REFERENCES PERSON ("ID") ENABLE
      Someone knows why?

      Kind regards
      luno
        • 1. Re: Foreign key not created in database
          Cdelahun-Oracle
          EclipseLink supports adding foreign key constraints using Alter table statements, so if your database allows constraints differently, support might be turned off for that platform. If so, please file an enhancement to have it supported for your database platform. If not, what database are you using and what gets generated for the tables?

          Best Regards,
          Chris