1 Reply Latest reply on Apr 14, 2006 2:05 PM by tvanbuskirk

    Entity Bean Foreign Key Problem, Need to Specify PK length?

    tvanbuskirk

      Hi All,

      I'm working with entity beans, and I've run into a problem with accessing foreign keys that I cannot figure out for the life of me. The error I'm getting is:

      08:35:36,962 ERROR [SchemaUpdate] Unsuccessful: create table cats_dogs (cats tinyblob not null, dogs tinyblob not null, primary key (dogs))
      08:35:36,963 ERROR [SchemaUpdate] BLOB/TEXT column 'dogs' used in key specification without a key length


      I appreciate your help!! I am using JBoss 4.0.4.CR2 with EJB 3.0 RC5 and Mysql 5 is my datasource.

      Below is the listing of the problem.

      I have three entity beans. Two beans are straight forward. If I deploy these two beans by themselves, everything works fine. The third uses @ManyToOne to access the primary keys of the first two beans, and set them as it's own primary keys. This is where the above problem occurs.

      Here is the first straight forward entity bean:
      @Entity
      @Table(name = "cats")
      public class Cats implements Serializable
      {
       private int id;
       private String name;
       private String description;
      
       public Cats() {}
      
       public Cats(String name,String description)
       {
       this.name = name;
       this.description = description;
       }
      
       @Id
       @GeneratedValue
       public int getId ()
       {
       return id;
       }
      
       public void setId (int id)
       {
       this.id = id;
       }
      
       public String getDescription()
       {
       return description;
       }
      
       public void setDescription(String description)
       {
       this.description = description;
       }
      
       public String getName()
       {
       return name;
       }
      
       public void setName(String name)
       {
       this.name = name;
       }
      
      }


      Here's the other straight forward entity bean:

      @Entity
      @Table(name = "dogs")
      public class Dogs implements Serializable
      {
       private int id;
       private String name;
      
       // DogType is an enumeration
       private DogType type;
      
       private int distance;
       private String description;
      
       public Dogs() {}
      
       public Dogs(String name, DogType type, int distance, String description)
       {
       this.name = name;
       this.type = type;
       this.distance = distance;
       this.description = description;
       }
      
       public String getDescription()
       {
       return description;
       }
      
       public void setDescription(String description)
       {
       this.description = description;
       }
      
       public int getDistance()
       {
       return distance;
       }
      
       public void setDistance(int distance)
       {
       this.distance = distance;
       }
      
       @Id
       @GeneratedValue
       public int getId()
       {
       return id;
       }
      
       public void setId(int id)
       {
       this.id = id;
       }
      
       public String getName()
       {
       return name;
       }
      
       public void setName(String name)
       {
       this.name = name;
       }
      
       public DogType getType()
       {
       return type;
       }
      
       public void setType(DogType type)
       {
       this.type = type;
       }
      
      }
      


      The entity bean with the problem is the following:

      
      @Entity
      @Table(name = "cats_dogs")
      public class CatsDogs implements Serializable
      {
       private Cats cats;
       private Dogs dogs;
      
       public CatsDogs() {}
      
       public CatsDogs(Cats cats, Dogs dogs)
       {
       this.cats = cats;
       this.dogs = dogs;
       }
      
      
       @Id
       @ManyToOne
       @JoinColumn(name="cats_id",referencedColumnName="id")
       public Cats getCats()
       {
       return cats;
       }
      
       public void setCats(Cats cats)
       {
       this.cats = cats;
       }
      
       @Id
       @ManyToOne
       @JoinColumn(name="dogs_id",referencedColumnName="id")
       public Dogs getDogs()
       {
       return dogs;
       }
      
       public void setDogs(Dogs dogs)
       {
       this.dogs = dogs;
       }
      
      }
      
      
      


      Thanks in advance!!!

        • 1. Re: Entity Bean Foreign Key Problem, Need to Specify PK leng
          tvanbuskirk

          I've found that the problem is determining the primary key of my CatsDogs class. I want my combined primary key to be the primary key of Cats and the primary key of Dogs. I changed my code to:

          @ManyToOne(optional=false)
          @PrimaryKeyJoinColumn(name="cats_id",referencedColumnName="id")
          public Cats getCats()
          {
           return cats;
          }
          
          @ManyToOne(optional=false)
          @PrimaryKeyJoinColumn(name="dogs_id",referencedColumnName="id")
          public Dogs getDogs()
          {
           return dogs;
          }
          


          However then JBoss complains that there is no primary key for this class. If I add

          @Id
          tags to the above methods, I get the same error posted above.

          So, apparently the only solution is to add an additional ID field for the CatsDogs class that would be the primary key. It would then look like:

          @Id
          public int getId()
          {
           return id;
          }
          ...
          @ManyToOne(optional=false)
          @PrimaryKeyJoinColumn(name="cats_id",referencedColumnName="id")
          public Cats getCats()
          {
           return cats;
          }
          ...
          @ManyToOne(optional=false)
          @PrimaryKeyJoinColumn(name="dogs_id",referencedColumnName="id")
          public Dogs getDogs()
          {
           return dogs;
          }
          
          


          This would take my database out of Third Normal Form and I don't want to do that.

          Does anyone know how to fix this problem???

          Thanks!