guillaume kotarba guillaume kotarba - 3 months ago 26
Java Question

JPA - ArrayList relationship that was not marked cascade PERSIST

Hello I have this error message, I don't really know what to do.

"a new object was found through a relationship that was not marked cascade PERSIST"

I know there is something wrong about the entity relationship, but I tried a lot of things and it doesn't work.

Here are my two beans :

@Entity
public class Emoji implements Serializable {

private static long serialVersionUID = 1L;

public static long getSerialVersionUID() {
return serialVersionUID;
}

public static void setSerialVersionUID(long aSerialVersionUID) {
serialVersionUID = aSerialVersionUID;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
private Utilisateur createur;
private String titre;
private List<Message> conversations;...


...

@Entity
public class Message implements Serializable {

private static long serialVersionUID = 1L;

public static long getSerialVersionUID() {
return serialVersionUID;
}


public static void setSerialVersionUID(long aSerialVersionUID) {
serialVersionUID = aSerialVersionUID;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String message;
@ManyToOne
private Utilisateur auteur;

public Message() {
} ...

Answer

Add @OneToMany annotation to your conversations field.

@Entity
public class Emoji implements Serializable  {

private static long serialVersionUID = 1L;

public static long getSerialVersionUID() {
    return serialVersionUID;
}

public static void setSerialVersionUID(long aSerialVersionUID) {
    serialVersionUID = aSerialVersionUID;
}
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne
private Utilisateur createur;
private String titre;
@OneToMany(cascade = CascadeType.PERSIST)
private List<Message> conversations;
}