2 Replies Latest reply on Jul 1, 2004 3:46 PM by mzint

    Create fails - CMP, CMR & Compound PK

    mzint

      Hi there,

      I'm pretty new to this, so maybe someone could help me with this.

      I get the following error message when I try to create a new entry.
      This is just an example database consisting of two tables Moons and Planets.

      The MoonEJB contains the fields planet, moon, radius and diameter and a foreign key relationship to the table Planets.

      When I call the function:

      /**
       * @ejb.interface-method view-type = "remote"
       * @jboss-net.web-method
       * @jboss-net.wsdd-operation
       */
       public void addMoon(String pl, String mo, int ra, int di){
      
       try {
       moHome = getMoonLocalHome();
       moHome.createMoon(pl,mo,ra,di);
       } catch (Exception e) {
       e.printStackTrace();
       }
       }


      I get the following Error Message ->
      Could not create entity
      java.sql.SQLException: Try to insert null into a non-nullable column in statement [INSERT INTO MOONS (PLANET, MOON, RADIUS, DIAMETER, planet) VALUES ('Earth', 'newMoon', 1000, 2000, NULL)]


      I presume the error is due to the fact that I haven't set the relationship between the Moon and the corresponding planet, when I create a new moon.

      The MoonEJB contains the following create method:

      /**
       * @return
       * @throws CreateException
       *
       * @ejb.create-method
       */
       public MoonPK ejbCreateMoon(String planet, String moon, int radius, int diameter) throws CreateException{
       setPlanet(planet);
       setMoon(moon);
       setRadius(radius);
       setDiameter(diameter);
       return null;
       }
      
       public void ejbPostCreateMond(String planet, String moon, int radius, int diameter){ }


      The CMP fields are defined like this:
      /**
       * @return
       * @ejb.persistent-field
       * @ejb.persistence column-name = "PLANET"
       * @ejb.interface-method view-type = "local"
       */
       public abstract String getPlanet();
       public abstract void setPlanet(String planet);


      and the CMR is defined as follows:
      /**
       *
       * @return
       *
       * @ejb.interface-method
       * @ejb.relation name = "PlanetAndMoonRelation"
       * role-name = "PlanetHasMoons"
       * target-role-name = "MoonsHavePlanet"
       * target-ejb = "Planets"
       *
       * @jboss.relation fk-column = "planet"
       * related-pk-field = "planet"
       * fk-constraint = "true"
       */
       public abstract PlanetsLocal getPlanetRel();
       public abstract void setPlanetsRel(PlanetLocal planet);


      Any suggestions ??

      thanks
      matt

        • 1. Re: Create fails - CMP, CMR & Compound PK
          mzint

          Ok - I thought I knew what to do, but that doesn't work as well.

          I extended the create method with an additional parameter, the relationship field, which should be initialized in the ejbPostCreate method, but I still get the same error message. I inserted System.out.println's to check which method gets called first and the ejbPostCreate method is called after the ejbCreate method. Shouldn't it be the other way around ??

          /**
           * @return
           * @throws CreateException
           *
           * @ejb.create-method
           */
           public MoonPK ejbCreateMoon(String planet, String moon, int radius, int diameter, PlanetLocal plocal) throws CreateException{
           setPlanet(planet);
           setMoon(moon);
           setRadius(radius);
           setDiameter(diameter);
           return null;
           }
          
           public void ejbPostCreateMond(String planet, String moon, int radius, int diameter, PlanetLocal plocal){
           setPlanetCMR(plocal);
           }


          • 2. found Solution in previous posting
            mzint