-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Saving OneToMany , foreign key not set
PostPosted: Fri Feb 08, 2013 4:33 pm 
Newbie

Joined: Thu Jul 28, 2011 2:54 pm
Posts: 12
I've two tables: TaStock and TaStockPrice. Field tastockid in table TaStockPrice is the foreign key to table TaStock.

Code:
@Entity
public class TaStock {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Integer id

    @OneToMany(mappedBy = "taStock", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    private List<TaStockPrice> tastockpriceList;

    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);
       }
    }
    ....
}



Code:
@Entity
public class TaStockPrice {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    Integer id
    @Column
    private Integer tastockid;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "tastockid", nullable = false, updatable = false, insertable = false)
    private TaStock taStock;
    ...
}



persisting taStock with Children

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);
}


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

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


Thanks in advance for help


Top
 Profile  
 
 Post subject: Re: Saving OneToMany , foreign key not set
PostPosted: Sat Feb 09, 2013 5:25 am 
Newbie

Joined: Fri Feb 01, 2013 2:51 pm
Posts: 3
You should not have tastockid field in tastockprice class. Hibernate takes care of it through the reference to tastock.


Top
 Profile  
 
 Post subject: Re: Saving OneToMany , foreign key not set
PostPosted: Sun Feb 10, 2013 4:51 pm 
Newbie

Joined: Thu Jul 28, 2011 2:54 pm
Posts: 12
I've commented out the field taStockId in the class TaStockPrice. But still the same exception.

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

The table in Postgresql looks like this :

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 (id) MATCH SIMPLE
ON UPDATE CASCADE ON DELETE CASCADE
)

With my current setup, hibernate doesn't seem to set field tastockid. How Can I make sure that hibernate recognised tastockid as the foreign key, and sets this field when saving TaStock ?


Top
 Profile  
 
 Post subject: Re: Saving OneToMany , foreign key not set
PostPosted: Mon Feb 11, 2013 3:45 pm 
Newbie

Joined: Thu Jul 28, 2011 2:54 pm
Posts: 12
I removed "private Integer tastockid" from TaStockPrice, and modified
@JoinColumn(name = "tastockid", nullable = false, updatable = false, insertable = true)

solved.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.