Home > Back-end >  Hibernate a one-to-many relationship requires each have a one-to-many associations, how to do?
Hibernate a one-to-many relationship requires each have a one-to-many associations, how to do?

Time:11-27

The first question, could describe not clear, please bosses don't angry, limited experience, you mentioned, I'll improve
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 Set GetStocks () {
Return stocks;
}

Public void setStocks (Set Stocks) {
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 Set GetNws () {
Return the NWS is;
}

Public void setNws (Set The 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 Set GetThs () {
Return TSH.
}

Public void setThs (Set TSH) {
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
  • Related