Well, my use case is very simple. I have an Employee that manages many Orders. In addition, an Order can have many Products.There are two kind of products, these that are stored already and others (custom products)that could be created in anytime.So my problem is what to do, if the same product already exists.
So something like that:
Order order= new Order();
order.add(new product()); //loop here in order to add many products
Employee.add(order);
It will just add duplicate records for Products.
davided80 wrote:
Another solution:
3) Retrieve the id with a query instead of loading the whole object
Code:
Long productId = session.createQuery( "SELECT id FROM Product WHERE naturalId = ?" ).setParameter(0, naturalId).uniqueResult();
Product transient.SetId( productId );
session.saveOrUpdate( transient );
Thank You!That works.This is the most straightforward solution in my opinion. The other solution is 2. but i think is slower than 3.
It would be convenient, that hibernate automatically does that only by checking the natural id. Especially if i have a large collection of transient objects.