title: Spring JPA: Saving An entity having association with an entity which is sort of master table
body: <p>I have following entities and their relationship in database tables.</p>
  
  <p>Entity <strong>TransactionType</strong> which will contain a master set of data which will never change/or rarely will get changed.</p>
  
  <pre><code>@Entity
  @Table(name="ONB_TRANS_TYPE")
  @Cacheable(true)
  public class TransactionType implements Serializable {
  
      private static final long serialVersionUID = -6630648311619744810L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Integer ID;
  
      @Column(name = "TRANS_TYPE_NAME",unique=true)
      private String transTypeName;
  
      //getters and setters
  
  }
  </code></pre>
  
  <p>Another Enitity <strong>CandidateTransaction</strong> which has a foreign key from TransactionType, in table the relationship between the two tables is the normal Pk-FK relationship.</p>
  
  <pre><code>@Entity
  @Table(name="ONB_CANDIDATE_TRANS")
  public class CandidateTransaction implements Serializable{
  
      private static final long serialVersionUID = 3615632069112078119L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Integer ID;
  
      //FK to ONB_Candidate_Info table
      @Column(name="CANDIDATE_ID")
      private String candidateId;
  
      private UUID TRANS_IDENTIFIER;
  
  
      @OneToOne 
      @JoinColumn(name="TRANS_TYPE_ID")
      private TransactionTypeDTO transactionType;
  }
  </code></pre>
  
  <p>I have made a Unidirectional OneToOneMapping between two entities.
  Now when I want to persist an object of CandidateTransaction, I am doing following while saving and it throws me an error stating you cannot insert null in TRANS_TYPE_ID.
  What I want to ask is do I have to explicitly fetch the Trans_TYPE_ID from Master Table (ONB_TRANS_TYPE) or I shouldn't ORM do this ?? As I have given the mapping ??</p>
  
  <pre><code>@Service
  public class CandidateTransactionManagerImpl implements CandidateTransactionManager {
  
      @Autowired
      private CandidateTransactionRepository candidateTransactionRepository;
  
      @Autowired
      private TransactionTypeRepository transactionTypeRepository;
  
      @Override
      public void saveTransactionProgress(String candidateId,CandidateProgressRequestBO candidateProgressRequestBO) throws ONBException {
  
          CandidateTransaction savedInstance = new CandidateTransaction();
  
          TransactionType transactionType ;
  
          CandidateTransaction candidate;
          if(CollectionUtils.isEmpty(candidateProgressRequestBO.getStepStatus()))
              throw new ONBException("No Transaaction to Save! Transaction Object Empty",new Exception());
          else {
              for(CandidateProgressBO candidateProgress : candidateProgressRequestBO.getStepStatus()) {
                  transactionType = new TransactionType();
  
                  candidate = new CandidateTransaction();
                  candidate.setCandidateId(candidateId);
  
                  transactionType.setTransTypeName(candidateProgress.getStep().name());
  
                  candidate.setTransactionType(transactionType);
                  candidate.setMODIFIED_DATE(new Date());
                  savedInstance = candidateTransactionRepository.save(candidate);
  
                  System.out.println("Saved Isntance "+savedInstance);
              }
          }   
  
      }
  </code></pre>