Results 1 to 2 of 2

Thread: Saving OneToMany , foreign key not set

  1. #1

    Default Saving OneToMany , foreign key not set

    I've two tables: TaStock and TaStockPrice. Field tastockid in table TaStockPrice is the foreign key to table TaStock.
    PHP Code:
        @Entity  
        
    public class TaStock {  
          
            @
    Id  
            
    @GeneratedValue(strategy GenerationType.IDENTITY)  
            
    Integer id  
          
            
    @OneToMany(mappedBy "taStock"fetch FetchType.LAZYcascade CascadeType.ALL)  
            private List<
    TaStockPricetastockpriceList;  
          
            public 
    void addTaStockPrice(TaStockPrice taStockPrice) {  
               if (
    taStockPrice == null) {  
                   return;  
               }  
               
    taStockPrice.setTaStock(this);  
               if (
    tastockpriceList == null) {  
                   
    tastockpriceList = new ArrayList<TaStockPrice>();  
                   
    tastockpriceList.add(taStockPrice);  
               } else if (!
    tastockpriceList.contains(taStockPrice)) {  
                   
    tastockpriceList.add(taStockPrice);  
               }  
            }  
            ....  
        } 
    PHP Code:
        @Entity  
        
    public class TaStockPrice {  
          
            @
    Id  
            
    @GeneratedValue(strategy GenerationType.IDENTITY)  
            
    Integer id  
          
            
    @Column(insertable true)   
            private 
    Integer tastockid;  
          
          
            @
    ManyToOne(fetch FetchType.LAZY)  
            @
    JoinColumn(name "tastockid"nullable falseupdatable falseinsertable false)  
            private 
    TaStock taStock;  
            ...  
        } 
    persisting taStock with Children

    PHP Code:
    @Test  
    @Transactional(propagation Propagation.REQUIRES_NEW)  
    public 
    void createTaStock() throws Exception {  
        
    TaStock taStock = new TaStock();  
                 ...  
      
        
    TaStockPrice taStockPrice = new TaStockPrice();  
        
    taStockPrice.setTaStock(taStock);  
                 ...  
      
        
    taStock.addTaStockPrice(taStockPrice);  
        
    taStockService.persist(taStock);  


    When persisting a parent class, hibernate automatically persist the children of that class. But instead, the following exception occurs:

    javax.persistence.PersistenceException: org.hibernate.exception.ConstraintViolationExcepti on: ERROR: null value in column "tastockid" violates not-null constraint

    The DB table looks like this:
    PHP Code:
    CREATE TABLE tastockprice
    (
    id serial NOT NULL,
    tastockid integer NOT NULL,
    ...
    CONSTRAINT tastockprice_pkey PRIMARY KEY (id ),
    CONSTRAINT tastockprice_tastockid_fkey FOREIGN KEY (tastockid)
    REFERENCES tastock (idMATCH SIMPLE
    ON UPDATE CASCADE ON DELETE CASCADE

    Many thanks for help,

    Frank

  2. #2

    Default

    I removed "private Integer tastockid" from TaStockPrice, and modified
    @JoinColumn(name = "tastockid", nullable = false, updatable = false, insertable = true)

    solved.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •