To illustrate my question consider the following example:
@Entity
public class Box implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
@OneToMany //No cascade
private List<Item> items;
.
.
.
}
Above we have a Box which has a one-to-many association with Item
Now say you grab a Box object and start filling it with Items and try to persist it. It will tell you that you cannot do this because you have some non-transient nested objects with no cascade (or something like that).
In this situation, where you only want an association (with no persistence), but you want to use those fields while the object is alive, what do you do when you want to persist it?
Do you null the list of Item? Do you annotate it with @Transient?
That is my question. Thanks!
If your Item object is not an database entity you should annotate the collection as @Transient. If the Item object is mapped into the database you should use the @OneToMany annotation so this collection will be read from the database. To prevent this collection from being stored when you save the Box object you can add the @JoinColumn annotation and set the insertable and updatabble attribute to false: