-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Transient Object Exception. It doesn't work at all
PostPosted: Fri Feb 05, 2010 10:48 am 
Beginner
Beginner

Joined: Thu Nov 26, 2009 8:20 am
Posts: 23
Hello,
I'm trying to save a class diagram in a database using hibernate with JPA. Most works fine but the associations can not be saved.
My structure is the following:

Code:
public class ClassModel {
...
        @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
        @JoinColumn(name = "attribute_fk")
        @org.hibernate.annotations.Cascade(value = {org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
   private Set<Attribute> attributes;

   @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
   @JoinColumn(name = "source_association_fk")
        @org.hibernate.annotations.Cascade(value = {org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
   private Set<Association> sourceAssociations;
   
   @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
   @JoinColumn(name = "target_association_fk")
        @org.hibernate.annotations.Cascade(value = {org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
   private Set<Association> targetAssociations;
...

}


Association:
Code:
public class Association {
..

   @ManyToOne(cascade = CascadeType.ALL)
   @org.hibernate.annotations.OnDelete(action = org.hibernate.annotations.OnDeleteAction.CASCADE)
   @JoinColumn(name = "assoc_source_class_fk")
   private ClassModel source;
   @ManyToOne(cascade = CascadeType.ALL)
   @org.hibernate.annotations.OnDelete(action = org.hibernate.annotations.OnDeleteAction.CASCADE)
   @JoinColumn(name = "assoc_target_class_fk")
   private ClassModel target;
..
}


Attribute:
Code:
public class Attribute {
...
   @ManyToOne (cascade=CascadeType.ALL)
   @org.hibernate.annotations.OnDelete(action = org.hibernate.annotations.OnDeleteAction.CASCADE)
   @JoinColumn(name = "class_fk")
   private ClassModel clasS;
...
}


I can save a new class with a new attribute without any problems, but if I create 2 new classes and add an association I will get an Exception:

javax.persistence.PersistenceException: org.hibernate.type.SerializationException: could not serialize
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:647)
at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:236)
...
Caused by: org.hibernate.type.SerializationException: could not serialize
at org.hibernate.util.SerializationHelper.serialize(SerializationHelper.java:158)
at org.hibernate.util.SerializationHelper.serialize(SerializationHelper.java:178)
...
Caused by: java.io.NotSerializableException: Association
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)


If I change the cascading Type a little bit I will get this exception:
java.lang.IllegalStateException: org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: Association
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:644)
at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:236)
...

I really would appreciate any help, I was searching for a solution but failed.

Thanks in advance!
Best regards,
Alex


Top
 Profile  
 
 Post subject: Re: Transient Object Exception. It doesn't work at all
PostPosted: Fri Feb 05, 2010 11:48 am 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi StefanKNeedsHelp,

StefanKNeedsHelp wrote:
javax.persistence.PersistenceException: org.hibernate.type.SerializationException: could not serialize
at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:647)
at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:236)
...
Caused by: org.hibernate.type.SerializationException: could not serialize
at org.hibernate.util.SerializationHelper.serialize(SerializationHelper.java:158)
at org.hibernate.util.SerializationHelper.serialize(SerializationHelper.java:178)
...
Caused by: java.io.NotSerializableException: Association
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)


looks like you chopped off some important info: what actually could not be serialized. So, I guess, you are either trying to save a field value that is neither an entity nor of type Serializable or if it is an entity then cascading does not reach it.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: Transient Object Exception. It doesn't work at all
PostPosted: Mon Feb 08, 2010 4:08 am 
Beginner
Beginner

Joined: Thu Nov 26, 2009 8:20 am
Posts: 23
Hello Froestel!
First, thanks for your reply!
I don't know which important info I should hav chopped.
The hole Association class is like this:

Code:
@Entity
public class Association extends DiagramElement {

   @ManyToOne(cascade = CascadeType.ALL)
   @org.hibernate.annotations.OnDelete(action = org.hibernate.annotations.OnDeleteAction.CASCADE)
   @JoinColumn(name = "ClassModel_source_fk")
   private ClassModel source;
   
   @ManyToOne(cascade = CascadeType.ALL)
   @org.hibernate.annotations.OnDelete(action = org.hibernate.annotations.OnDeleteAction.CASCADE)
   @JoinColumn(name = "ClassModel_target_fk")
   private ClassModel target;
   
   public Association() {
      super();
      name = "";
   }
   
   public ClassModel getSource() {
      return source;
   }

   public ClassModel getTarget() {
      return target;
   }

   public void setSource(ClassModel pSource) {
      if(source == pSource)
         return;
      if(pSource == null) {
         source = pSource;
         return;
      }
      //New Connection
      if(source == null) {
         source = pSource;
      }
      //existing connection
      else {
         target.removeSourceAssociation(this);
         source.removeTargetAssociation(this);
         source = pSource;         
         target.addTargetAssociation(this);
         source.addSourceAssociation(this);
      }
   }
   
   public void setTarget(ClassModel pTarget) {
      if(target == pTarget)
         return;
      if(pTarget == null) {
         target = pTarget;
         return;
      }
      //New Target
      if(target == null)  {
         target = pTarget;
         source.addSourceAssociation(this);
         target.addTargetAssociation(this);
      }
      //Change Target
      else {
         target.removeTargetAssociation(this);
         source.removeSourceAssociation(this);
         target = pTarget;
         target.addTargetAssociation(this);
         source.addSourceAssociation(this);
      }
   }
}


And I will only get the exceptions shown above if I add the following mebers to my classmodel:

Code:
   @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
   @JoinColumn(name = "source_association_fk")
    @org.hibernate.annotations.Cascade(value = {org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
   private Set<Association> sourceAssociations;
   
   @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
   @JoinColumn(name = "target_association_fk")
    @org.hibernate.annotations.Cascade(value = {org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
   private Set<Association> targetAssociations;


Without them everything is working fine on merging the detached objects. With them I will get weather the Serializable - or the TransientObjectPersistence - Exception.

BTW: Saving the models detached objects:

Code:
sWritingManager.getTransaction().begin();
      //lMan.refresh(pDiagram);
      Diagram lDiagram = sWritingManager.merge(pDiagram);
      sWritingManager.getTransaction().commit();
      return lDiagram;


I am not working with serialized objects so I do not know where the exception comes from...

Best regards
Alex


Top
 Profile  
 
 Post subject: Re: Transient Object Exception. It doesn't work at all
PostPosted: Mon Feb 08, 2010 4:51 am 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi StefanKNeedsHelp,

doesn't it say what could not be serialized at the end of this line?
Quote:
javax.persistence.PersistenceException: org.hibernate.type.SerializationException: could not serialize

Usually the type is given that could not be serialized.

You should possibly specify the owning side of your Many-To-One relationships. In most cases that's the one with the @ManyToOne annotation. But the other side tells that it is owned:
Code:
@OneToMany(mappedBy="Association")


You may also check if you need to specify
Code:
@Cascade({org.hibernate.annotations.CascadeType.ALL,
          org.hibernate.annotations.CascadeType.DELETE_ORPHAN})

on your association instead of simply DELETE_ORPHAN.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: Transient Object Exception. It doesn't work at all
PostPosted: Mon Feb 08, 2010 6:27 am 
Beginner
Beginner

Joined: Thu Nov 26, 2009 8:20 am
Posts: 23
Hello and thanks again,

Okay, I tried to add the cascading.ALL annotation but it didn't change anything...

The type is specified within the exception, yes:

Caused by: java.io.NotSerializableException: metamodel.Association
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at org.hibernate.util.SerializationHelper.serialize(SerializationHelper.java:154)

EDIT
Changing the hole annotation on the class side doesn't change anything, too:

Code:
   @OneToMany(mappedBy="source", targetEntity = Association.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @org.hibernate.annotations.Cascade(value = {org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
   private Set<Association> sourceAssociation;
   
   @OneToMany(mappedBy="target", targetEntity = Association.class, fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @org.hibernate.annotations.Cascade(value = {org.hibernate.annotations.CascadeType.ALL, org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
   private Set<Association> targetAssociation;


I am really frustrated, because I can see only two differences between Association and the Attribute class: The second ClassModel attribute and the setter methods
Perhaps he can not handle the setter methodes??!

Achja, wir können auch gerne in Deutsch weiter machen :-D
Es ist wirklich komisch, vorallem weil ich eine TransientObjectException bekomme wenn ich ein bisschen mit den Cascades spiele.
Wenn ich alles so lasse eben die SerializeableException, obwohl ich wie gesagt keine serialisierten Objekte verwende...
Ich bin wirklich ratlos.

Vlg
Alex


Top
 Profile  
 
 Post subject: Re: Transient Object Exception. It doesn't work at all
PostPosted: Mon Feb 08, 2010 7:10 am 
Beginner
Beginner

Joined: Thu Nov 26, 2009 8:20 am
Posts: 23
Hello
argh!!!!

I solved the problem, really crazy...

Association extends DiagramElement and DiagramElement has an attribue (AbstractInfo). The AbstractInfo class is implemented by the AssociationInfo class and this class did not declare the field private static final long serialVersionUID = 1L;

Really crazy, but in the end an easy error :)

Thanks a lot for your help!

Liebe Grüße ;)
Alex


Top
 Profile  
 
 Post subject: Re: Transient Object Exception. It doesn't work at all
PostPosted: Mon Feb 08, 2010 7:12 am 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi StefanKNeedsHelp,

This was what I intended to write to you right now:
Quote:
On the serialization exception - Association must be completely serializable - this means all its fields that are meant to be serialized must be serializable including anything that comes from any parent class.


CU
Froestel

P.S.: Don't switch to German in an English speaking forum since their might be non-German speaking people reading this thread - this is no private conversation here and is supposed to probably help others, too.

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.