How to persist two entities with JPA

后端 未结 2 1409
醉话见心
醉话见心 2020-12-03 16:18

I am using the JPA in my webapp and I can\'t figure out how to persist two new entities that relate to each other. Here an example:

These are the two entities

2条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-03 16:24

    You can persist one object and its child at a time. So I think this should work:

    ProfilePicture profilePicture = new ProfilePicture("http://www.url.to/picture.jpg");  // create the picture object
    Consumer consumer = new Consumer("John Doe"); // create the consumer object
    consumer.setProfilePicture(profilePicture); 
    consumerFacade.create(consumer);
    

提交回复
热议问题