Note that there are some explanatory texts on larger screens.

plurals
  1. POEclipseLink/JPA2 ManyToOne bidirectional cascade persist issue
    primarykey
    data
    text
    <p>This is similar to <a href="https://stackoverflow.com/questions/3197667/how-to-cascade-persist-using-jpa-eclipselink">How to cascade persist using JPA/EclipseLink</a></p> <p>I have to entities like so. One is RoomEntity that has a one to many bi-directional relationship with ComputerEntity. eg. each room has 0..n computers. </p> <pre><code>@Entity public class ComputerEntity implements Serializable { @Id @GeneratedValue(generator="computerSeq",strategy= GenerationType.SEQUENCE) @SequenceGenerator(name="computerSeq",sequenceName="SEQUENCECOMPUTERID",allocationSize=1) private long computerID; @Column(name = "COMPUTERID") public long getComputerID() { return computerID; } public void setComputerID(long computerID) { this.computerID = computerID; } private RoomEntity room; @ManyToOne() @JoinColumn(name = "ROOMID", referencedColumnName = "ROOMID") public RoomEntity getRoom() { return room; } public void setRoom(RoomEntity room) { this.room = room; } } //ComputerEntity @Entity public class RoomEntity { @Id @GeneratedValue(generator="roomSeq",strategy= GenerationType.SEQUENCE) @SequenceGenerator(name="roomSeq",sequenceName="SEQUENCEROOMID",allocationSize=1) private long roomID; @OneToMany(mappedBy = "room", cascade = {CascadeType.PERSIST, CascadeType.REMOVE}) private Set&lt;ComputerEntity&gt; computers; @javax.persistence.Column(name = "ROOMID") public long getRoomID() { return roomID; } public void setRoomID(long roomID) { this.roomID = roomID; } public Set&lt;ComputerEntity&gt; getComputers() { return computers; } public void setComputers(Set&lt;ComputerEntity&gt; computers) { for(ComputerEntity computer : computers) { computer.setRoom(this); } this.computers = computers; } }//RoomEntity </code></pre> <p>When I try to persist an new room with a computer like so:</p> <pre><code> RoomEntity room = new RoomEntity(); room.setAdministrator("Fox Moulder"); room.setLocation("Area 51"); ComputerEntity computer1 = new ComputerEntity(); computer1.setDescription("Alienware area51 laptop"); Set&lt;ComputerEntity&gt; computers = new HashSet&lt;ComputerEntity&gt;(); computers.add(computer1); room.setComputers(computers); roomBean.createRoom(room); </code></pre> <p>roomBean is a Stateless EJB and roomBean.createRoom simply calls entityManager.persist(room). Since I have a CascadeType.PERSIST on the computers field of RoomEntity the ComptuerEntity is created. However, if I look at the room field of that ComputerEntity I see that room field is null. I would have assumed that Eclipselink would have automatically filled in the room since I have a bi-directional relationship. In order to have the room set in this way I had to add</p> <pre><code> for(ComputerEntity computer : computers) { computer.setRoom(this); } </code></pre> <p>to room.setComputers(...). Is this the correct approach or is there a way to get Eclipselink to automatically set this? </p> <p>Thanks. -Noah</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload