我使用JPA,休眠,春天和MySQL休眠@OneToOne不能添加或更新子行:外鍵約束
不能添加或更新子行,外鍵約束失敗(
db1
。stock_detail
,約束FK_STOCK_ID
外鍵(STOCK_ID
)參考文獻stock
(STOCK_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);
看起來你忘了在你的桌子上創建主鍵。 – Everv0id