-->
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.  [ 5 posts ] 
Author Message
 Post subject: Exception: Hibernate3 + SpringFrameWork
PostPosted: Thu Oct 27, 2005 10:38 am 
Newbie

Joined: Fri Oct 14, 2005 11:42 am
Posts: 9
Location: Madrid, SP
Hello again, I'm having some problems saving persisted data to a table. I don't know if this is the rigth place to post this doubt, I posted it in springframework forum too. If this isn't relate with hibernate forums, please feel free to delete this topic.

Well, Here I paste what I've wrote this morning in the spring forum:

{ // BEGIN POST

This excepction takes place when I want to do the save to the persisted object into the database (getHibernateTemplate().save(object)):

Exception thrown executing action 'es.tid.sgd.web.flow.rangerequest.RangeRequestAction@8caf5' in state 'viewInitRangeRequest' of flow 'rangeRequestFlow'; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: es.tid.sgd.model.bean.usuario.Usuario;
nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: es.tid.sgd.model.bean.usuario.Usuario

That means that something is wrong with that object (Usuario = user). Hibernate mapping is good cause I did a select searching (getHibernateTemplate().find("criteria")) for range requests and it returned to me all data that is in the table called Range Requests. So, if it could do a select, I think that a save should work too, shouldn't it?

The problem is when I get the object from the request this way:
RangeRequest rr = (RangeRequest) getFormObject(ctx); // ctx = context
rr have some attributes from the request, like name, size, managerUsers, but I don't get an attribute called requestUser who is that does the request.
To solve this, I thought that I could do something like this:

public void insert(Bean bean) {

SolicitudRango sr = (SolicitudRango) bean;

Usuario u=new Usuario();
u.setCodUsuario(14);
u.setApellidos("ORTEGA VARAS");
u.setNombre("AMAIA");
u.setLogin("amaia");
u.setTelefono("");
u.setTelefonoMovil("");
u.setCorreoElectronico("amaia.arrondo@telefonica.es");
sr.setUsuarioSolicitante(u);
// Is here the problem??

getHibernateTemplate().save(sr);

}

But it seems that spring and hibernate do not like this way ...
I hope that now I gave you all the info you need to solve my 'big doubt'.

} // END POST

And the same for you,

Thanks in advance.
Miguel M.


Top
 Profile  
 
 Post subject: cascade
PostPosted: Thu Oct 27, 2005 12:38 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
You are truing to save SolicitudRango and not the user directly it means that is the user is new then SolicitudRango should define cascade=”all|save-update” to be able to save the user, otherwise it throws the exception you see.
http://www.hibernate.org/hib_docs/v3/re ... ild-update

PS: I suggest not to use HibernateTemplate, there are many problems with the approach. It is better to use alternative strategies provided by Spring.

http://www.onjava.com/lpt/a/5854

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 27, 2005 2:27 pm 
Newbie

Joined: Fri Oct 14, 2005 11:42 am
Posts: 9
Location: Madrid, SP
Hi, I have to thank you for answering my post :)
I won't be able to prove different strategies till tomorrow when Im at work.

But, let me make sure that I have to try another way to insert data in my database.

SolicitudRango has an attribute called user (well, it has more attributes but all their values are got from the FlowScope and there is no problems with those attributes) which is an object with attributes like:
userCode
userName
userMail
userRole
and so on ...

SolicitudRango table is like this:
solicitud_rango_code
name_solicitud_rango
request_user_code <-- I only have a code for the requester (integer) and I don't want to insert a new user into the database. I want to insert only its code.
manager_user_one_code
manager_user_two_code
... and more

With getHibernateTemplate().save((SolicitudRango) object);, I want to insert, into the table called SolicitudRango, an entry with the elements written above (obviusly).
I mean, I want that when it inserts a new SolicitudRango object, hibernate only inserts the request_user_code instead inserting a new user to the table called users. Request User is the one who has logged into the system (for some reasons we haven't implemented loggin in our system, but if it happens with this object, it will happen with more like requestType, Date and more predefined values for a range request).

If I can remember, in the mapping of SolicitudRango.java (SolicitudRango.hbm.xml) I have this:
<many-to-one class="package.to.user.Usuario" column="request_user_code" lazy="true"/>
(I don't remember If I have more attributes, but it's something like this, mapping the user pojo into SolicitudRango.hbm.xml)

So, I have to change that mapping or I have to try another way using what you tell me about spring?

Thank you again,
Miguel M.


Top
 Profile  
 
 Post subject: cascade
PostPosted: Thu Oct 27, 2005 2:50 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Should be
<many-to-one class="package.to.user.Usuario" column="request_user_code" lazy="true" cascade='all'/>

http://www.hibernate.org/hib_docs/v3/re ... -manytoone

It will work with HibernateTemplate.

Getting rid of HibernateTemplate is optional, but strungly encouraged.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 27, 2005 3:02 pm 
Newbie

Joined: Fri Oct 14, 2005 11:42 am
Posts: 9
Location: Madrid, SP
Thanks a lot, I will give it a chance tomorrow at work.

I'm realising that I'm so weak in hibernate mapping. I hope that this works cause I have to give in a little functionality tomorrow :S ... Anyways, Thanks again :)

Miguel M.


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