简体   繁体   中英

Open JPA 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.

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



@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

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void createTaStock() throws Exception {
    TaStock taStock = new TaStock();
             ...

    TaStockPrice taStockPrice = new TaStockPrice();
             ...

    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:

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

我从TaStockPrice删除了private Integer tastockid" ,并修改了@JoinColumn(name = "tastockid", nullable = false, updatable = false, insertable = true)来解决它。

You are setting the collection as being not insertable nor updateable. This way hibernate will never persist it.

You could set how hibernate should treat this relation using the cascade-setting in your annotation. For more information, here is a thorough blog-post on the subject: http://www.mkyong.com/hibernate/hibernate-cascade-example-save-update-delete-and-delete-orphan/ .

Use below annotation on tastockpriceList .

@OneToMany
@Cascade(CascadeType.ALL)
@JoinColumn(name="tastock")

That should resolve the problem.

In order to enable save ability on a @OneToMany relation eg

@OneToMany(mappedBy="myTable", cascade=CascadeType.ALL)
private List<item> items;

Then you have to tell to your @ManyToOne relation is allowed to update myTable like this updatable = true

@ManyToOne
@JoinColumn(name="fk_myTable", nullable = false, updatable = true, insertable = true)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM