0

I have a class Notification which contains an object messengerData:

public class Notification{

    @OneToOne(mappedBy = "messengerDataId")
    private MessengerData messengerData;
    ...
}

The MessengerData class contains a map of resources, it means that messengerdata contains couples of this is why I am using the one to many relation:

public class MessengerData{

        @OneToOne(mappedBy = "messengerData")
        private Notification notification;

        @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.REMOVE },
                 fetch = FetchType.EAGER)
        @Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DETACH })
        @JoinTable(name = HemisTablesNames.MESSENGER_RESOURCES, joinColumns = @JoinColumn(name = "idResource"),
                 inverseJoinColumns = @JoinColumn(name = "messengerDataId"))

        private Map<String, Resource> resources = new HashMap<String, Resource>();
        ...

The class Resource contains a map< String, NotificationTextData>:

    @OneToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.REMOVE },
        fetch = FetchType.EAGER)
@Cascade({ org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.DETACH })
@JoinTable(name = HemisTablesNames.MESSENGER_NOTIF_TEXT_DATA, joinColumns = @JoinColumn(name = "idNTD"),
        inverseJoinColumns = @JoinColumn(name = "idResource"))
private Map<String, NotificationTextData> textDatas;

Here is how I save object:

EntityManager.persist(notification);
EntityManager.commitTransaction();

My problem is that I am getting this exception:

Couldn't save notification : org.hibernate.PersistentObjectException: detached entity passed to persist: com.ubiant.hemis.type.Notification

Could someone help me with this?

Aymen Ragoubi
  • 298
  • 5
  • 22
  • Add some code where you save entity – Nikolai Shevchenko Sep 26 '18 at 17:02
  • I edit my post and added how I save object but I don't think that problem comes from that. – Aymen Ragoubi Sep 27 '18 at 08:40
  • Aymen, the object that you're trying to save (`notification`), where it is obtained from? – Nikolai Shevchenko Sep 27 '18 at 09:05
  • this is the object Notification that I instanciate when receiving request of a new notification, which I receive it as a json object. – Aymen Ragoubi Sep 27 '18 at 09:30
  • If you *instantiate* notification then you can't use persist, because the produced object is not attached to hibernate session. You should use `merge` instead of `persist`. Details here: https://stackoverflow.com/a/13837495/2224047 – Nikolai Shevchenko Sep 27 '18 at 09:40
  • I don't think that the problem is that, because I am not touching the code saving object to DB, it's a so complicated code and it works for a while, I believe that problem comes from annotation when I use Map – Aymen Ragoubi Sep 27 '18 at 09:43
  • I posted last time another post which explains better what I want to do, [https://stackoverflow.com/questions/52364780/hibernate-exception-onetomany-or-manytomany-targeting-an-unmapped-class-java] – Aymen Ragoubi Sep 27 '18 at 09:45
  • @Nikolay Finally using merge resolve my problem, post an answer so I can accept it. – Aymen Ragoubi Sep 27 '18 at 13:42

2 Answers2

0

try to use something like this:

@ElementCollection(targetClass = String.class)
@CollectionTable(name = "MAP")
@MapKeyColumn(name="key")
@Column(name="value")
private Map<String, String> map;

You're trying to persist an object without some references. Maybe that's your problem.

0

If you instantiate notification then you can't use persist, because the produced object is not attached to hibernate session. You should use merge instead of persist

Nikolai Shevchenko
  • 7,083
  • 8
  • 33
  • 42