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.  [ 6 posts ] 
Author Message
 Post subject: Problem with Transitive Persistence
PostPosted: Mon May 19, 2008 10:54 am 
Newbie

Joined: Mon May 19, 2008 5:25 am
Posts: 5
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version:

Mapping documents: Annotation

Name and version of the database you are using: MySQL 5.0.x

Hi Guy,
I have a basic problem in bidirectional one-to-many association for transitive persistence when both parent and child are new object.
I have following classes:

Parent Code:
Code:
@Entity
public class Parent implements java.io.Serializable
{
  @Id
  @GeneratedValue
  private Long id;

  @oneToMany(mappedBy = "parent", fetch = FetchType.EAGER, cascade = javax.persistence.CascadeType.ALL)
  private Set<Child> children = new HashSet<Child>();

  public Long getId() {return id;}
  public void setId(Long id) {this.id = id;}

  public Set<Child> getChildren() {return children;}
  public void setChildren(Set<Child> children) {this.children = children;}

  public void addChild(Child c) {
       c.setParent(this);   
       children.add(c);
  }
 
  .... 
}


And the Child class looks like this:
Code:
@Entity
public class Child implements java.io.Serializable
{
  @Id
  private String id;

  @ManyToOne
  @JoinColumn(name="parentId", insertable=false, updatable=false, nullable = false )
  private Parent parent ;

  @OneToOne    //unidirectional oneToone
  private Item item;

  public Long getId() {return id;}
  public void setId(String id) {this.id = id;}

  public Parent getParent() {return parent;}
  public void setParent(Parent parent) {this.parent = parent;}

  public Item getItem() {return this.item;}
  public void setItem(Item item) {this.item = item;}
 
  .... 
}


Please, note that Item is an Entity with no reference to the Child object. Since the relation is only unidirectional and hold by the Child class.

In my Facet class, I am then doing something like this:

//create a new transient Parent
Parent parent = new Parent();

//create a new transient Child
Child child = new Child();

//Load an aleady persistent Item
Item item = itemManager.findItem(itemId);

child.setItem(item); // I am wondering what happens here, does child try to go automatically to persist state???

//add to parent, save parent and expecte to have a transitive persistence
parent.addChild(child);

manager.saveParent(parent);

//manager.saveParent calls a dao instance which performs entityManager.merge(parent)

Please note that the FlushMode is AUTO.

During the flush I am getting something like following errors:


Caused by: java.sql.SQLException: Field 'parentId' doesn't have a default value .....


I don't understand this error, since I am trying to do the transitive persistence throw the Parent/Child. I was expected a cacade merge.

I will appreciate any suggestion to make me understand what I am missing.

Thx


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 19, 2008 1:45 pm 
Beginner
Beginner

Joined: Wed Dec 20, 2006 11:21 am
Posts: 20
This looks correct, except for the part with manager.saveParent(parent)

What does this code do exactly? Calling merge does not seem correct - you would rather call save. Try to manually call session.save(parent) and see if the child is saved


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 19, 2008 2:07 pm 
Newbie

Joined: Mon May 19, 2008 5:25 am
Posts: 5
thank you for your reply.

Actually the saveParent in Manager does something like this:

Code:
private ParentDao dao;

...

public Parent saveParent(Parent parent) {

     .....
     return dao.saveParent(parent);

}


In ParentDao Implemenation:

Code:
   private EntityManager entityManager;
   .....

   public Parent saveParent(Parent parent) {
        return entityManager.merge(parent);
   }


I will be happy for any suggestion.


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 22, 2008 6:06 pm 
Newbie

Joined: Mon May 19, 2008 5:25 am
Posts: 5
OK it looks like nobody can help here!

As soon as I removed insertable = false and updatable=false from the @ManyToOne association, everything was fine!!!! Roughly speaking it is quite strange behaviours. Completely different to the book explication.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 23, 2008 9:00 am 
Pro
Pro

Joined: Tue Jun 12, 2007 4:13 am
Posts: 209
Location: Berlin, Germany
Hi Romy,

what you did with "insertable=false, updatable=false" on the "@ManyToOne" is the strange thing. In the Hibernate book (p. 266 & 293f) these annotation attributes are used - but they are used to as a workaround if you would need to reverse the "mappedBy" annotation to the other side.

Carlo


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 23, 2008 10:03 am 
Newbie

Joined: Mon May 19, 2008 5:25 am
Posts: 5
Hi Carlo,

thank u for your repy.

My understanding of inertable=false and updatable=false was used to realise a true Parent/Child transitive persistence, manged only by Parent. What I really want to realise.
As I removed these parameters, it would be then possible to save the parent via the child, isn't it? If I am wrong please correct me so that I can have a better understanding.

However this is not the effect I want to have. I would like to avoid any tentative to update or persist a Parent via the child. The insertable=false and updatable=false was the only option I found in the hibernate book.

I will be happy to hear how to realise it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.