java 打开 JPA 保存 OneToMany ,未设置外键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14592968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open JPA Saving OneToMany , foreign key not set
提问by user2023141
I've two tables: TaStock and TaStockPrice. Field tastockid in table TaStockPrice is the foreign key to table TaStock.
我有两个表:TaStock 和 TaStockPrice。TaStockPrice 表中的字段 tastockid 是 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
与孩子一起坚持 taStock
@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
javax.persistence.PersistenceException:org.hibernate.exception.ConstraintViolationException:错误:“tastockid”列中的空值违反了非空约束
回答by user2023141
I removed private Integer tastockid"
from TaStockPrice
, and modified
@JoinColumn(name = "tastockid", nullable = false, updatable = false, insertable = true)
to solve it.
我private Integer tastockid"
从 中删除 TaStockPrice
,并修改
@JoinColumn(name = "tastockid", nullable = false, updatable = false, insertable = true)
以解决它。
回答by Kurt Du Bois
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/.
您可以使用注释中的级联设置来设置 hibernate 应该如何处理这种关系。有关更多信息,这里有一篇关于该主题的详尽博客文章:http: //www.mkyong.com/hibernate/hibernate-cascade-example-save-update-delete-and-delete-orphan/。
回答by Ilan M
In order to enable save ability on a @OneToMany
relation e.g.
为了在@OneToMany
关系上启用保存能力,例如
@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
关系被允许像这样更新 myTable updatable = true
@ManyToOne
@JoinColumn(name="fk_myTable", nullable = false, updatable = true, insertable = true)
回答by Mubin
Use below annotation on tastockpriceList
.
在 上使用以下注释tastockpriceList
。
@OneToMany
@Cascade(CascadeType.ALL)
@JoinColumn(name="tastock")
@OneToMany
@Cascade(CascadeType.ALL)
@JoinColumn(name="tastock")
That should resolve the problem.
那应该可以解决问题。