5 Replies Latest reply: Mar 1, 2013 7:35 AM by r035198x RSS

    help with defaukt values in JPA

    REDO LOG
      how can we set the default values of an entity fields as the values of other fields from another enitity?

      public class ServiceF{
        
       @Id
       private Long id
      
       @Basic
       private String name;
      
       @ManyToOne
       private Integer transactionCode;
      
       private Integer maxAmount;
      
      
      }
       
      and the transaction entity:

      @Entity
      ...
      public class Transaction{
      
       @Id
       private Integer transCode;
      
       @Basic
       private Integer maxAmount;
      
      
      
      }
      the service fee (ServiceF) entity contains the transaction fields and the transactionCode in the ServiceF entity is a foreign key referencing to the Transaction primary key,

      what I want know is if I omit the transaction maxAmount in the ServiceF I would like it to have the value of the same transaction maxAmount in the Transaction entity of the

      transaction that has the transactionCode in the Transaction entity


      thanks for any help
        • 1. Re: help with defaukt values in JPA
          r035198x
          Write a method annotated as @PrePersist and set the defaults there.
          • 2. Re: help with defaukt values in JPA
            REDO LOG
            Hi, thank you for responding

            as I understand from your response, you suggest to do the following:
            @Entity
            public class ServiceF{
            
            //fields, setters and getters
            
              @PrePersist
              private void init(){
                  // and here call the EJB to get the values of the Transaction attributes that has the id = transactionCode
                      
                      
                 }
            
            }
            • 3. Re: help with defaukt values in JPA
              r035198x
              Not quite, your ManyToOne relationship should not be to the Integer id but the the related entity itself. Then in prepersist there are no ejb calls required. just getting the value from the related entity. All you have to deal with are object relationships and JPA will do the id links for you. Google "JPA tutorial" for details.
              • 4. Re: help with defaukt values in JPA
                REDO LOG
                Hi, here is the class modified:
                public class ServiceF{
                  
                 
                  @Id
                 private Long id
                 
                 @Basic
                 private String name;
                 
                 @ManyToOne
                 @JoinColumn(name = "transaction_code")
                 private Transaction transaction;
                 
                 @Basic
                 private Integer maxAmount;
                
                
                 @PrePersist
                 public void unit(){
                 // call the ejb to getb the values of the transaction withn the code = transactionCode
                
                
                 }
                
                
                }
                right?
                • 5. Re: help with defaukt values in JPA
                  r035198x
                  The section 3.5 of JPA specification states:
                  “In general, the lifecycle method of a portable application should not invoke EntityManager or Query operations, access other entity instances, or modify relationships within the same persistence context. A lifecycle callback method may modify the non-relationship state of the entity on which it is invoked.”
                  Better set those values in your service layer or re-look your model design.