Uploaded image for project: 'Spring Roo'
  1. Spring Roo
  2. ROO-2366

@OneToOne relationships - accessor generation for back reference

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Duplicate
    • Affects Version/s: 1.1.4.RELEASE
    • Fix Version/s: 1.1.4.RELEASE
    • Component/s: PERSISTENCE
    • Labels:
      None
    • Environment:
      GAE

      Description

      This ticket describes the same problem as this one : https://jira.springsource.org/browse/ROO-2350

      My script :

      project --topLevelPackage com.test
      
      persistence setup --provider DATANUCLEUS --database GOOGLE_APP_ENGINE 
      
      entity --class ~.User --permitReservedWords
      field string --fieldName name --notNull 
      
      entity --class ~.Account --serializable 
      field string --fieldName type --notNull
       
      field set --fieldName user --type ~.User --cardinality
      
      field reference --fieldName account --type ~.Account --class ~.User --cardinality ONE_TO_ONE
      
      field reference --fieldName linkedUser --type ~.User --class ~.Account --cardinality ONE_TO_ONE
      

      The corresponding classes:

      Account.java
      package com.test;
      
      import ...
      
      @RooJavaBean
      @RooToString
      @RooEntity
      @RooSerializable
      public class Account {
      
          @NotNull
          private String type;
      
          @OneToOne
          private User linkedUser;
      }
      
      
      User.java
      package com.test;
      
      import ...
      
      @RooJavaBean
      @RooToString
      @RooEntity
      public class User {
      
          @OneToOne
          private Account account;
      }
      
      

      The generated accessors in javaBean classes :

      Account_Roo_JavaBean.aj
          public User Account.getLinkedUser() {
              if (this.linkedUserId != null) {
                  this.linkedUser = User.findUser(this.linkedUserId);
              } else {
                  this.linkedUser = null;
              }
              return this.linkedUser;
          }
          
          public void Account.setLinkedUser(User linkedUser) {
              if (linkedUser != null) {
                  if (linkedUser.getId () == null) {
                      linkedUser.persist();
                  }
                  this.linkedUserId = linkedUser.getId();
              } else {
                  this.linkedUserId = null;
              }
          }
          
      }
      
      User_Roo_JavaBean.aj
             public Account User.getAccount() {
              if (this.accountId != null) {
                  this.account = Account.findAccount(this.accountId);
              } else {
                  this.account = null;
              }
              return this.account;
          }
          
          public void User.setAccount(Account account) {
              if (account != null) {
                  if (account.getId () == null) {
                      account.persist();
                  }
                  this.accountId = account.getId();
              } else {
                  this.accountId = null;
              }
          }
          
      }
      

      As described in ticket 2350, it would be nice if the generated accessors would be :

      Account_Roo_JavaBean.aj
          public User Account.getLinkedUser() {
              if (this.linkedUserId != null) {
               if (this.linkedUser == null || this.linkedUser.getId() != this.linkedUserId) {
                  this.linkedUser = User.findUser(this.linkedUserId);
               }
              } else {
                  this.linkedUser = null;
              }
              return this.linkedUser;
          }
          
          public void Account.setLinkedUser(User linkedUser) {
              if (linkedUser != null) {
                  if (linkedUser.getId () == null) {
                      linkedUser.persist();
                  }
                  this.linkedUserId = linkedUser.getId();
              } else {
                  this.linkedUserId = null;
              }
             this.linkedUser = linkedUser;
          }
          
      }
      
      User_Roo_JavaBean.aj
          public Account User.getAccount() {
      
              if (this.accountId != null) {
                  if (this.account == null || this.account.getId() != this.accountId) {
                      this.account = Account.findAccount(this.accountId);
                  )
              } else {
                  this.account = null;
              }
              return this.account;
          }
          
          public void User.setAccount(Account account) {
              if (account != null) {
                  if (account.getId () == null) {
                      account.persist();
                  }
                  this.accountId = account.getId();
              } else {
                  this.accountId = null;
              }
              this.account = account;
          }
      }
      

      Thank you in advance

        Attachments

          Activity

            People

            Assignee:
            stewarta Alan Stewart
            Reporter:
            pierrette Pierrette Bertrand
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: