-->
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: JPA with Hibernate insertable & updatable question
PostPosted: Sun Jun 15, 2008 3:58 am 
Newbie

Joined: Sun Jun 15, 2008 2:07 am
Posts: 4
Location: Malaysia
Dear all,

I am configuring a table "A" to have a reference to master table "B". The codes are as below:

@Entity
public class A {
@Column(name="B_CODE")
private String bCode;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="B_CODE",insertable=false,updatable=false)
private B b;
...

When I try to persist() or merge() my detached A instance, I hit this exception:

org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing

I am working on a 3 tier web application. I know that I have a invalid/transient reference of B in A because i instantiate a new instance of B to avoid my detached A instance hitting NullPointerException when rendering output at web layer. Besides, the transient B instance can keep some value e.g. bDescription that was needed to be displayed at the web layer.

Why in the world do hibernate need to bother about my transient reference of B in entity A since I have already marked the reference field with insertable and updatable = false? The value that I am going to persist/merge into my physical storage is in the attribute bCode. Besides, I did not mark any cascading persist/merge effect on association B in entity A.

To resolve this problem, I temporary set my b reference to null before executing persist() / merge() method for entity "A". To overcome this extra effort, I have implemented @PrePersist and @PreUpdate in my class "A".

@PrePersist
@PreUpdate
public void preSave() {
this.b = null;
...

Nevertheless, I am still getting the TransientObjectException. Is hibernate going to treat this as a bug or I have to use my tedious alternative way to overcome this problem? Millions thanks for help.

_________________
Andy


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 15, 2008 4:35 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

Could you be a little more specific? How exactly are A and B mapped. what are the @Id properties, ... ? How does the persist/merge code looks like?

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 15, 2008 7:58 pm 
Newbie

Joined: Sun Jun 15, 2008 2:07 am
Posts: 4
Location: Malaysia
Hi Hardy,

Thanks for the reply. Below is how class A & B were defined:

@Entity
public class A {
@Id
@Column(name="A_CODE")
private String aCode;

@Column(name="B_CODE")
private String bCode;

@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="B_CODE",insertable=false,updatable=false)
private B b;
...


@Entity
public class B{
@Id
@Column(name="B_CODE")
private String bCode;

// no association for entity A defined
...


Basically you can think of A as the business transaction table and B as a master setup table being referenced by A.

The persist & merge codes is simple:

Persist: entityManager.persist(a);
Merge: a = entityManager.merge(a);

_________________
Andy


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 17, 2008 6:36 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

Your entity B is not transient. You marked the join column as non insertable and updatable, but that does not make it transient. When the entity manager detects the state of all the objects it considers B as transient if the id is not a valid persisted id.

I think if you want to stick with this approach you will have to keep setting b to null before persisting. You could also make sure that your dummy B instance uses as id the id of an already persisted instance (could be a instance just created for this purpose). Given your mapping it will work. Personally I don't like it. I am also not sure about mapping the column B_CODE effectively twice. Once indirectly via the JoinColumn annotation and once as a simple string property.

Regarding cascading - just because you did not specify a cascading option the default is none and hence you are responsible to persist the transient object yourself.

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jun 22, 2008 10:29 am 
Newbie

Joined: Sun Jun 15, 2008 2:07 am
Posts: 4
Location: Malaysia
Hi Hardy,

Thanks for your reply, however I felt that hibernate should just ignore the object if it is insertable and updatable = false.

What about the @PrePersist & @PreUpdate method? Since I decided to manually set them to null, i purposely created the PreSave() method to handle setting those transient object to null. However, hibernate doesn't seems to follow the specification of JPA properly.

Can we take into consideration that this is a bug?

Thanks,

_________________
Andy


Top
 Profile  
 
 Post subject: Re: JPA with Hibernate insertable & updatable question
PostPosted: Wed May 06, 2009 7:57 am 
Newbie

Joined: Wed May 06, 2009 5:13 am
Posts: 2
I would also expect that there is some way to just save one object and ignore the referenced objects - is that really impossible with JPA?
The hibernate method saveOrUpdate seems to do that: http://forum.hibernate.org/viewtopic.php?f=1&t=996421


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.