3 Replies Latest reply on Feb 3, 2002 4:45 PM by Dan Ciarniello

    Referential Integrity question

    Dan Ciarniello Newbie

      I am just starting to look at CMR and something is puzzling me. I'm hoping that someone can shed some light on the issue.

      I have a basic one-to-many relationship between two tables:

      TeamID (PK)
      Team properties

      PlayerID (PK)
      TeamID (FK)
      Player properties

      Various examples that I've seen dealing with 1:n relationships show that to create a player that is a member of a team, I must do something like:

      player = playerhome.create(playerproperties);

      What I find puzzling is that there is no way to force a player to be added to a team. If I forget to call team.addPlayer, the player is created but is left dangling.

      Essentially, this means that the foreign-key relationship is not required (i.e. PLAYER.TeamID is allowed to be NULL). I haven't been able to figure out how to set up the deployment descriptors and classes so the the foreign-key must be set.

      Is there anyway to do this? If the spec does not allow for this, why not? It seems to me that one is forced to use a bit of BMR in what is supposed to be CMR.


        • 1. Re: Referential Integrity question
          Mika Kuusela Newbie


          You didn't specify which version of JBoss you are using. In 3.0.0alpha there's a file called jbosscmp-jdbc.xml and in 2.4.X a file called jaws.xml. With one of these files, it's possible to configure your datasource to enforce referential integrity. Just create one of these files depending of your JBoss version and put it in your ejb-jar -file in META-INF -directory.

          You find instructions how to create these files in the DTD:s. There's good comments in those files.

          Hope this helps,


          • 2. Re: Referential Integrity question
            Åsmund Hjulstad Newbie

            You could always to something like

            playerhome.create(properties, team)

            and then in your entitybean have

            public PlayerPK ejbCreate(Properties p, Team t) {
            return null;

            public void ejbPostCreate(Properties p, Team t) {

            The crucial point being to set the relation in the ejbPostCreate...  I do this all the time.

            The other alternative, of course, is to have a createPlayer method on the Team entity bean. I chose not to do this, because this means mixing create and business methods in the local/remote interface.

            Åsmund Hjulstad

            • 3. Re: Referential Integrity question
              Dan Ciarniello Newbie

              I am using jboss 3.0 alpha so I've set up the relationships in jbosscmp-jdbc.xml. I did not realize that DTDs were even available since none of the JBoss deployment XML files that I have ever seen have a DOCTYPE element. However, I've now done some poking around and discovered that the DTDs are at http://www.jboss.org/j2ee/dtd.

              It seems that I was interpreting something in the spec a little too literally. The spec states that you can set CMR fields in ejbPostCreate so I, naively, tried do so something like setTeamID(teamid) which, of course, does not work while Åsmund's does.

              It is still slightly unsatisfying to me that the way the spec is written, one cannot make foreign keys NOT NULL since the insertion of the data and the setting of the foreign key are done in separate steps. However, my dissatisfaction is probably due more to my own prejudices than anything else.