Can I set an entity relation with just the ID?
问题 I have a JPA (Hibernate) entity: @Entity class Transaction { @ManyToOne private Room room; } When I create a new Transaction , I know the ID of the Room that it should refer to (but don't have a Room object). Can I somehow create and persist a Transaction with just this info, or do I really need to: Room room = em.find(roomId, Room.class); em.persist(new Transaction(room, ...)); 回答1: You can use EntityManager.getReference() to get a proxy of the related entity without accessing the database.