Scene:
I'm design a stock equity calculation tools:
I can create a lot of stock pool, pool inside to add some stock, typical one-to-one relationship, one-way
So every stock, and I are going to have a lot of transactions, so that the stock of transaction records in the pool a stock, it is a one-to-many relationship, one-way
So now I should how to implement this kind of relationship in a tree?
I try to the following:
StockPool one-to-many associations Stock, one party management
Stock, and one-to-many associations TransactionHistory, or one party management, also is the Stock, but the Stock is StockPool management side
Code smells long, so list only relevant content, no matter how to cascade, is a party in the management of
For StockPool
the @onetomany (cascade=CascadeType. ALL the fetch=FetchType. LAZY)
@ JoinColumn (name="id")//that field is key, used to correlate the article table
Public SetGetStocks () {
Return stocks;
}
Public void setStocks (SetStocks) {
This. Stocks=stocks;
}
And
The @onetomany (cascade=CascadeType. ALL the fetch=FetchType. LAZY)
@ JoinColumn (name="id")//that field is key, is used to associate net table
Public SetGetNws () {
Return the NWS is;
}
Public void setNws (SetThe NWS) {
Enclosing the NWS=NWS);
}
For Stock
the @onetomany (cascade=CascadeType. ALL, fetch=FetchType. LAZY)
@ JoinColumn (name="id")//that field is key, is used to associate a stock table
Public SetGetThs () {
Return TSH.
}
Public void setThs (SetTSH) {
This. TSH=TSH;
}
So now,
I successfully created after the stock pool
Look and no problem, add a stock to an inside, the stock has two trading records, but
Java. SQL. SQLIntegrityConstraintViolationException:
Always add or update a child row: a foreign key constraint fails
(` networth_ding `. ` t_stockding `, CONSTRAINT ` FK_pi66wsimny0t01abiw8wm5ql9 `
FOREIGN KEY (` id `) REFERENCES ` t_stockpool ` (` id `))
Tragedy appeared, and foreign key constraints fails,
General meaning should be t_stockding foreign key references are t_stockpool id, but this id in t_stockpool doesn't exist, the problem is, that this is cascade operation, should be implemented by the Hibernate.
And even if I try to keep records of operation, still complains, also make uncertain, ask bosses give directions