Is it possible to establish an entity relationship only with an identifier?

I have a JPA (Hibernate) object:

@Entity class Transaction { @ManyToOne private Room room; } 

When I create a new Transaction , I know the Room identifier that it should refer to (but does not have a Room object). Can I somehow create and save a Transaction with just this information, or do I really need:

 Room room = em.find(roomId, Room.class); em.persist(new Transaction(room, ...)); 
+4
source share
1 answer

I had a similar problem when I found an alternative solution, but maybe this is not the best practice.

Now, since the display is dependent on roomId , create the Room constructor (type roomId) and set the bean before saving the bean transaction . Therefore, you need to get data from the database. What hibernation cares about Identity, what it needs to display beans.

I used this approach to get data, and I hope that you do not want the Room to be updated when updating the Transaction . So set insert, update the mapping properties to false.

+1
source

All Articles