"Why should I train them up when they might leave? Let somebody else train them, then I'll hire them afterwards." — Companies, in a prisoner's dilemma.
well you sit them down before they take the offer and level with them "If you come here I promise I'll treat you good and train you up for a bright future, but in exchange ideally I want you to stay for at least two years if you can help it, otherwise the cost of training doesn't work out"
It's not a legal agreement but a gentleman's agreement. They may still freely leave for whatever reason, especially if they were not treated well or treated like a slave (ie employer broke the agreement), but if all goes well it's some assurance, or at least a communication so everyone is on the same page. And usually being on the same stage and starting out with such trust sets the stage nicely for a good, trust-based safe working relationship
Training people is a cost, an investment, if everyone does it the cost is amortized across the industry. If I can cut that cost by using AI I'm now at a competitive advantage, everyone will look to cut that cost because the downside of paying for training juniors that may leave is worse for that company than the downside of the whole industry not training juniors any more.
Fast forward half a decade forward, and there's no juniors or mediors to onboard, no-one to have gemeric expertise that can be fine tuned to your product.