2015-09-25 92 views
0

我使用JPA,休眠,春天和MySQL休眠@OneToOne不能添加或更新子行:外鍵約束

不能添加或更新子行,外鍵約束失敗(db1stock_detail,約束FK_STOCK_ID外鍵(STOCK_ID)參考文獻stockSTOCK_ID))

創建腳本

CREATE TABLE IF NOT EXISTS `stock` (
    `STOCK_ID` int(10) unsigned NOT NULL, 
    `STOCK_CODE` varchar(10) NOT NULL, 
    `STOCK_NAME` varchar(20) NOT NULL 
) ENGINE=InnoDB AUTO_INCREMENT=86 DEFAULT CHARSET=utf8; 

CREATE TABLE IF NOT EXISTS `stock_detail` (
    `STOCK_ID` int(10) unsigned NOT NULL, 
    `COMP_NAME` varchar(100) NOT NULL, 
    `COMP_DESC` varchar(255) NOT NULL 
) ENGINE=InnoDB AUTO_INCREMENT=72 DEFAULT CHARSET=utf8; 

ALTER TABLE `stock` 
    ADD PRIMARY KEY (`STOCK_ID`) USING BTREE, 
    ADD UNIQUE KEY `UNI_STOCK_NAME` (`STOCK_NAME`), 
    ADD UNIQUE KEY `UNI_STOCK_CODE` (`STOCK_CODE`) USING BTREE; 

ALTER TABLE `stock_detail` 
    ADD PRIMARY KEY (`STOCK_ID`) USING BTREE; 

ALTER TABLE `stock` 
    MODIFY `STOCK_ID` int(10) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=86; 

ALTER TABLE `stock_detail` 
    MODIFY `STOCK_ID` int(10) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=72; 

ALTER TABLE `stock_detail` 
    ADD CONSTRAINT `FK_STOCK_ID` FOREIGN KEY (`STOCK_ID`) REFERENCES `stock` (`STOCK_ID`); 

股票實體

@Entity 
@Table(name = "stock") 
@XmlRootElement 
@Component("astock") 
@NamedQueries({ 
    @NamedQuery(name = "Stock.findAll", query = "SELECT s FROM Stock s"), 
    @NamedQuery(name = "Stock.findByStockId", query = "SELECT s FROM Stock s WHERE s.stockId = :stockId"), 
    @NamedQuery(name = "Stock.findByStockCode", query = "SELECT s FROM Stock s WHERE s.stockCode = :stockCode"), 
    @NamedQuery(name = "Stock.findByStockName", query = "SELECT s FROM Stock s WHERE s.stockName = :stockName")}) 
public class Stock implements Serializable { 
    private static final long serialVersionUID = 1L; 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Basic(optional = false) 
    @Column(name = "STOCK_ID") 
    private Integer stockId; 
    @Basic(optional = false) 

    @Size(min = 1, max = 10) 
    @Column(name = "STOCK_CODE") 
    private String stockCode; 
    @Basic(optional = false) 

    @Size(min = 1, max = 20) 
    @Column(name = "STOCK_NAME") 
    private String stockName; 
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "stock") 
    private StockDetail stockDetail; 

StockDetail實體

@Entity 
@Table(name = "stock_detail") 
@XmlRootElement 
@NamedQueries({ 
    @NamedQuery(name = "StockDetail.findAll", query = "SELECT s FROM StockDetail s"), 
    @NamedQuery(name = "StockDetail.findByStockId", query = "SELECT s FROM StockDetail s WHERE s.stockId = :stockId"), 
    @NamedQuery(name = "StockDetail.findByCompName", query = "SELECT s FROM StockDetail s WHERE s.compName = :compName"), 
    @NamedQuery(name = "StockDetail.findByCompDesc", query = "SELECT s FROM StockDetail s WHERE s.compDesc = :compDesc")}) 
public class StockDetail implements Serializable { 
    private static final long serialVersionUID = 1L; 
    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Basic(optional = false) 
    @Column(name = "STOCK_ID") 
    private Integer stockId; 
    @Basic(optional = false) 

    @Size(min = 1, max = 100) 
    @Column(name = "COMP_NAME") 
    private String compName; 

    @Basic(optional = false) 
    @Size(min = 1, max = 255) 
    @Column(name = "COMP_DESC") 
    private String compDesc; 
    @JoinColumn(name = "STOCK_ID", referencedColumnName = "STOCK_ID") 
    @OneToOne(cascade = CascadeType.ALL) 
    private Stock stock; 

如何,我想保存它。

Stock StockServices = (Stock) applicationContext.getBean("astock"); 

    Stock stock = new Stock(); 
    stock.setStockCode("123"); 
    stock.setStockName("AAPL"); 

    StockDetail stockDetail = new StockDetail(); 
    stockDetail.setCompName("Apple"); 
    stockDetail.setCompDesc("A hardware and software company"); 

    stockDetail.setStock(stock); 
    stock.setStockDetail(stockDetail); 

    sessionFactory.getCurrentSession().saveOrUpdate(stock); 
+0

看起來你忘了在你的桌子上創建主鍵。 – Everv0id

回答

0

你的問題是,您嘗試使用stock_detail.STOCK_ID既是PRIMARY KEYstock_detailFOREIGN KEY從股市和你爲他們分配不同的自動遞增值。

當Hibernate保存它們時,它試圖用STOCK_ID =86stock_detailSTOCK_ID=72來儲存股票,該股票打破了外鍵契約。

請在您的stock_detail表E.g stock_detail_id中作爲單獨的主鍵,並將stock_id作爲單獨的列。

0

同意接受的答案。

仍然如果你想堅持使用外鍵作爲子表中的主鍵,請在子表上使用Foreign Key Generator。通過這種方式,我們可以減少用於創建太多獨特ID的DB引擎的開銷。

參考:ForeignKeyGeneratorUsage

相關問題